Build Android with Gradle (Dependency & Compile)

Dependencies, Android Libraries and Multi-project setup

Dependency Local & Remote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
repositories {
mavenCentral()
}

dependencies {
// Local packages
compile files('libs/foo.jar')

// Remote artifacts
compile 'com.google.guava:guava:11.0.2'
}

android {
...
}

Complile

  • compile: main application
  • testCompile: test application
  • debugCompile: debug Build Type
  • releaseCompile: release Build Type.
  • <buildtype>Compile: (Ex: alphaCompile, betaCompile)

Multi project setup

MyProject/
├── settings.gradle
├── build.gradle
├── app/
│   └── build.gradle
└── libraries/
    ├── lib1/
    │   └── build.gradle
    └── lib2/
        └── build.gradle
setting.gradle
1
2
3
4
// :app
// :libraries:lib1
// :libraries:lib2
include ':app', ':libraries:lib1', ':libraries:lib2'
app/build.gradle
1
2
3
4
5
6
// Use Java Project lib1 with jar
dependencies {
// 如果超過一個lib,那順序將會是很重要低
compile project(':libraries:lib1')
compile project(':libraries:lib2')
}

If you want to share code that accesses Android APIs or uses Android-style resources

these libraries cannot be regular Java project, they have to be Android Library Projects.

lib2/build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
buildscript {
repositories {
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:0.5.6'
}
}

apply plugin: 'android-library'

android {
compileSdkVersion 15
}