android多版本打包,差异化
// An highlighted block
defaultConfig {
applicationId "xxxxxx"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0.1"
flavorDimensions "versionCode" //同一纬度
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
//“app” 名称随便
flavorDimensions 'app' //同一纬度
productFlavors {
//test1写你对应的新建的包名 .xxx写你对应的成功的包名 会在默认的application name后面加上 .xxx
test1{
applicationIdSuffix ".test1"
//versionNameSuffix "-meiye" //更改versionnamer 会在 1.0.1 后面加“” -xxx“”
dimension "app" //同一纬度
}
test2{
applicationIdSuffix ".test2"
//versionNameSuffix "-zuyu"
dimension "app" //同一纬度
}
}
//打包生成对应聚到版本名称 xxxx写入自己的名字
android.applicationVariants.all { variant ->
variant.outputs.all { output ->// each 改为 all
def fileName = "XXXX_${variant.productFlavors[0].name}_release.apk"
def outFile = output.outputFile
if (outFile != null && outFile.name.endsWith('.apk')) {
outputFileName = fileName // output.outputFile 改为 outputFileName
}
}
}
对应包的创建
在src中新建如上面我们新建两个包名 test1 和 test2 目录下的结构和src/main下一致,
如需修改app_name 比如放在string.xml中app_name 字段, 我们分辨在test1 和test2中分别创建res/vlaues/strings.xml中 分辨加入app_name 放入不同的值 。其它资源同理
示例图