Is it possible to define an Android string-array resource in Gradle?

后端 未结 4 2343
被撕碎了的回忆
被撕碎了的回忆 2021-01-04 17:50

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


    
         


        
4条回答
  •  庸人自扰
    2021-01-04 18:17

    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.

提交回复
热议问题