Android Gradle常用配置

编译加速

gradle.properties中配置

1
2
3
4
org.gradle.configureondemand=true #demand
org.gradle.daemon=true #单独的守护进程
org.gradle.parallel=true #串行编译
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 #内存设置

build.gradle中添加

1
2
3
4
5
6
7
8
9
10
lintOptions {
disable 'InvalidPackage', 'ContentDescription'
abortOnError false
}
dexOptions {
jumboMode = true
preDexLibraries = false
javaMaxHeapSize "4g"
}

只支持特定abi

abiFilters配置,可配合Product flavors使用。

1
2
3
ndk {
abiFilters "armeabi", "x86"
}

依赖库版本管理

在项目工程build.gradle中统一定义

1
2
3
4
5
6
7
8
9
10
ext {
// Sdk and tools
minSdkVersion = 10
targetSdkVersion = 22
compileSdkVersion = 23
buildToolsVersion = '23.0.2'
// App dependencies
supportVersion = '23.4.0'
}

在模块工程build.gradle使用

1
2
3
4
5
6
7
8
9
10
11
12
13
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}
dependencies {
// App's dependencies, including test
compile "com.android.support:appcompat-v7:$rootProject.supportVersion"
compile "com.android.support:cardview-v7:$rootProject.supportVersion"
compile "com.android.support:design:$rootProject.supportVersion"
compile "com.android.support:recyclerview-v7:$rootProject.supportVersion"
compile "com.android.support:support-v4:$rootProject.supportVersion"
}

依赖库多版本解决

1
2
3
configurations.all {
resolutionStrategy.force "com.android.support:support-v4:$rootProject.supportVersion"
}

签名信息

项目中添加sign.properties

1
2
3
4
keystore=[path to]\release.keystore
keystore.password=*********
keyAlias=***********
keyPassword=********

build.gradle读取文件并设置签名信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(new File(project.property("sign.properties")).exists()) {
Properties props = new Properties()
props.load(new FileInputStream(file(project.property("sign.properties"))))
signingConfigs {
release {
storeFile file(props['keystore'])
storePassword props['keystore.password']
keyAlias props['keyAlias']
keyPassword props['keyPassword']
}
}
}