-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started with Gradle
Getting started with Gradle on the command-line is easy!
Download Gradle (see specific links below) and then extract the Gradle distribution to a folder, which we will call GRADLE_HOME. Add GRADLE_HOME/bin to your PATH environment variable.
Download Gradle 1.8 from the Gradle web site. At the moment, Android Studio v0.3.0 requires 1.8 now.
Download Gradle 1.6 from the Gradle web site. At the moment, only Gradle 1.6 works correctly with Eclipse. Using Gradle 1.7+ will fail with a cryptic error.
Define the ANDROID_HOME environment variable which points to your Android SDK.
// Unix
export ANDROID_HOME=~/android-sdks
// Windows
set ANDROID_HOME=C:\android-sdks
Afterwards, you have to configure the GRADLE_HOME environment variable on your path:
export GRADLE_HOME=/your_gradle_directory
export PATH=$PATH:$GRADLE_HOME/bin
In order for Gradle to work, ensure you have the API 17 SDK installed including the latest Android SDK Platform-tools and Android SDK Build-tools. Check this in the Android SDK Manager from within Eclipse.
Finally, you can check your working installation by running:
gradle -v
To use Gradle in your Android application, you have to create a build.gradle
file in your application root directory:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
android {
compileSdkVersion 17
buildToolsVersion '17'
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
instrumentTest {
java.srcDirs = ['tests/src']
res.srcDirs = ['tests/res']
assets.srcDirs = ['tests/assets']
resources.srcDirs = ['tests/src']
}
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
}
Note that with Gradle 1.6 as downloaded above, the SDK version must be specified as 17. Changing to 18 at this time may break the Gradle build.
To build the APK, run this command in the root directory:
gradle assemble
If a build occurs and you see the output:
BUILD SUCCESSFUL
Total time: 5.738 secs
Then Gradle is successfully set up for your project. If you get an error, try googling the error. Usually the issue is that you need to install the build tools or make sure to use Gradle 1.6.
If you have integration tests you want to run, make sure you have an open emulator or connected device and run this command in the root directory:
gradle build
This will build the apk, then automatically compile and run your integration tests.
To add dependencies to your project, you need to modify the build.gradle
file and add extra lines configuring the packages you require. For example, for certain Google or Android, dependencies look like:
android {
...
dependencies {
// Google Play Services
compile 'com.google.android.gms:play-services:4.0.30'
// Support Libraries
compile 'com.android.support:support-v4:19.0.0'
compile 'com.android.support:appcompat-v7:19.0.0'
compile 'com.android.support:gridlayout-v7:19.0.0'
compile 'com.android.support:support-v7-mediarouter:19.0.0'
compile 'com.android.support:support-v13:19.0.0'
// Note: these libraries require the "Google Repository" and "Android Repository"
// to be installed via the SDK manager.
}
}
You can also add dependencies based on the Maven Central Repository. The best tool for finding packages is actually the Gradle Please utility that takes care of helping you locate the correct package and version to add to your gradle file for any library:
Check out the following links for more details: