Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android studio gradle flavor dimensions build varients not working correctly

I have two dimensions of an app, call then green and blue. There will only be these two dimensions but an unlimited number of product flavors. This is the way I'm setting it up in gradle

flavorDimensions "green", "blue"

productFlavors {

    one {
        applicationId "com.app.green.one"
        versionCode 1
        versionName "1.0.0.1";
        flavorDimension = "green"
    }
    two {
        applicationId "com.app.blue.two"
        versionCode 6
        versionName "1.0.1";
        flavorDimension = "blue"
    }
}

But then after i sync gradle, in the build variants tab all I see is oneTwoDebug and oneTwoRelease, where I should see greenOneDebug greenOneRelease, blueTwoDebug, blueTwoRelease

In theory I want to extend it to be something like this

one {
    applicationId "com.app.green.one"
    versionCode 1
    versionName "1.0.0.1";
    flavorDimension = "green"
}
two {
    applicationId "com.app.blue.two"
    versionCode 6
    versionName "1.0.1";
    flavorDimension = "blue"
}
three {
    applicationId "com.app.green.three"
    versionCode 1
    versionName "1.0.0.1";
    flavorDimension = "green"
}
four {
    applicationId "com.app.blue.four"
    versionCode 6
    versionName "1.0.1";
    flavorDimension = "blue"
}

In this case dimensions represent the "type" of app, and then the flavors are more for organizations which can be added.

**EDIT I had the wrong set up for gradle as pointed out here is a more accurate depiction of what I have

flavorDimensions "type", "organization"

productFlavors {

    blue {
        applicationId "com.app.blue"
        flavorDimension = "type"
        versionCode 6
        versionName "1.0.1";
    }
    red {
        applicationId "com.app.red"
        flavorDimension = "type"
        versionCode 1
        versionName "1.0.0.1";
    }

    company1 {
        flavorDimension = "organization"
    }
    company2 {
        flavorDimension = "organization"
    }
}

So far this works, So I can create java source directories for toggling types, but what if I want organization specific config files, do I create java source dirs for each organization as well?

like image 852
Brian Avatar asked Jun 15 '15 20:06

Brian


People also ask

How do I fix Gradle issues?

For this, you have to connect your PC to the internet and you have to open your Android studio. After opening your project click on the Sync Project with Gradle files option. This will automatically download the new Gradle files and will fix the issue which is caused by the Gradle files.

How do I get the current flavor in Gradle?

There is no "current flavor". The Gradle build file is building an object model of the build process. It is not an interpreted script only being used once a build is underway and a "current flavor" is known.

What is flavorDimensions?

A flavorDimension is something like a flavor category and every combination of a flavor from each dimension will produce a variant. In your case, you must define one flavorDimension named "type" and another dimension named "organization".

What is Flavour dimension Android?

The flavor dimensions define the cartesian product that will be used to produce variants. Example: flavorDimensions("dimA", "dimB") productFlavors { row1 { ... dimension = "dimA" } row2 { ... dimension = "dimA" } row3 { ... dimension = "dimA" } col1 { ...


1 Answers

I think you misunderstood the concept of flavorDimension.

A flavorDimension is something like a flavor category and every combination of a flavor from each dimension will produce a variant.

In your case, you must define one flavorDimension named "type" and another dimension named "organization". It will produce, for each flavor in the dimension "organization" all possible "type" (or the dual formulation : for each "type" it will produce a variant for each organization).

The flavor dimensions define the cartesian product that will be used to produce variants.


EDIT : I'll try to illustrate with pseudo-gradle code:

Let's define some "type" : bronze, silver and gold

Let's define some organizations : customerA, customerB, customerC

All those are productFlavors, but they belong to 2 different dimensions:

flavorDimensions("type_line", "organization")
productFlavors {

    gold {
        ...
        dimension = "type_line"
    }
    silver {
        ...
        dimension = "type_line"
    }
    bronze {
         ...
        dimension = "type_line"
    }

     customerA {
        ...
        dimension = "organization"
    }
    customerB {
        ...
        dimension = "organization"
    }
    customerC {
         ...
        dimension = "organization"
    }

}

This config will produce 18 (3*3*2) variants (if you have the 2 standard build types : debug and release) :

gold-customerA-debug ; gold-customerA-release ; gold-customerB-debug ; gold-customerB-release ; gold-customerC-debug ; gold-customerC-release ;

silver-customerA-debug ; silver-customerA-release ; silver-customerB-debug ; silver-customerB-release ; silver-customerC-debug ; silver-customerC-release ;

... (the same for bronze)

Note that the name of the dimension is totally arbitrary and have no impact on variant names.

Flavor dimensions are very powerful, but if you use too much of them : it results in an exponential explosion of the number of variants (a post build clean-up task may be useful to delete useless or non-sense variant)

like image 180
ben75 Avatar answered Oct 11 '22 11:10

ben75