How to change appname or app icon for different flavor version?

来源:互联网 发布:尼康处理raw软件 编辑:程序博客网 时间:2024/06/06 12:39

1. resValue in defaultConfig

declare a resValue in your defaultConfig which will become the Application’s name. (Attention: if you choose to name it app_name, be sure to delete the original app_name property from your strings.xml file, or it will clash.)
defaultConfig {
// applicationId, versionCode, etc.

(...)// define your base Applications name hereresValue 'string', 'app_name', 'MyApp'

}

2.resValue in productFlavors

set your productFlavors as you did already. You can leave them empty if it is ok for you to concat your App’s name with the flavor’s name only, or provide an explicit resValue, which will override the value from defaultConfig.
productFlavors {
dev {
// resValue ‘string’, ‘app_name’, ‘MyAppDevFlavor’
}

prod {    // resValue 'string', 'app_name', 'MyAppProdFlavor'}

}

3. configure the resValue’s name at gradle configuration time

configure the resValue’s name at gradle configuration time
android.applicationVariants.all { variant ->
// get app_name field from defaultConfig
def appName = variant.mergedFlavor.resValues.get(‘app_name’).getValue()

// concat new App name with each flavor's nameappName = "${appName}"variant.productFlavors.each { flavor ->    appName += " ${flavor.name}"}// optionally add buildType nameappName += " ${variant.buildType.name}"// your requirement: if buildType == debug, add DEV to App nameif(variant.buildType.name == "debug") {    appName += " DEV"}// set new resValevariant.resValue 'string', 'app_name', appName

}

4.In your AndroidManifest, set the app_name field:

5. use manefestPlaceholders

5.http://stackoverflow.com/questions/24785270/how-to-change-app-name-per-gradle-build-type

android {
buildTypes {
release {
manifestPlaceholders = [appName: “My Standard App Name”]
}
debug {
manifestPlaceholders = [appName: “Debug”]
}
}
}
Then in your AndroidManifest.xml put:

6. user flavor res folder

Make sure you created the folders in the right path.

It should be app/src/flavor1/res.

To make more sense your main resources are located in src/main/res.

这里写图片描述

7.总结

三种方式:

1.manifestPlaceholders
2. resValue
3. 文件 res

NOTE:

1.buildTypes和productFlavors 中均可以使用前两种方式,且第二种要好,因为第一种方式是直接替换不利于国际化资源串引用。
2.如果两者都声明了app_name,则最后以buildTypes的设定为准。
原因是Gradle的资源合并功能中在权重上:

BuildType > Flavor > main > Dependecies.

8.参考链接

http://stackoverflow.com/questions/26555975/using-alternate-resources-with-build-flavors-in-gradle

http://stackoverflow.com/questions/16737006/using-build-flavors-structuring-source-folders-and-build-gradle-correctly
http://sodino.com/2016/09/01/android-gradle-change-appName/

0 0
原创粉丝点击