Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

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

I have a product with multiple product flavors like:

buildTypes {
    debug {
    }

    release {
    }

}

productFlavors {
    flavor1 {
        buildConfigField "String" "country" "se"
        buildConfigField "String" "language" "sv-SE"
        buildConfigField "String" "appName" "Flavor1"
    }
    flavor2 {
        buildConfigField "String" "country" "se"
        buildConfigField "String" "language" "sv-SE"
        buildConfigField "String" "appName" "Flavor2"
    }
    flavor3 {
        buildConfigField "String" "country" "se"
        buildConfigField "String" "language" "sv-SE"
        buildConfigField "String" "appName" "Flavor3"
    }
    flavor4 {
        buildConfigField "String" "country" "se"
        buildConfigField "String" "language" "sv-SE"
        buildConfigField "String" "appName" "Flavor4"
    }
    flavor5 {
        buildConfigField "String" "country" "se"
        buildConfigField "String" "language" "no-NO"
        buildConfigField "String" "appName" "Flavor5"
    }
}

I would prefer a common section with all properties and only override those that are different. Is this possible?

I would also like to put all flavors (and perhaps buildTypes) in it's own file to make it more readable. So whenever you have to change a flavor, you can easily find it in its own file, and not have to scroll over thousands of line which it will be if I have all flavors and buildTypes together with all the rest in the main build file.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...