resValue gradle 错误:不支持的类型“字符串"在“生成的.xml"中

resValue gradle error: Unsupported type quot;Stringquot; in quot;generated.xmlquot;(resValue gradle 错误:不支持的类型“字符串在“生成的.xml中)
本文介绍了resValue gradle 错误:不支持的类型“字符串"在“生成的.xml"中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

几周前我发布了一个问题如何根据 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 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"中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android release APK crash with java.lang.AssertionError: impossible in java.lang.Enum(Android 发布 APK 因 java.lang.AssertionError 崩溃:在 java.lang.Enum 中不可能)
Finished with Non Zero Exit Value 3(以非零退出值 3 结束)
On gradle:3.0.0 More than one file was found with OS independent path #39;META-INF/ASL2.0#39;(在 gradle:3.0.0 上找到多个文件,其独立于操作系统的路径为“META-INF/ASL2.0)
Android : app loading library at runtime on Lollipop but not IceCreamSandwich(Android:运行时在 Lollipop 上而不是 IceCreamSandwich 上的应用程序加载库)
buildConfigField depending on flavor + buildType(buildConfigField 取决于风味 + buildType)
How do I suppress warnings when compiling an android library with gradle?(使用 gradle 编译 android 库时如何抑制警告?)