Android开发在构建apk时,AS会根据指定的编译变体选择指定的代码、资源和配置项进行apk构建,编译变体由两部分组成:
-
编译类型–BuildType
-
产品特性–ProductFlavor
编译变体由不同的ProductFlavor和BuildType组合而成
BuildVariant=ProductFlavor*BuildType
1. 编译类型–BuildType
在app module的build.gradle文件中,BuildType通过buildTypes{}块配置,默认情况下,AS自动生成了debug和releae两种buildType,还可以根据不同的需要修改或新增不同的buildType
android {
defaultConfig {
manifestPlaceholders = [hostName:"www.example.com"]
...
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix ".debug"
debuggable true
}
/**
* The `initWith` property allows you to copy configurations from other build types,
* then configure only the settings you want to change. This one copies the debug build
* type, and then changes the manifest placeholder and application ID.
*/
staging {
initWith debug
manifestPlaceholders = [hostName:"internal.example.com"]
applicationIdSuffix ".debugStaging"
}
}
}
上面的例子就定义了3中buildType,分别是debug、release和staging,各个buildType内可以配置applicationId前缀()、是否开启混淆(minifyEnabled)、是否开启mutildex(multiDexEnabled)、配置Proguard规则、设置不同的签名(signingConfig)等,具体可配置的选项如下
具体查看