Android Studio 基础篇四 BuildTypes

BuildTypes

默认情况下,Android Plugin会自动给项目设置同时构建应用程序的debug和release版本。当然,开发者也可以根据具体需求自定义新的BuildType。

1
2
3
4
5
buildTypes {
customBuildType {
applicationIdSuffix ".custom"
}
}

在上面例子中,自定义一个BuildType,名为customBuildType。其为applicationId添加后缀”.custom”。

下面我们一起看看GradleDemo项目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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.0"

defaultConfig {
applicationId "cn.spade.android.gradle"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {

debug{
applicationIdSuffix ".debug"
}

release {
debuggable false
jniDebuggable false
renderscriptDebuggable false
renderscriptOptimLevel 4
versionNameSuffix ".release"
applicationIdSuffix ".release"
minifyEnabled false
zipAlignEnabled true
testCoverageEnabled true
pseudoLocalesEnabled true
embedMicroApp true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}

anotherDebug.initWith(buildTypes.debug)
anotherDebug{
applicationIdSuffix = ".another"
}

customBuildType {
applicationIdSuffix ".custom"
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
}

在build.gradle中,我们新增anotherDebug和customBuildType两种BuildType,同时在原有的debug和release上添加了很多自定义配置。

  1. debug与customBuildType比较简单,只是在defaultConfig.applicationId的基础上添加不同的后缀,以作区分。
  2. anotherDebug通过initWith初始化,在debug上进行扩展,重新定义applicationIdSuffix,其他属性保持与debug一致。
  3. 在release中相对复杂,配置很多BuildTyope支持的配置。

BuildType支持配置:

配置名称 作用
debuggable Whether this build type should generate a debuggable apk.
jniDebuggable Whether this build type is configured to generate an APK with debuggable native code.
renderscriptDebuggable Whether the build type is configured to generate an apk with debuggable RenderScript code.
renderscriptOptimLevel Optimization level to use by the renderscript compiler
versionNameSuffix Version name suffix.
applicationIdSuffix Application id suffix applied to this build type.
minifyEnabled Whether Minify is enabled for this build type.
zipAlignEnabled Whether zipalign is enabled for this build type.
testCoverageEnabled Whether test coverage is enabled for this build type.
pseudoLocalesEnabled Whether to generate pseudo locale in the APK.
embedMicroApp Whether a linked Android Wear app should be embedded in variant using this build type.
proguardFiles Adds new ProGuard configuration files.

在这里我们先初步熟悉下BuildType的基本配置项,在今后的项目实践中不断的深入学习,只有这样才能真正的理解和应用。

参考资料:

  1. com.android.build.gradle
  2. com.android.builder