Gradle Mixing Assemblies

Is there a way to β€œmix” multiple flavors in gradle?

For example, let's say I have two options: Red and Blue . Now let me say that I have two flavors that I want them to have (sub-flavors if you want): Complex and Simple .

What I know so far is that it will require me to create four flavors: Red-Complex , Red-Simple , Blue-Complex , Blue-Simple .

I do not understand how this should be structured within the project. How to define a Red code that is split between Red-Complex and Red-Simple flavors, but also has a Complex code that is split between Red-Complex and Blue-Complex ? I do not want to copy / paste the code between tastes.

What is the best way to achieve this in gradle?

+4
source share
1 answer

Here's how flavoring works: https://proandroiddev.com/advanced-android-flavors-part-1-building-white-label-apps-on-android-ade16af23bcf

β†’ In your src folder you want to create 2 varieties called "red", "blue".

β†’ Create 2 folders with the name "red", "blue" in src (the same level as the main one).

β†’ folder structure as the main one.

β†’ Here you have the option to override any XML / resources / Java files / values, etc.

β†’ (resources with the same name as main will be redefined when creating Flavor).

β†’ Use the sample code in build.gradle to add flavor to the project.

 productFlavors { blue{ minSdkVersion 21 applicationId 'something' targetSdkVersion 28 versionCode 1 versionName "1.0.0" } red { minSdkVersion 21 applicationId 'something' targetSdkVersion 28 versionCode 1 versionName '1.0.0' } } buildTypes { Debug { } Release { } } 

Will give assembly options:

 blueDebug blueRelease redDebug redRelease 

// ================ Add additional tastes to it ================

Here: https://proandroiddev.com/advanced-android-flavors-part-2-enter-flavor-dimensions-4ad7f486f6

β†’ have sub-flavors such as Simple, Complex (in other words "buildTypes").

β†’ For sub-flavors or, say, buildTypes, we use sizes.

β†’ flavourDimensions "Flavor", "SubFlavour" (in your build.gradle) // It can be any keyword, use it inside yourself // think of this keyword as a group identifier that defines a collection of options as a group.

β†’ Use it to your liking β†’ We may think that the Keyword β€œdimension” is more like defining a set of aroma and sub-aroma

 flavorDimensions "Flavour", "SubFlavour" productFlavors { // group one Simple { dimension "SubFlavour" } Complex { dimension "SubFlavour" } //group two // Take a note: do not use upper case in your flavours (one which match the folder names in src) // Sub-Flavours can have Upper case blue { dimension "Flavour" minSdkVersion 21 applicationId 'something' targetSdkVersion 28 versionCode 1 versionName "1.0.0" } red { dimension "Flavour" minSdkVersion 21 applicationId 'something' targetSdkVersion 28 versionCode 1 versionName '1.0.0' } } 

β†’ If configured correctly, you will see below buildVariants.

  blueSimpleDebug blueSimpleRelease blueComplexDebug blueComplexRelease redSimpleDebug redSimpleRelease redComplexDebug redComplexRelease 

// ================== build.gradle ================= // Your build.gradle may look here.

 android { buildTypes { debug { } release { } // Any other you want to add here // Ex, development {} } flavorDimensions "Flavour", "SubFlavour" productFlavors { Simple { dimension "SubFlavour" } Complex { dimension "SubFlavour" } Blue { dimension "Flavour" minSdkVersion 21 applicationId 'something' targetSdkVersion 28 versionCode 1 versionName "1.0.0" } Red { dimension "Flavour" minSdkVersion 21 applicationId 'something' targetSdkVersion 28 versionCode 1 versionName '1.0.0' } } sourceSets { main { aidl.srcDirs = ['src/main/aidl', 'src/main/res/animation', 'src/main/res/animations'] res.srcDirs = [ 'src/main/res/anim', 'src/main/res/layouts/xyz', 'src/main/res/layouts/abc', 'src/main/res/layouts', 'src/main/res/layout', 'src/main/res' ] resources.srcDirs = ['src/main/resources', 'src/main/resources/xml'] manifest.srcFile 'src/main/AndroidManifest.xml' java.srcDirs = ['src/main/java'] } red { java.srcDirs = ['src/red/java'] res.srcDirs = [ 'src/red/res/layouts/xyz', 'src/red/res/layouts/abc', 'src/red/res/layouts', 'src/red/res' ] assets.srcDirs = ['src/red/assets'] manifest.srcFile 'src/red/AndroidManifest.xml' } blue { java.srcDirs = ['src/blue/java'] res.srcDirs = [ 'src/blue/res/layouts/xyz', 'src/blue/res/layouts/abc', 'src/blue/res/layouts', 'src/blue/res' ] assets.srcDirs = ['src/blue/assets'] manifest.srcFile 'src/blue/AndroidManifest.xml' } } } //andorid dependencies { // yout dependencies } 
0
source

All Articles