- 运用buildConfigField命令在BuildConfig中添加一个字段
buildConfigField "boolean", "LOG_ENABLE", "true";
- 运用manifestPlaceholders生成可在AndroidManifest.xml中使用的变量
manifestPlaceholders = [hostName:"www.example.com"]
- 压缩混淆代码
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- 压缩资源
shrinkResources true
- 多dex处理
multiDexEnabled true
multiDexKeepFile file('multidex-config.txt')
multiDexKeepProguard file('multidex-config.pro')
// 在application里重写attachBaseContext
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(context);
MultiDex.install(this);
}
- 单独只为某一个variants添加一些依赖,只需要在Compile加上对应的variant名字前缀
dependencies {
compile 'com.android.support:support-v4:22.2.0'
flavorOneCompile 'com.android.support:appcompat-v7:22.2.0'//只为flavorOne添加这个依赖
}
- flavorDimensions配置多维productFlavor
flavorDimensions "type", "environment"
- 编写variantFilter代码块过滤构建变体
flavorDimensions "api", "mode"
productFlavors {
demo {...}
full {...}
minApi24 {...}
minApi23 {...}
minApi21 {...}
}
variantFilter { variant ->
def names = variant.flavors*.name
// To check for a build type instead, use variant.buildType.name == "buildType"
if (names.contains("minApi21") && names.contains("demo")) {
// Gradle ignores any variants that satisfy the conditions above.
setIgnore(true)
}
}
- resValue动态配置文字
productFlavors {
demo {
resValue("string","dynamic_text","demo的动态文字")
}
}
注意:string.xml里面不能有dynamic_text字段
- sourceSets设置源集,给每个module配置不同的资源
sourceSets {
demo {
setRoot "src/demo"
res.srcDirs = ['src/demo/res', 'src/demo/res/']
}
}
写于2018-6-11
网友评论