問題描述
幾周前我發布了一個問題如何根據 buildType 覆蓋資源.就在昨天,還有一個 gradle 插件版本為 android.根據這篇關于 G+ 的帖子,我決定寫下這個問題.
A few weeks ago I posted a question How to override resources depending on buildType. And just yesterday there was a gradle plugin release for android. Based on this post on G+ I decided to write this question.
我已經詳細描述的問題:
我想根據 buildType
創建一些資源值,但這不能正常工作:只有在我通過命令行進行完整構建時才會創建文件generated.xml":
I want to create some resource values depending on the buildType
, but this doesn't work properly:
The file "generated.xml" will be only created if I make a complete build over the command line:
gradlew build
但是通過命令行構建完整的項目我也得到了一個錯誤:
But I also get an error by building the complete project over comannd line:
* What went wrong: Execution failed for task ':app:merge<buildVariant>Resources'.
Unsupported type 'String' in file C:Users...uild
esgenerated
eleasevaluesgenerated.xml
所有其他構建試用版都不會創建此文件.我嘗試了以下操作:
Every other build-trial doesn't create this file. I tried following:
- 通過 IDE:
- 重建項目
- 執行外部任務assembleBuildVariant"
- gradlew assembleBuildVariant
奇怪的 gradle 控制臺輸出:
Strange gradle console output:
:app:generateBuildVariantResValues UP-TO-DATE
我的 build.gradle:
buildTypes { debug{ buildConfigField "String", "FOO", ""FOO DEBUG"" resValue "String", "RES FOO", "RES FOO DEBUG" } release { buildConfigField "String", "FOO", ""FOO RELEASE"" resValue "String", "RES FOO", "RES FOO RELEASE" } }
我的生成的.xml":
<!-- Automatically generated file. DO NOT MODIFY --> <!-- Values from build type: release --> <item name="RES FOO" type="String">RES FOO RELEASE</item>
我的問題:
這是一個錯誤還是我錯過了什么?為什么這個文件不是由 IDE 上的
Rebuild
創建的?Is this a bug or did I miss something? And why this file isn't created by a
Rebuild
over the IDE?我的 build.gradle(更新 2014-02-10 基于 rciovatis 答案):
defaultConfig { minSdkVersion 14 targetSdkVersion 19 versionCode 1 versionName "1.0" resValue "string", "RES_FOO", "RES FOO" } buildTypes { debug{ buildConfigField "String", "FOO", ""FOO DEBUG"" resValue "string", "RES_FOO", "RES FOO DEBUG" } release { buildConfigField "String", "FOO", ""FOO RELEASE"" resValue "string", "RES_FOO", "RES FOO RELEASE" } }
2014 年 2 月 14 日更新它的工作原理:
更新 gradle android 插件之后一切正常:
在/build/res/all/中,您應該會看到以下文件夾:
In /build/res/all/ you should see following folders:
- 全部
- 已生成(-> 在這里您可以通過
resValue
找到生成的資源值)
- all
- generated (-> here you find the generated resource values by
resValue
)
第一個文件夾
all
包含所有合并的資源.在方向all/<buildVariant>/values/values.xml
你應該找到生成的資源,在我的例子中:The first folder
all
contains all merged resources. In the directionall/<buildVariant>/values/values.xml
you should find the generated resources, in my case:// for buildType DEBUG <item name="TESTFOO" type="string">TEST FOO DEBUG</item> // for buildType RELEASE <item name="TESTFOO" type="string">TEST FOO RELEASE</item>
要獲取代碼中的值,只需像其他所有資源一樣使用生成的資源:
To get the values in code just use the generated resource like all others:
getResources().getString(R.string.TESTFOO)
推薦答案
我解決了在
defaultConfig
塊中添加資源的問題.對你來說,它會是這樣的:I solved adding the resources also in the
defaultConfig
block. For you it would be something like:android { defaultConfig { resValue "string", "RES_FOO", "RES FOO RELEASE" } buildTypes { debug{ buildConfigField "String", "FOO", ""FOO DEBUG"" resValue "string", "RES_FOO", "RES FOO DEBUG" } release { buildConfigField "String", "FOO", ""FOO RELEASE"" resValue "string", "RES_FOO", "RES FOO RELEASE" } } }
請注意:
- 項目類型必須是
string
而不是String
- 項目名稱不得包含空格(與正常資源名稱一樣)
從 0.8.3 開始,只需在構建類型塊中聲明 resValue 即可正常工作.
Since 0.8.3 it should works fine just declaring the resValue in the build type block.
這篇關于resValue gradle 錯誤:不支持的類型“字符串"在“生成的.xml"中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!