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模板网!
更新到 Android Build Tools 25.1.6 GCM/FCM 后出现 IncompIncompatibleClassChangeError after updating to Android Build Tools 25.1.6 GCM / FCM(更新到 Android Build Tools 25.1.6 GCM/FCM 后出现 Incompatible
如何在 gradle 中获取当前风味How to get current flavor in gradle(如何在 gradle 中获取当前风味)
如何修复“意外元素<查询>在“清单How to fix quot;unexpected element lt;queriesgt; found in lt;manifestgt;quot; error?(如何修复“意外元素lt;查询gt;在“清单中找到错误
基于 Android Gradle 中多风味库的多风味应用Multi flavor app based on multi flavor library in Android Gradle(基于 Android Gradle 中多风味库的多风味应用)
Android 依赖在编译和运行时有不同的版本Android dependency has different version for the compile and runtime(Android 依赖在编译和运行时有不同的版本)
本地 aar 库的传递依赖Transitive dependencies for local aar library(本地 aar 库的传递依赖)