23

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.

Chris
  • 3,192
  • 4
  • 30
  • 43
  • 1
    AFAIK, at the present time there's no official support for Gradle-based builds from Eclipse, other than doing the builds yourself at the command line. Eclipse/Gradle project support will arrive eventually. – CommonsWare Jun 14 '13 at 11:26
  • Thank you, that makes me feel less incompetent already :-) – Chris Jun 14 '13 at 11:30
  • However, in all fairness, I have no idea if there's an unofficial/unsupported way of accomplishing your objective... which is why I'm posting these as comments. :-) – CommonsWare Jun 14 '13 at 11:33
  • Studio is far better than Eclipse , I don't know why do you want to stick with Eclipse. I am suggesting you to give a try to Android Studio , you will definitely like it. – Piyush Agarwal Oct 05 '13 at 08:58
  • Having used Android Studio for productive development for three months now, I must say it is not for the faint-hearted. Unit test support is almost non-existent, build times are through the roof, DDSM isn't properly integrated, and dependency management sometimes seems like black magic (like figuring out you sometimes need to restart the IDE to make Gradle run on a perfectly good build script). However, you're right, I personally like it most of the time :-). – Chris Oct 07 '13 at 06:39
  • @CommonsWare any update to this as far as you know yet? Can Eclipse build using the new Gradle system yet? – spierce7 Nov 14 '13 at 16:55
  • 1
    @spierce7: Nothing yet AFAICT. I will be somewhat surprised if it is available before Android Studio ships, just because they only have so many engineers. Note that Eclipse and a current ADT *can* export a Gradle build file describing the project, but that's it. – CommonsWare Nov 14 '13 at 17:35
  • 1
    The answer is updated. – Paul Verest Apr 15 '14 at 08:58

2 Answers2

24

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 x...@android.com

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:

Community
  • 1
  • 1
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • 2
    Wow, thanks for updating the answer. In the meantime however, our team has completely migrated over to Android Studio and we are quite happy. I figure this answer will be very helpful for any other devs who are looking into the Android build system conundrum. Thanks again! – Chris Apr 16 '14 at 09:35
  • 1
    Time solves everything, but sometimes rushing to newer software will be spending more time than expected. As of April 2014 gradle based new build system is 0.9 and seems coming to final 1.0 release. Time to more widely adopt is coming. – Paul Verest Apr 16 '14 at 12:33
  • 1
    The Google team seem to be totally neglecting adding support for building Android apps with gradle in eclipse. – Martin Apr 19 '14 at 21:42
1
  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.

rajkabbur
  • 187
  • 3