Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

android - resValue gradle error: Unsupported type "String" in "generated.xml"

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.

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:

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:

  • over IDE:
    • rebuild project
    • execute external task "assembleBuildVariant"
  • over command line:
    • gradlew assembleBuildVariant

Strange gradle console output:

:app:generateBuildVariantResValues UP-TO-DATE

My 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":

<!-- 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?

My build.gradle (UPDATE 2014-02-10 based on rciovatis answer):

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:

After an update of the gradle android plugin everything works fine:

In /build/res/all/ you should see following folders:

  • all
  • generated (-> here you find the generated resource values by 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:

// 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"
        }
    }
}

Please note that:

  • item type must be string and not String
  • item name must not contain spaces (as normal resource name)

EDIT: Since 0.8.3 it should works fine just declaring the resValue in the build type block.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...