Skip to content
jvoegele edited this page Sep 14, 2010 · 29 revisions

This is the Android plugin for the Gradle build system. This plugin enables the creation of Android applications using Gradle, with all of the power and flexibility you’ve come to expect from Gradle.

Features

Features of the Android plugin include:

  • Compile, package, and install Android applications. (Including handling of Android resource files.)
  • Sign application packages using the default debug key, or with a release key for publication to Android Market.
  • Incorporation of ProGuard to ensure that applications have minimal memory footprint.
  • Create Android applications in Scala (and possibly Groovy or Clojure).

The Android plugin fully integrates into the Gradle build lifecycle by extending the Java plugin. Furthermore, the incorporation of ProGuard into the build not only ensures that Android application packages are small and tight, it also trivially enables the use of Scala for Android application development simply by incorporating the existing Scala plugin into the build. ProGuard will include only those classes from the Scala library that are actually used by your Android application, resulting in an application package that is as small as possible.

Tasks and Lifecycle

The Android plugin adds the following tasks and dependencies to the build:

Task Description Depends On Dependents
androidProcessResources Generate R.java source file from Android resource XML files compileJava
proguard Process classes and JARs with ProGuard classes
androidPackageDebug Creates the Android application apk package, signed with debug key proguard assemble
androidPackageRelease Creates the Android application apk package, which must be signed before it is published proguard
androidInstall Installs the debug package onto a running emulator or device androidPackageDebug
androidUninstall Uninstalls the application from a running emulator or device

Usage

To use the Android plugin for Gradle you must first create the application skeleton using the android command-line tool. For example:

$ android create project --target 2 --path ./MyAndroidApp --activity MyAndroidActivity --package my.android.package

This will create and Android application skeleton that you can immediately build using Ant. To build with Gradle instead, you must (1) create a build.gradle file that includes the Android plugin, and (2) move the source code to the directory expected by Gradle.

1) Create a build.gradle file in the root directory of the project, and include the Android plugin as follows:

buildscript {
  repositories {
    mavenRepo(urls: 'http://jvoegele.com/maven2/')
  }
  dependencies {
    classpath 'com.jvoegele.gradle.plugins:android-plugin:0.8'
  }
}
usePlugin com.jvoegele.gradle.plugins.android.AndroidPlugin

2) The android create project command created the source code in the src directory of the project. The Android plugin tries to conform to the conventions established by Android’s Ant-based build, but in this case it must conform to Gradle’s “source sets” convention. Therefore, the source should be moved to src/main/java instead. Once you’ve done this you can, of course, utilize Gradle’s source sets to their full extent by placing resources in src/main/resources, Scala source files in src/main/scala etc.

If your Android project was initially created by Eclipse rather than the android create project command, then you will have some additional setup work to do. The Android plugin for Gradle must be told the location of the Android SDK. When you create a project with the android create project command, the location is filled in for you by the application generator, but the Eclipse project generator does not provide this information to the project. Therefore, you must fill it in yourself. To do this, create (or edit) the local.properties file in the root of the project and add the sdk.dir property, referring to the location of your Android SDK installation:

sdk.dir = /path/to/android/sdk

(Note that this file should not be checked in to your version control system as it will likely differ across various development environments.)

Once you’ve performed these steps you can build your Android application by invoking the tasks described above.

Limitations

  • In the current version of the Android plugin, the proguard task is incorporated into the build but is not very configurable. It can be disabled, however, by setting “proguard.enabled = false” in your build.gradle file.
  • The Android plugin does not currently handle .aidl files. I plan to address this in the next version.

Future Directions

  • In a future version of the Android plugin, I would like to integrate with the Eclipse plugin to ensure that Eclipse projects generated with “gradle eclipse” are optimized for Android development.
  • Update to Gradle 0.9
  • Make it easier to declare the plugin in the build.gradle file, ideally by simply saying usePlugin 'android' or apply plugin: 'android'
Clone this wiki locally