Build Android with Gradle (Sourcesets)

Project Structure

source sets
├── source components(Java Plugin)
└── source components(Android Plugin)

source sets

  • src/main/
  • src/androidTest/

source components(Java Plugin)

  • java/
  • resources/

source components(Android Plugin)

  • AndroidManifest.xml
  • res/
  • assets/
  • aidl/
  • rs/
  • jni/

Project Structure 展開

src
├── main
│   ├── java/
│   ├── resources/
│   ├── res/
│   ├── assets/
│   ├── aidl/
│   ├── rs/
│   ├── jni/
│   └── AndroidManifest.xml
│
└── androidTest
    ├── java/
    ├── resources/
    ├── res/
    ├── assets/
    ├── aidl/
    ├── rs/
    └── jni/
    // src/androidTest/AndroidManifest.xml is created automatically

Configuring the Structure

Java 的設定方式
1
2
3
4
5
6
7
8
9
10
11
12
sourceSets {
main {
java {
srcDir 'src/java'
}
}
}

// 或者用下面的方式設定
sourceSets {
main.java.srcDirs = ['src/java']
}
Android 的設定方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Anroid plugin 有他自己的 sourceSets
android {
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}

// setRoot moves the whole sourceSet from src/androidTest/* to tests/*
androidTest.setRoot('tests')
}
}