####Some tips'n tricks to build Android Eclipse projects using Gradle. http://tools.android.com/tech-docs/new-build-system/user-guide
http://www.alonsoruibal.com/my-gradle-tips-and-tricks/
http://www.vogella.com/tutorials/AndroidBuild/article.html
Using Gradle builds has many advantages. In our case we can change the package name of the app and get two builds of the same app with different configurations. i.e. If you have a demo and pro app you can just have the same project for both and release them in one build step. There are many more advantages like dependency management and other stuff, Google is your friend.
Almost all the info about Android and Gradle is related to Android Studio but there is many less info about using Gradle with Eclipse, this is what this guide is for.
If you want to use Gradle in your project you have to have, at least, a build.gradle file in the
root of your project and execute gradle build
to build it.
build.gradle is the file where you specify the tasks you want to execute at building time.
This is the minimal configuration.
There is an Eclipse plugin for Gradle, you can also run Gradle from the terminal, in Eclipse you have to install the plugin following the instructions from this URL:
During my tests I've found that Eclipse plugin works quite well but command-line works ok also. It's your choice. I ended up using command-line for testing and debugging and Eclipse plugin once our configuration is working to manage tasks (we'll see this later).
Another thing with Eclipse is that you have to have your Android SDK path set up, you can do it two ways:
-
Include a
local.properties
file in your project root folder with your sdk path:sdk.dir=/Users/user/Path/To/Android/sdk
-
Specify your Android SDK path globally: (this is the recommended way)
In OSX you can do it editing your
/etc/launchd.conf
file, add the line (requires reboot):setenv ANDROID_HOME /Users/user/Path/To/Android/sdk
You can now use Gradle with Eclipse, pick a project, right-click on it, Configure, Convert to Gradle Project,
It's handy to re-define your source folders gen and src in your java build path
Not all the versions of Gradle work with Android. At the moment 3-24-2014 the last version of gradle that works with Android is gradle 1.11.
http://tools.android.com/tech-docs/new-build-system
Android Studio (AS) uses Gradle as default build system but gradle files from AS doesn't fit our needs because AS projects are different than Eclipse's so we have to make some adjustments to use Gradle with Eclipse. (Specify the paths of our sources)
Example of a minimal gradle.build
file in Eclipse:
buildscript {
repositories { mavenCentral() }
dependencies { classpath 'com.android.tools.build:gradle:0.9.1' }
}
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 19
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
sourceSets
is where we specify our Eclipse Projects paths.
The rest is common in all Android builds.
This minimal configuration allows you to build a very simple Android project but we have to deal with more complicated stuff like dependencies, libraries and defining two (or more) versions of our app.
Dependencies can come in three ways:
-
A
.jar
file in your/libs
folder:The most obvious example is the compatibility library:
android-support-v4.jar
-
A library from a repository
If you use maps or another feature from google-play-services you have to use the play-services library, many other libraries are in repos ready to use. They're also called artifacts.
-
A library project
There are plenty of that ;)
######So, how to deal with dependencies?
You have to include a dependencies
section in your build.gradle file,
to compile .jar
and repository libraries just include it in your section:
dependencies {
compile files('libs/android-support-v4.jar')
compile 'com.google.android.gms:play-services:4.1.32'
}
Example of including ActionBarSherlock library with Gradle from mavencentral repository,
notice that repositories {mavenCentral()}
is called twice, inside buildscript{}
section
and outside, and the @aar
termination:
https://github.com/JakeWharton/ActionBarSherlock-Gradle-Sample/blob/master/build.gradle
######What about library projects?
Library projects are trickier but, long-story short, they have to be declared in a settings.gradle file in your root project and they have to have their own, working build.gradle file.
-
Create a file in your project root folder called settings.gradle and include the path to your library project, use relative paths so they can be used everywhere
include ":..:project:library"
-
Edit your dependencies section in your build.gradle and include the project
dependencies {
compile files('libs/android-support-v4.jar')
compile 'com.google.android.gms:play-services:4.1.32'
compile project(':..:project:library')
}
All the libraries have to have their working own build.gradle file with their own
dependencies and so on.
A library project build.gradle has to use android-library
plugin instead of android
This is a basic build.gradle file for an Android library project:
buildscript {
repositories { mavenCentral() }
dependencies { classpath 'com.android.tools.build:gradle:0.9.1' }
}
apply plugin: 'android-library'
dependencies {
...
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 19
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
Sometimes dependencies from a library may conflict with our project dependencies, this also happens in Ant. Different versions of a library...
####Build different versions of an app http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
If you want to create different versions of an app, lets say free and pro (or french and italian) you have to specify it in your build.gradle file, they are called productFlavors
buildscript {
repositories {
mavenCentral()
}
dependencies { classpath 'com.android.tools.build:gradle:0.9.1' }
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 19
}
productFlavors {
free {
packageName = 'com.app.test.free'
}
pro {
packageName = 'com.app.test.pro'
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
free {
res.srcDirs = ['free/res']
}
pro {
res.srcDirs = ['pro/res']
}
}
}
}
As you can see you can specify different sourceSets (another icon or whatever) using different paths in your project, use different package names... Check the user-guide. Link on top of the section.
####Gradle tasks Once your setup is done and your build completed you'll see in your Eclipse Gradle Tasks View a list of tasks you can do.
The usual ones will be installDebug while development and releaseCompile to release the app.
Every flavor has it's specific tasks to build and debug... you can also define your own tasks besides the default ones.
This is only the surface, please check the docs for more stuff like dealing with proguard update version number, and so on.
Some useful links:
http://stackoverflow.com/questions/18328730/how-to-create-a-release-signed-apk-file-using-gradle
http://tools.android.com/tech-docs/new-build-system/user-guide
http://www.alonsoruibal.com/my-gradle-tips-and-tricks/
You can also still use your old good Ant build, just Right Click on your project and Run As, Android Application
Another thing you may consider is to have your own local Maven Repository. Check the links!
It's also recommended to add this folders and file to the ignore file if you're using git or mercurial: