1 创建flavor
在module的build.gradle的defaultConfig同级目录中添加flavors集合
productFlavors {
}
2 flavors集合中添加flavor
添加flavor
productFlavors {
flavor_test {
applicationIdSuffix ".test"
versionCode 1
versionName "1.0"
minSdkVersion 23
buildConfigField 'boolean', 'IS_FREE', "true" // 可以在代码中通过BuildConfig.IS_FREE使用
buildConfigField 'boolean', 'IS_TEST', "true"
manifestPlaceholders = [ appLabel: "Flavor(test)"]
}
}
3 在该module中添加该flavor对应的文件夹, 跟该flavor名称一致(flavor_test)
4 通过Build Variant窗口切换flavor
5 完整的 productFlavors
工程可以通过上述 ProductFlavorSample 修改 productFlavors得到, 修改后的productFlavors为:
productFlavors {
free {
applicationIdSuffix ".free"
versionCode 1
versionName "1.0.0"
buildConfigField 'boolean', 'IS_FREE', "true"
buildConfigField 'boolean', 'IS_TEST', "false"
manifestPlaceholders = [ appLabel: "Flavor(Free)" ]
}
paid {
applicationIdSuffix ".paid"
versionCode 2
versionName "1.0.1"
buildConfigField 'boolean', 'IS_FREE', "false"
buildConfigField 'boolean', 'IS_TEST', "false"
manifestPlaceholders = [ appLabel: "Flavor(Paid)" ]
}
flavor_test {
applicationIdSuffix ".test"
versionCode 1
versionName "1.0"
minSdkVersion 23
buildConfigField 'boolean', 'IS_FREE', "true"
buildConfigField 'boolean', 'IS_TEST', "true"
manifestPlaceholders = [ appLabel: "Flavor(test)"]
}
}