我想解决每个构建类型和风格的 manifestPlaceholders 依赖关系。例如,我有
productFlavors {
dev {
manifestPlaceholders = ['applicationLabel': 'DevFlavor']
}
prod {
manifestPlaceholders = ['applicationLabel': 'ProdFlavor']
}
.....
buildTypes {
debug {
def old_name = manifestPlaceholders.get('applicationLabel'); // every time is null
// def old_name = productFlavors.dev.manifestPlaceholders.get('applicationLabel'); // ITS OK, but not dynamic
manifestPlaceholders = [applicationLabel: old_name + ' Dev']
}
是否有添加'Dev'后缀来调试产品口味的解决方案?
感谢您的帮助
最佳答案
只是为我上面的评论举一个工作示例:
声明
resValue
在你的defaultConfig
这将成为应用程序的名称。 (注意:如果您选择将其命名为app_name
,请务必从您的app_name
文件中删除原始strings.xml
属性,否则会发生冲突。)defaultConfig { // applicationId, versionCode, etc. (...) // define your base Applications name here resValue 'string', 'app_name', 'MyApp' }
设置您的
productFlavors
就像你已经做的那样。如果您可以将您的应用程序名称仅与风格名称连接,或者提供明确的resValue
,您可以将它们留空。 ,这将覆盖defaultConfig
中的值.productFlavors { dev { // resValue 'string', 'app_name', 'MyAppDevFlavor' } prod { // resValue 'string', 'app_name', 'MyAppProdFlavor' } }
配置
resValue
gradle 配置时的名称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 name appName = "${appName}" variant.productFlavors.each { flavor -> appName += " ${flavor.name}" } // optionally add buildType name appName += " ${variant.buildType.name}" // your requirement: if buildType == debug, add DEV to App name if(variant.buildType.name == "debug") { appName += " DEV" } // set new resVale variant.resValue 'string', 'app_name', appName }
在您的 AndroidManifest 中,设置
app_name
领域:<application ... android:label="@string/app_name" ... >
正如我上面提到的,一定要删除默认的 app_name
来自 strings.xml
的属性(property)
关于java - 为每个构建类型解析应用程序标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32092665/