问题描述
resValue 方法(或其他任何名称)允许您在 buildTypes 或 productFlavors设置资源值>.有没有相应的方法获取由resValue设置的资源值?
The resValue method (or whatever it's called) allows you to set a resource value in buildTypes or productFlavors. Is there a corresponding way to get a resource value that was set by resValue?
似乎 productFlavors 在 buildTypes 之前进行评估,因此 buildTypes 中设置的 resValue 优先.我想在调试版本中将Debug"附加到应用程序名称,但我需要获取在产品风味中设置的值才能附加到它.
It appears that productFlavors is evaluated before buildTypes, so a resValue set in buildTypes takes precedence. I want to append "Debug" to the app name in debug builds, but I need to get the value that was set in the product flavor in order to append to it.
我尝试了 Marcin Koziński 的使用变量的建议,但所有产品风格在任何构建类型之前进行评估.因此,这不起作用:
I tried Marcin Koziński's suggestion to use a variable, but all product flavors are evaluated before any build type. Therefore, this does not work:
android {
String appName = ""
productFlavors {
Foo {
appName = "Foo"
}
Bar {
appName = "Bar"
}
}
buildTypes {
release {
resValue "string", "app_name", appName
}
debug {
resValue "string", "app_name", appName + " Debug"
}
}
}
在 buildTypes 中,appName 始终具有 last 产品风格的值.所以在这个例子中,所有构建都接收到名称 "Bar" 或 "Bar Debug".
In buildTypes, appName always has the value from the last product flavor. So in this example, all builds receive the name "Bar" or "Bar Debug".
基本上,我需要一个类似于 applicationIdSuffix 的 resValueSuffix.显然不存在这样的动物.com.android.application 插件是否公开了我可以用来实现此目的的任何东西?
Basically, I need a resValueSuffix analogous to applicationIdSuffix. Apparently no such animal exists. Does the com.android.application plugin expose anything that I could use to achieve this?
推荐答案
如果您只是尝试设置 App Label(或其他清单值),您可以使用 清单占位符.
If you are only trying to set the App Label (or other manifest values) you can solve this with manifest placeholders.
android {
productFlavors {
Foo {
applicationId "com.myexample.foo"
manifestPlaceholders.appName = "Foo"
}
Bar {
applicationId "com.myexample.bar"
manifestPlaceholders.appName = "Bar"
}
}
buildTypes {
release {
manifestPlaceholders.appNameSuffix =""
}
debug {
manifestPlaceholders.appNameSuffix =".Debug"
applicationIdSuffix ".debug"
}
}
}
然后在您的 Android Manifest 中,您只需将两个占位符用于您的应用名称(或其他值)
Then in your Android Manifest you simply use both placeholders for your app name (or other values)
<application
android:label="${appName}${appNameSuffix}"
...
</application>
这允许您在单个设备上并排安装所有 4 个变体,并在应用程序抽屉/启动器中为它们赋予不同的名称.
This allow you to install all 4 variants side by side on a single device as well as give them different names in the app drawer / launcher.
编辑 2019 年 11 月 22 日
EDIT 11/22/2019
根据@javaxian 的反馈更新了占位符值的设置方式
Updated how placeholders values are set based on feedback from @javaxian
这篇关于如何在 build.gradle 中获取资源值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)