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

后端 未结 4 2344
被撕碎了的回忆
被撕碎了的回忆 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:32

    Defining String[] of Urls in BuildConfig:

    android {
    
        ...
    
        def tests1 = ["url-a1", "url-b1", "url-c1"]
    
        ext {
            tests2 = ["url-a2", "url-b2", "url-c2"]
        }
    
        // Specifies one flavor dimension.
        flavorDimensions "default"
    
        productFlavors {
            devel {
                dimension 'default'
            }
        }
    
        //Default values for all productFlavors
        productFlavors.all {
            ext.tests3 = ["url-a3", "url-b3", "url-c3"]
        } 
    
        android.applicationVariants.all { variant ->
    
            buildConfigField("String[]", "TESTS1", '{' + tests1.collect {
                "\"${it}\""
            }.join(",") + '}')
    
            buildConfigField("String[]", "TESTS2", '{' + android.ext.tests2.collect {
                "\"${it}\""
            }.join(",") + '}')
    
            //TEST3 works only when is some Flavor defined 
            buildConfigField("String[]", "TESTS3", '{' + variant.productFlavors.get(0).ext.tests3.collect {
                "\"${it}\""
            }.join(",") + '}')
        }
    }
    

提交回复
热议问题