Is it possible to use the Gradle build system for Android with Eclipse?

I've an app that needs to be build multiple times with different resources for different customers (branding, configuration, and pre-loaded data change between customers).

In the wake of this year's Google I/O I've heard about the new Gradle-based Android build-system. So I thought, it would be a good idea, to realize this one-source/multiple-apks scenario using a Gradle build script.

Now here comes my question: How can I get started using Gradle for building while sticking to Eclipse? All the reads I found online point to converting the project to the still immature Android Studio. I'd like to put off migrating to Android Studio until that's declared "safe" for production use by Google.

Ideally, I'd like to be able to hook the build script(s) to the Debug and Run Configurations of Eclipse, very much the same way as I can pick and choose different build targets for debugging and archiving in XCode. If that's possible, what steps are required to make that work?

I apologize for the noobish quality of these questions, but for me this is actually quite an undiscovered country. Any help is appreciated.

Edit: Our team has migrated to Android Studio in late October 2013 and since version 0.4 were are encountering fewer and fewer bugs. If your organization is not super-conservative about adopting pre-1.0 environments for development, I'd encourage you to jump into the cold water and try working with Android Studio and its Gradle build system. The only important thing missing IMHO is decent support for unit-testing.


Solution 1:

It is possible to use 2 build systems (Eclipse + gradle based). Just make sure output folders are different (bin for ADT, build for gradle). (Update for TL;DR : check Nodeclipse/Enide Gradle for Eclipse (marketplace) )

File -> Export -> Generate Gradle build files will just add build.gradle with content below (but check versions). No existing files are changed.

com.android.tools.build:gradle version should be the latest. For gradle type gradle build as said in http://tools.android.com/tech-docs/new-build-system/user-guide. Try gradle tasks for more. (On my slow Internet connection it took 1 hour! for gradle to download all needed dependencies)

Vogella tutorial http://www.vogella.com/articles/AndroidBuild/article.html is not yet ready. Other online tutorials are not really finished http://www.jayway.com/2013/02/26/using-gradle-for-building-android-applications/

Eclipse ADT is not yet using gradle, I think it will be polished within Android Studio first. It would be not so wise to start using evolving technology in both IDEs at the same time.

See build.gradle example below. If you already mastered gradle, then maybe Wizards is not needed at all. For the latest build.gradle template for classic Android project check gh.c/N/n-1/b/m/o.n.e.e.g/docs/android/build.gradle.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 8
    buildToolsVersion "19.0.0"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

ADT-Bundle does not come with Eclipse Marketplace, so update site could be used.

Update p2 repository for Gradle Integration for Eclipse is

http://dist.springsource.com/release/TOOLS/gradle

But as of version 3.4.0 it does not provide Editor for .gradle files. So there is no sense of having it for Android development.

I would go with default ADT build, having gradle as secondary build for experimentations and keeping an eye when flow of bugs on http://tools.android.com/tech-docs/new-build-system becomes rare. (That should be around formal 1.0 version)

UPDATE: 2014-04-15

Alex Ruiz's (from Android team) Blog about Android, Gradle & ADT

Android’s Gradle Model

Instead of creating IDE-specific Android/Gradle models, we decided to have an IDE-agnostic representation of a Gradle project. This way, we have a single source of information that is easier to maintain. IDE integration will be implemented as a plug-in for each supported IDE (in our case, Eclipse and IDEA.) The main benefit of this approach is that we can release the Gradle plug-in independently of the IDE integration plug-ins. For example, we can ship a new version of the Eclipse plug-in that has several bug fixes, without affecting the Gradle side of Android.

As of April 2014 eclipse-gradle plugin is not compatible with android-gradle plugin:

As answered in Issue 57668 by Android team (raised by @arcone)

Project Member #2 [email protected]

The eclipse plugin is not compatible with the android plugin.

You will not be able to import an Android gradle project into Eclipse using the default Gradle support in Eclipse.

To make it work in Eclipse we will have to change the Gradle plugin for Eclipse, the same way we are modifying the Gradle support in IntelliJ

That is Android team is working on gradle plugin for IntelliJ and gradle plugin for Eclipse needs to be updated too.

There is effort at Nodeclipse to smooth the transition times. And continue to develop in Eclipse while still experimenting or fully using gradle.

Nodeclipse/Enide Gradle for Eclipse (marketplace)

Some screenshots for Gradle for Eclipse:

Solution 2:

  1. Install Gradle:
    a. http://gradle.org/gradle-download/ b. Choose 2.1 from previous releases section. c. Unzip at convenient folder.(Ex : D:\Graddle\gradle-2.1) d. Set system environment variables. i. GRADLE_HOME as D:\Graddle\gradle-2.1) ii. GRADLE_OPTS as -XX:MaxPermSize=512m iii. %GRADLE_HOME%\bin to Path iv. Set ANDROID_HOME ( Ex: D:\android-sdk) v. Append “%ANDROID %\platform-tools” to path.

e. Open command prompt and check gradle is set. May use gradle -version to check.

  1. Install Gradle eclipse PlugIn: a. Launch Eclipse b. Help > Eclipse Market Place c. Search “gradle” d. In that choose “Nodeeclipse/enide” e. Select all listed, accept & install. f. Restart eclipse once installed.

  2. Set Gradle & Java Homes : a. Launch eclipse. b. Window > Preferences > Gradle EnIDE c. Set these if not set : i. Gradle home to use is set ( Ex: D:\Graddle\gradle-2.1) ii. Alternate JAVA_HOME to use is set ( Ex : C:\Program Files (x86)\Java\jdk1.7.0_60) iii. JVM options for GRADLE_OPTS is set to “-XX:MaxPermSize=512m”

  3. Build the Project: a. Expand APK in eclipse Java explorer. b. Right click on build.gradle c. Run As > Gradle GUI d. Comand Line : gradle clean build e. Wait for build to complete : First time build will take several minutes. f. If Build dex error or Java heap space error : i. Open build.gradle in editor. ii. For multi dex builds- Set appropriate javaMaxHeapSize based on your java (javaMaxHeapSize=1024M for 32bit Java,2048M for 64bit Java) iii. May comment signing (//apply from: "$rootProject.projectDir/jenkins_config/gradle/signing.gradle";) to avoid signing for debug build. iv. Build again after these fixes.

  4. Install Build On device: a. Connect the device to m/c. b. Right click on build.gradle c. Run As > gradle installDebug Gradle Android start d. Wait for install to complete

  5. Debug Build: a. Launch the app b. Attach the debugger (DDMS>Devices > App). c. We able to debug on few devices checked.