開啟多版本方式:在app下的build.gradle添加如下設(shè)置
android {
sourceSets {
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/resources']
aidl.srcDirs = ['src/main/aidl']
renderscript.srcDirs = ['src/maom']
res.srcDirs = ['src/main/res']
assets.srcDirs = ['src/main/assets']
jniLibs.srcDir 'src/main/jniLibs'
}
//用各自對應(yīng)的資源文件路徑
newapp.res.srcDirs = ['src/main/res-newapp']
xqapp.res.srcDirs = ['src/main/res-xqapp']
// Move the tests to tests/java, tests/res, etc...
// instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
// debug.setRoot('build-types/debug')
// release.setRoot('build-types/release')
}
flavorDimensions "default"
productFlavors {
defaultapp {
manifestPlaceholders = [channel: "defaultapp", app_name: "默認app"]
resValue("string", "strKey", "defaultapp")
}
newapp {
manifestPlaceholders = [channel: "newapp", app_name: "新版app1"]
resValue("string", "strKey", "newapp")
}
xqapp {
manifestPlaceholders = [channel: "xqapp", app_name: "新版app2"]
resValue("string", "strKey", "xqapp")
}
}
}
調(diào)試切換在Android Stuido 左下角例:main下的資源是公用的
各自對應(yīng)的資源文件路徑如src/main/res-newapp
只需添加newapp需要的資源。
若存在同名資源柱嫌,各版本優(yōu)先使用對應(yīng)設(shè)置路徑下的資源鼻疮,類似覆蓋操作
如src/main/res-newapp/drawable
和src/main/res/drawable
都存在logo.png
則在newapp版本優(yōu)先使用src/mainres-newapp/drawable
下的logo.png
切換defaultapp版本則使用的是src/main/res/drawable
下的logo.png
image.png