Android resValue gradle 错误:“generated.xml”中不支持类型“String”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21600160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
resValue gradle error: Unsupported type "String" in "generated.xml"
提问by owe
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 覆盖资源。就在昨天,android 发布了一个gradle 插件。基于这篇关于 G+ 的帖子,我决定写这个问题。
The problem I have described in detail:
我详细描述的问题:
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:
我想根据 . 创建一些资源值buildType
,但这不能正常工作:只有在我通过命令行进行完整构建时,才会创建文件“generated.xml”:
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\...\build\res\generated\release\values\generated.xml
Every other build-trial doesn't create this file. I tried following:
其他所有构建试验都不会创建此文件。我尝试了以下操作:
- over IDE:
- rebuild project
- execute external task "assembleBuildVariant"
- over command line:
- gradlew assembleBuildVariant
- 通过IDE:
- 重建项目
- 执行外部任务“assembleBuildVariant”
- 通过命令行:
- gradlew assembleBuildVariant
Strange gradle console output:
奇怪的 gradle 控制台输出:
:app:generateBuildVariantResValues UP-TO-DATE
My build.gradle:
我的 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"
}
}
My "generated.xml":
我的“生成的.xml”:
<!-- Automatically generated file. DO NOT MODIFY -->
<!-- Values from build type: release -->
<item name="RES FOO" type="String">RES FOO RELEASE</item>
My question:
我的问题:
Is this a bug or did I miss something? And why this file isn't created by a Rebuild
over the IDE?
这是一个错误还是我错过了什么?为什么这个文件不是由Rebuild
IDE创建的?
My build.gradle (UPDATE 2014-02-10 based on rciovatis answer):
我的 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"
}
}
UPDATE 2014-02-14 IT WORKS:
更新 2014-02-14 它有效:
After an update of the gradle android plugineverything works fine:
在gradle android 插件更新后一切正常:
In /build/res/all/ you should see following folders:
在 /build/res/all/ 中,您应该看到以下文件夹:
- all
- generated (-> here you find the generated resource values by
resValue
)
- 全部
- 生成的(-> 在这里您可以找到生成的资源值
resValue
)
The first folder all
contains all merged resources. In the direction all/<buildVariant>/values/values.xml
you should find the generated resources, in my case:
第一个文件夹all
包含所有合并的资源。在all/<buildVariant>/values/values.xml
你应该找到生成的资源的方向上,在我的例子中:
// 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)
回答by rciovati
I solved adding the resources also in the defaultConfig
block. For you it would be something like:
我也解决了在defaultConfig
块中添加资源的问题。对你来说,它会是这样的:
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"
}
}
}
Please note that:
请注意:
- item type must be
string
and notString
- item name must not contain spaces (as normal resource name)
- 项目类型必须是
string
而不是String
- 项目名称不得包含空格(作为正常资源名称)
EDIT: Since 0.8.3 it should works fine just declaring the resValue in the build type block.
编辑:从 0.8.3 开始,它应该可以正常工作,只需在构建类型块中声明 resValue。