問題描述
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模板網!