Is there a way to have a common section with buildConfigField and resValue in gradle?

前端 未结 1 1464
孤独总比滥情好
孤独总比滥情好 2020-12-08 13:40

I have a product with multiple product flavors like:

buildTypes {
    debug {
    }

    release {
    }

}

productFlavors {
    flavor1 {
        buildCon         


        
相关标签:
1条回答
  • 2020-12-08 14:20

    Selvin is correct, use the defaultConfig closure - there is no neater way! In the following example, flavors 1, 2 & 5 would set the default country and language to de. Flavors 3 & 4 override this with their own languages.

    defaultConfig {
        buildConfigField "String", "country", "de"
        buildConfigField "String", "language", "de"
    }
    
    buildTypes {
        debug {
        }
    
        release {
        }
    }
    
    productFlavors {
        flavor1 {
            buildConfigField "String", "appName", "Flavor1"
        }
        flavor2 {
            buildConfigField "String", "appName", "Flavor2"
        }
        flavor3 {
            buildConfigField "String", "country", "uk"
            buildConfigField "String", "language", "en_GB"
            buildConfigField "String", "appName", "Flavor3"
        }
        flavor4 {
            buildConfigField "String", "country", "fr"
            buildConfigField "String", "language", "fr"
            buildConfigField "String", "appName", "Flavor4"
        }
        flavor5 {
            buildConfigField "String", "appName", name.capitalize()
        }
    }
    

    NOTE

    Just an FYI that you can use name.capitalize() to turn the name of any flavour, e.g. flavor5, into the app name of Flavor5 by using the capitalize() method - which will capitalize the first character in the String. However, this MUST go in the flavor, not defaultConfig

    0 讨论(0)
提交回复
热议问题