Gradle is a new build system that Android is currently promoting. It can be used to build Android project by adding a Gradle Android plugin. It is also possible that Android would move to Gradle-only build system, ditching the old ant-way to build things. The Android Studio only supports Gradle projects.
Quickstart
To join the new family, we need to write a new build.gradle file in project root. An general example of build.gradle would look like the snippet below. Please note that the system is still in active development, version numbers could change very soon. I’m using Gradle 1.7, plugin 0.5.6, Android Build Tools 17 and my target SDK version is 15.
This simple gradle file should be able to build most of standard Android projects. But what if I have special need? I’m going to talk about how to add support for AndroidAnnotation and token replacement below.
Android Annotation
AndroidAnnotation is a set of very useful annotations that you could use in your code to make your code shorter and more easier to read. The framework utilize a annotation processor to scan through your source code file and generate helper classes. To integrate it, we need to do 2 things:
- Add processor flags to Java compiler
- Add androidannotation-api dependency
Manipulating Compile Task
To add extra flags to compiler, we need to use the interface Android Gradle plugin exposed to access relevant tasks. The build plugin organize tasks into different “variants”, some basic variants includes debug and release. To iterate through all variants, we could use android.applicationVariants.all
.
The configurations.apt
is missing at the moment. We used the variable to store path to Android Annotation processor and we use Gradle’s dependencies managing feature to resolve the path for us.
Add dependencies
For managing dependencies, Gradle uses a dependencies
block. The following code snippets shows different ways to include dependencies. fileTree
can be used to iterate files in directory, string
will be resolved using repositories(default includes Maven Central) and project
is used to reference dependent local projects. We will talk about multi-project build support later.
Say if you put the Android Annotation API jar into libs directory, the build system should picked it up by now.
Token Replacement
Sometimes we would like to have a way to reference build version, git revisions from Java code. To accomplish this, we need to collect those informations and put it in a Java source file which will be picked out later during compilation.
Let’s say if you have a AppBuild.java looks like below and we put it in compile-libs
directory.
Note the @git-rev@
is the token we will replace with current git revision number each time we build the project. Now we have the template, we need to rig it into the build system. The way I’m doing this is by adding a generation task per variant and setup the dependency for compiling task to be depend on this generation task.
What this code snippets do is to initiate a copy task by the name generateAppBuild${variant}
. Note the outputs.upToDateWhen line would cause the task to always run, otherwise it’ll be skipped if the source file is not changed, which is not what we want. You’ll also need to import ReplaceTokens filter from our good old friend, ant
.
Multi-Projects
It is very common that you might have some sub-projects that needs to be build together. Previously, I showed that you could have compile project(':path:to:project')
in dependencies
block to have it being included during compile time. However, you need some extra setup to make it work. You need an extra file called settings.gradle
with those lines.
This indicates you have two sub-projects, they’re located at ${project.root}/extra/actionbarsherlock
and ${project.root}/extra/ViewPagerIndicator
. Gradle will look for build.gradle inside those directories and set up project dependencies automatically.
Conclusion
In this article, I showed how to migrate your android project to utilize Gradle as the build system. Additionally, I also showed a way to integrate Android Annotations and build information generation. I hope these would be enough to get your started with Gradle. Here is the final build.gradle you should have after all these extra codes.