一)基本配置
build配置
1
2
3
4
5
6
7
8
|
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
} |
Android脚本
1
|
apply plugin: 'com.android.application' |
Android配置
1
2
3
4
|
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
} |
项目结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
MyApp
├── build.gradle
├── settings.gradle
└── app
├── build.gradle
├── build
├── libs
└── src
└── main
├── java
│ └── com.package.myapp
└── res
├── drawable
├── layout
└── etc. |
- Gradle Wrapper结构(这些新建项目时都添加给了用户,不需要重新添加)
1
2
3
4
5
6
|
myapp/
├── gradlew
├── gradlew.bat
└── gradle/wrapper/
├── gradle-wrapper.jar
└── gradle-wrapper.properties |
运行build任务 - 列出所有可用任务
生成App-debug.apk任务
1
2
3
|
$ ./gradlew assembleDebug
# Apk路径: MyApp/app/build/ outputs/apk |
- 手动导入Eclipse-Android项目(自动导入请连续点“下一步”)
在项目路径下创建build.gradle文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
androidTest.setRoot('tests')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
} |
PS 也可以复制粘贴Eclipse-Android项目的源代码到Android Studio的项目里
二)自定义配置
- Gradle所有文件结构
1
2
3
4
5
|
MyApp
├── build.gradle
├── settings.gradle
└── app
└── build.gradle |
settings.gradle
MyApp/build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
|
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
allprojects {
repositories {
jcenter()
}
} |
MyApp/app/build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.gradleforandroid.gettingstarted"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
} |
基础任务
1
2
3
4
5
6
7
8
9
|
$ ./gradlew assemble -为所有构建类型创建apk
$ ./gradlew check 运行所有的检查,比如说Android Lint,如果发现问题可终止任务
$ ./gradlew build 运行以上两个任务
$ ./gradlew clean -清除生成的apk
++++
$ ./gradlew connectedCheck - 在设备上运行测试
$ ./gradlew deviceCheck - 远程设备运行测试
$ ./gradlew installDebug/installRelease - 在设备商安装指定版本
$ ./gradlew uninstall - 卸载 |
三) 依赖管理
仓库
预设配置仓库
1
2
3
4
5
|
repositories {
mavenCentral()
jcenter()
mavenLocal()
} |
远程仓库
1
2
3
4
5
6
7
8
9
10
11
12
|
repositories {
maven {
url "http://repo.acmecorp.com/maven2"
credentials {
username 'user'
password 'secretpassword'
}
}
ivy {
url "http://repo.acmecorp.com/repo"
}
} |
本地仓库
1
2
3
4
5
|
repositories {
maven {
url "../repo"
}
} |
本地依赖
项目文件依赖
1
2
3
|
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
} |
原生库结构与配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 结构:
app
├── AndroidManifest.xml
└── jniLibs
├── armeabi
│ └── nativelib.so
├── armeabi-v7a
│ └── nativelib.so
├── mips
│ └── nativelib.so
└── x86
└── nativelib.so
# 配置:
android {
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
}
} |
Libray项目
1
2
3
4
5
6
7
8
|
# 修改Android插件:
apply plugin: 'com.android.library'
# settings.gradle新增libray项目:
include ':app', ':library'
# app内引用library项目:
dependencies {
compile project(':library')
} |
.aar文件
1
2
3
4
5
6
7
8
9
10
|
# 生成arr
repositories {
flatDir {
dirs 'aars'
}
}
# 使用aar
dependencies {
compile(name:'libraryname', ext:'aar')
} |
依赖概念
1
2
3
4
5
6
|
# 动态版本
dependencies {
compile 'com.android.support:support-v4:22.2.+'
compile 'com.android.support:appcompat-v7:22.2+'
compile 'com.android.support:recyclerview-v7:+'
} |
- Android Studio内添加依赖
四)构建变体
构建类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
android {
buildTypes {
# release类型
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
# staging类型 复制debug类型
staging.initWith(buildTypes.debug)
staging {
applicationIdSuffix ".staging"
versionNameSuffix "-staging"
buildConfigField "String", "API_URL", "\"http://staging.example.com/api\""
}
}
} |
产品格局
1
2
3
4
5
6
7
8
9
10
11
12
13
|
android {
productFlavors {
red {
applicationId 'com.gradleforandroid.red'
versionCode 3
}
blue {
applicationId 'com.gradleforandroid.blue'
minSdkVersion 14
versionCode 4
}
}
} |
构建变体
签名配置
1
2
3
4
5
6
7
8
9
10
11
|
android {
signingConfigs {
staging.initWith(signingConfigs.debug)
release {
storeFile file("release.keystore")
storePassword"secretpassword"
keyAlias "gradleforandroid"
keyPassword "secretpassword"
}
}
} |
五)多模块构建管理
加速构建
1
2
|
在gradle.properties里面添加:
org.gradle.parallel=true |
六) 测试
单元测试
使用JUnit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# 结构:
app
└─── src
├─── main
│ ├─── java
│ │ └─── com.example.app
│ └───res
└─── test
└─── java
└─── com.example.app
# 依赖:
dependencies {
testCompile 'junit:junit:4.12'
} |
使用Robolectric
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# 依赖:
apply plugin: 'org.robolectric'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
testCompile 'junit:junit:4.12'
testCompile'org.robolectric:robolectric:3.0'
testCompile'org.robolectric:shadows-support:3.0'
}
# Demo:
@RunWith(RobolectricTestRunner.class)
@Config(manifest = "app/src/main/AndroidManifest.xml", sdk = 18)
public class MainActivityTest {
@Test
public void clickingButtonShouldChangeText() {
AppCompatActivity activity = Robolectric.buildActivity(MainActivity.class).create().get();
Button button = (Button) activity.findViewById(R.id.button);
TextView textView = (TextView) activity.findViewById(R.id.label);
button.performClick();
assertThat(textView.getText().toString(), equalTo(activity.getString(R.string.hello_robolectric)));
}
} |
功能测试
使用Espresso
测试覆盖度
七)创建任务与插件
八)配置CI
九)自定义配置 - 进阶
缩减apk文件大小
使用ProGuard
1
2
3
4
5
6
7
8
9
|
android {
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
} |
收缩资源文件 - 自动 (<手动待续>)
1
2
3
4
5
6
7
8
|
android {
buildTypes {
release {
minifyEnabled true
shrinkResources true
}
}
} |
加速构建
1
2
3
|
org.gradle.parallel=true # 并行构建
org.gradle.daemon=true # 开启Gradle守护进程
org.gradle.jvmargs=-Xms256m -Xmx1024m # 配置JVM<参照下图> |
使用Profiling
使用Jack(Java Android Compiler Kit) and Jill(Jack Intermediate Library Linker)
忽略Lint
1
2
3
4
5
|
android {
lintOptions {
abortOnError false
}
} |
使用Ant
app打包 - 进阶
分割apk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
android {
splits {
density {
enable true
exclude 'ldpi', 'mdpi'
compatibleScreens 'normal', 'large', 'xlarge'
}
}
}
生成结果:
app-hdpi-release.apk
app-universal-release.apk
app-xhdpi-release.apk
app-xxhdpi-release.apk
app-xxxhdpi-release.apk |
References:
- 《Gradle for Android》