本文介绍了是否有可能在Gradle中定义Android字符串数组资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < p>< p>< ;资源> 
< string-array name =url_array>
< item> http://www.url1.com< / item>
< item> http://www.url2.com< / item>
< item> http://www.url3.com< / item>
< / string-array>
< /资源>

...在我应用的build.gradle配置文件中。我不想在res / values / arrays.xml中对这些值进行硬编码,因为我希望能够为不同的构建变体(dev和prod)生成不同的数组内容。我知道有解决方法,但如果可能的话,这将是最干净的解决方案。



我尝试过类似下面摘录的resValue类型的string-array 或数组,但我得到一个错误,说resValue()不存在。当然,字符串的resValue类型参数适用于单个字符串,但我需要生成一个字符串数组资源。

  resValuestring-array,url_array,
[http://www.url1.com,
http://www.url2.com,
http ://www.url3.com]

Android版Gradle for Android文档无济于事。它列出了这种方法......





...但它并不表示该类型的有效值PARAM。它只是有一个链接,说:但这只是指向资源类型的常规Android文档,并没有描述如何在Gradle Android DSL中表达它们。



有没有人有任何指导?

HREF = https://stackoverflow.com/users/384306/schwiz> @schwiz 。我仍然无法通过resValue找到一种方法,但可以定义 buildConfigField 来实现相同的目标:

  buildConfigFieldString [],URL_ARRAY,
{+
\http://www.url1.com\, +
\http://www.url2.com\,+
\http://www.url3.com\+
}

这使您可以通过BuildConfig访问数组:

  public static final String [] URL_ARRAY = {
http://www.url1.com,
http:/ /www.url2.com,
http://www.url3.com}; //为清晰起见,添加了空白

然后,您可以覆盖每个buildType的buildConfigField值。所以,除非你特别需要这个在R.array。*中,这将满足你的需求。如果有其他人知道如何使用resValue做到这一点,请暂时将其打开。


In Gradle for Android, I'm trying to generate the equivalent of this string-array resource...

<resources>
    <string-array name="url_array">
       <item>http://www.url1.com</item>
       <item>http://www.url2.com</item>
       <item>http://www.url3.com</item>
    </string-array>
</resources>

...in my app's build.gradle config file. I don't want to hardcode these values in res/values/arrays.xml, since I want to be able to generate different array contents for different build variants (dev vs. prod). I know there are workarounds, but this would be the cleanest solution if it's possible.

I've tried things like the excerpt below with a resValue type of "string-array" or "array", but I get an error saying resValue() doesn't exist. Of course, a resValue type param of "string" works for single strings, but I need to generate a string array resource.

resValue "string-array", "url_array",
   ["http://www.url1.com",
    "http://www.url2.com",
    "http://www.url3.com"]

The Gradle for Android documentation doesn't help. It lists this method...

void resValue(String type, String name, String value)

...but it doesn't indicate the valid values for the type param. It simply has a link that says "See Resource Types" but that just points to the regular Android docs for resource types and doesn't describe how to express them in the Gradle Android DSL.

Does anyone have any guidance? I've looked all over online and haven't found anything.

解决方案

Here's the closest I got, with guidance from @schwiz. I still can't find a way to do this with resValue, but it's possible to define a buildConfigField that can accomplish the same goal:

buildConfigField "String[]", "URL_ARRAY",
        "{" +
                "\"http://www.url1.com\"," +
                "\"http://www.url2.com\"," +
                "\"http://www.url3.com\"" +
                "}"

That gives you access to the array, via BuildConfig:

public static final String[] URL_ARRAY = {
   "http://www.url1.com",
   "http://www.url2.com",
   "http://www.url3.com"}; // whitespace added for clarity

You can then override the buildConfigField value per buildType. So, unless you specifically need this to be in R.array.*, this will meet your needs. Leaving this open for now in case anyone else knows how to do this with resValue.

这篇关于是否有可能在Gradle中定义Android字符串数组资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 16:05