几周前我发布了一个问题如何根据 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:
奇怪的 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 找到生成的资源值)resValue)第一个文件夹all 包含所有合并的资源.在方向 all/<buildVariant>/values/values.xml 你应该找到生成的资源,在我的例子中:
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:
// 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模板网!
更新到 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 库的传递依赖)