Gradle 配置多渠道打包,替换桌面icon, app_name , 不同apk替换不同资源文件
很久没有写博客了,因为新入职公司很忙, 每天任务很多, 根本没时间整理,最近因为公司项目需要不同版本出不同版本的apk ,很多资源文件和桌面icon name也可能需要替换,所以去研究了一下gradle 配置,打包apk 这些文件自动替换, 这样就可以方便很多了.:查找了很多资料,都或多或少有些缺少,都没达到要求,最终自己整理一下, 以后就可以查找自己的博客,方便日后工作.
- Manifest 中; application配置
android:appComponentFactory="app"
android:icon="${app_icon}"
android:label="${app_label}"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning,HardcodedDebugMode"
tools:replace="android:name,android:icon,android:label,android:theme,android:appComponentFactory"
- gradle 配置
defaultConfig {
flavorDimensions "App"
}
buildTypes{
release{
// 批量打包
applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "${variant.productFlavors[0].name}_v${defaultConfig.versionName}_${releaseTime()}.apk"
}
}
}
}
// 多渠道/多环境 的不同配置
productFlavors {
master {
// applicationId "com.xxx.xxxxx" // 配置不同appid
// 动态添加 string.xml 字段;注意,这里是添加,在 string.xml 不能有这个字段,会重名!!!
//将colors 中home_back 删除,
// resValue "color", "home_back", "#0D1B35"
// 修改 AndroidManifest.xml 里渠道变量
manifestPlaceholders = [app_icon : "@mipmap/logo_main",
app_label: "@string/app_name"]
dimension "App"
}
normal {
// applicationId "com.xxx.xxxxxx"
// resValue "color", "home_back", "#0D1B35" // string 中不能有home_back 这个字段,否则报错
manifestPlaceholders = [app_icon : "@mipmap/logo",
app_label: "@string/app_name"]
dimension "App"
}
}
// 配置不同apk的获取资源目, 需要配置一下
sourceSets{
master.res.srcDirs=['src/main/res'] //指定编译的目录
normal.res.srcDirs=['src/main/res-normal']
}
这样就可以替换了