MobileAndroidHow Gradle Works in the Android Build System

How Gradle Works in the Android Build System

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Introduction

There are various world-class build solutions available for developers. Ant and Maven come to mind for Java developers, but as any Android developer would know, the de facto buildset for Android development, Android Studio, is Gradle.

Gradle is an easily customizable build system that supports building by a convention model. Gradle is written in Java, but the build language is Groovy DSL (domain spec language). Gradle not only supports multi-project builds, but it also supports dependencies like Ivy and Maven. Gradle also can support building non-Java projects.

Gradle is a declarative build language, meaning, you tell it what to build, not how to build. Gradle expresses intent, which makes is easily readable, understandable, and hence highly maintainable.

You have a few ways to get Gradle:

  1. Download from gradle.org.
  2. We also can use sdkman (Software Development Kit Manager) from http://sdkman.io (works on non-Windows only).
  3. Get it as part of Android Studio.

Gradle Basics

Gradle has a build file, build.gradle. The build file contains tasks, plugins, and dependencies.

Gradle Tasks

A task is code that Gradle executes. Each task has a lifecycle and properties. Tasks have ‘action,’ which is code that is going to execute. Task actions are broken into two parts: ‘first action’ and ‘last action.’

Task also have dependencies—one task can depend on the other. This allows specifying an order in which tasks are executed.

Gradle Build Phases

Gradle has the concept of build phases. There is an initialization phase that is used to configure multi-project builds. The configuration phase involves executing code in the task that is not the action. The execution phase involves actually executing the task.

Task Dependencies

To declare a simple dependency, you can use Task.dependsOn <TaskName>. Sometimes, you might need a more explicit declaration, such as mustRunAfter, where a task can run only after another task has run. Likewise, there also is support for shouldRunAfter where the execution order is not forced. finalizedBy is also supported.

Android Plugin for Gradle

Here is a sample Gradle build file for Android that shows that jcenter is used as the repository and a dependency on Gradle 1.5 library.

//build.gradle (project)

// Top-level build file where you can add configuration
// options common to all sub-projects/modules.

buildscript {
   repositories {
      jcenter()
   }
   dependencies {
      classpath 'com.android.tools.build:gradle:1.5.0'

      // NOTE: Do not place your application dependencies here;
      // they belong in the individual module build.gradle files.
   }
}

allprojects {
   repositories {
      jcenter()
   }
}

task clean(type: Delete) {
   delete rootProject.buildDir
}

The module Gradle build file supports configuring build settings, like compileSdkVersion, buildToolsVersion, default configuration, build types, and dependencies.

//build.gradle module

apply plugin: 'com.android.application'

android {
   compileSdkVersion 22
   buildToolsVersion "22.0.1"

   defaultConfig {
      applicationId "com.example.vipul.myapplication"
      minSdkVersion 15
      targetSdkVersion 22
      versionCode 1
      versionName "1.0"
   }
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile
            ('proguard-android.txt'),
            'proguard-rules.pro'
      }
   }
}

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   testCompile 'junit:junit:4.12'
   compile 'com.android.support:appcompat-v7:22.2.1'
   compile 'com.android.support:design:22.2.1'
}

Summary

In this article, you learned about Gradle, a popular build system that also is used for Android development. I hope you have found this information useful.

About the Author

Vipul Patel is a technology geek based in Seattle. He can be reached at vipul.patel@hotmail.com. You can visit his LinkedIn profile at https://www.linkedin.com/pub/vipul-patel/6/675/508.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories