This is a example project for android build using gradle build tool. This contain,
- How to set file dependencies and module dependencies (android modules, java modules) to the project.
- Demonstrate how to use gradle and maven repositories in the project and each module.
- What are the gradle tasks and how we can use them.
-
Download the gradle from here (for this sample project using gradle 1.12)
-
Extract the gradle.tar.gz file to your preferred location and set PATH to GRADLE_HOME
-
Clone the project
git clone https://kavi707@bitbucket.org/kavi707/android-gradle-build.git
-
Clean and build the project
./gradle clean
./gradle build
./gradlew clean
./gradlew build
In a project we can define three type of dependencies, Module dependencies, file dependencies and Library dependencies. In this sample project you can see there are separate modules as Android application module (app), Android library module (support) and Java library module (java-support). These three modules depended as follows
-
app module depended on support module - (app module build.gradle)
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile project(':support') }
compile project(':support')
part defines app module depend on support module
-
support module depended on java-support module - (support module build.gradle)
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile project(':java-support') }
compile project(':java-support')
part defines support module depend on java-support module
-
support module use gcm.jar as external file dependency (support module build.gradle)
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile project(':java-support') compile files('libs/gcm.jar') }
compile files('libs/gcm.jar')
part defines support module have gcm.jar external dependency and its located in module libs directory
-
java-support module use maven library dependency (java-support module build.gradle)
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile group:'org.apache.commons', name:'commons-math3', version:'3.2' }
compile group:'org.apache.commons', name:'commons-math3', version:'3.2'
part defines java-support module use this apche-commons-math library as dependency. This library dependency integration is discussed below with further details
In Gradle we can use maven repositories as for dependency downloading. In this sample project java-support module shows how we can do it.
-
In Maven
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-math3</artifactId> <version>3.2</version> </dependency>
-
In Gradle
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile group:'org.apache.commons', name:'commons-math3', version:'3.2' }
Above two code blocks show how we defines a dependency in maven and gradle. So you can clearly see that, we need to configure same parameters as follows
Maven | Gradle |
---|---|
groupId | group |
artifactId | name |
version | version |
In gradle have tasks to execute. gradle clean
, gradle build
, gradle installDebug
, etc ... are some pre-defined tasks in gradle. At the beginning we used clean
& build
tasks to build the sample project.
-
All available pre-defined tasks
gradle tasks
-
Build and install android app in device
gradle installDebug
Just copy the each following task and paste them into your gradle.build file. Then check how they are run and execute.
-
Simple task
task taskX << { println 'taskX' } to run above task: gradle -q taskX
-
Dependency tasks
task taskX << { println 'taskX' } task taskY << { println 'taskY' } taskX.dependsOn taskY gradle -q taskX
or
task taskY << { println 'taskY' } task taskX(dependsOn: [taskY]) << { println 'taskX' } gradle -q taskX