Build Overview‎ > ‎

Building the Android Gradle Plugin

This page describes how to build the Android Gradle plugin, and to test it.

Get the Source Code

Follow the instructions here to get checkout the source code.

Once you have checked out the source code, the Gradle Plugin code can be found under tools/base.git

Building the plugin

All of the projects are build together in a multi-module Gradle project setup. The root of that project is tools/

The Gradle Plugin is currently built with Gradle 2.2. To ensure you are using the right version, please use the gradle wrapper scripts (gradlew) at the root of the project to build (more Gradle wrapper info here)

To prepare the build environment:
$ cd tools
$ ./gradlew init

This will populate a local repo with dependencies that are normally found in MavenCentral.
This will also copy your global gradle.properties (~/.gradle/gradle.properties) in the out/ folder since we redirect ~/.gradle to out/.gradle.

Finally you can build the Gradle plugin (and associated libraries) with

$ ./gradlew assemble

If assemble fails the first time you execute it, try the following

$ ./gradlew clean assemble

To test the plugin, you need to
  • Export a environment variable named ANDROID_HOME and point it to your SDK
  • Install all necessary package from the SDK manager (see appendix for all the package required)
  • Export a environment variable named ANDROID_NDK_HOME and point it to your NDK
  • Finally run the following command
$ ./gradlew check

If your tests fail because it cannot find all the necessary dependencies, execute publishLocal first (see below.)

Additionally, you should connect a device to your workstation and run:
$ ./gradlew connectedIntegrationTest

To run a specific connectedIntegrationTest, run:
$ ./gradlew connectedIntegrationTest -DconnectedIntegrationTest.single=BasicTest

Editing the plugin

The code of the plugin and its dependencies is located in tools/base. You can open this project with IntelliJ as there is already a tools/base/.idea setup.

tools/base/build-system/integration-test/sample contain a number of sample projects and tests for the plugin.  To run the integration tests. run:
$ ./gradlew :base:integration-test:test

To run just a single test, you can use the --tests argument with the test class you want to run.  e.g.:
$ ./gradlew :b:int:test --tests *.BasicTest

or use the system property flag (see Gradle docs for the difference: link, link):
$ ./gradlew :b:int:test -D:base:integration-test:test.single=BasicTest

To compile the samples manually, publish the plugin and its libraries first with
$ ./gradlew publishLocal
(Tip: you can use camelcase prefixes for target names, so for the above you can just run gradlew pL).
Then set the CUSTOM_REPO environment variable to the repository in the "out" folder inside the directory where you checked out the tools.   For example, if you ran the repo init command above in /my/aosp/work, then the repository will be in /my/aosp/work/out/repo.
(Also, running check, :base:integration-test:test, and connectedIntegrationTest does publishLocal first).

Debugging
For debugging  unit tests, you can use the following:
$ ./gradlew :base:gradle:test --debug-jvm --tests='*.BasicTest'

For debugging integration tests code (not the Gradle code being executed as part of the test):
$ ./gradlew :b:int:test --debug-jvm -D:base:integration-test:test.single=BasicTest

For debugging plugin code when run locally:
$ cd a-sample-project  # Make sure build.gradle points at your local repo, as described below.
$ ./gradlew --no-daemon -Dorg.gradle.debug=true someTask

If you need to debug an integration test while running within the integration tests framework, you can do : 
$ DEBUG_INNER_TEST=1 ./gradlew :b:int:test -D:base:integration-test:test.single=ShrinkTest # to run and debug only one test. --tests should also work.


This will silently wait for you to connect a debugger on port 5006. You can combine this with --debug-jvm flag (which expects a debugger on port 5005) to debug both the sides of the tooling API at the same time.

Using locally built plugin

To test your own Gradle projects, using your modified Android Gradle plugin, modify the build.gradle file to point to your local repository (where the above publishLocal target installed your build).

In other words, assuming your build.gradle contains something like this:

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

You need to point to your own repository instead. For example, if you ran the repo init command above in /my/aosp/work, then the repository will be in

buildscript {
    repositories {
        maven { url '/my/aosp/work/out/repo' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
    }
}

If you've made changes, make sure you run the tests to ensure you haven't broken anything:
./gradlew pL base:gradle:test base:gradle-model:test base:integration-test:test

Appendix
SDK Dependencies
The tests requires a number of packages in the SDK.  To run all tests: 

- Install the android SDK from here: https://developer.android.com/sdk/index.html#Other

- Execute tools/android

- Click Deselect all and check Obsolete (at the bottom)

- Select (not including the italic headings)…


Tools

Android SDK Build-tools 22.0.1

Android SDK Build-tools 21.1.2

Android SDK Build-tools (Obselete) 21.1.1

Android SDK Build-tools (Obselete) 21.1

Android SDK Build-tools (Obselete) 21

Android SDK Build-tools (Obselete) 20

Android SDK Build-tools 19.1

Android 5.1.1 (API 22)

SDK Platform 22

Android 5.0.1 (API 21)

SDK Platform 21

Android 4.4.2 (API 19)

SDK Platform 19

Android 4.0.3 (API 15)

SDK Platform 15

Extras

Android Support Repository 12

Android Support Library 22

Google Play Services 23

Google Repository 16

Alternatively, you can execute this command to install the needed dependencies automatically:

$ android update sdk -u -a -t build-tools-22.0.1,build-tools-21.1.2,build-tools-21.1.1,build-tools-21.1.0,build-tools-21.0.0,build-tools-19.1.0,android-22,android-21,android-19,android-15,extra-android-m2repository,extra-android-support,extra-google-google_play_services,extra-google-m2repository,build-tools-25.0.0

Comments