Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build flavors for different version of same class

I've got a project, structured like this:

project/    |    |---src/         |---flavorA2/         |      |         |      |---java/         |      |     |---com.abc.flavorA.mk2         |      |                 |-----classA.java         |      |                 |-----classB.java         |      |---res/         |      |---AndroidManifest.xml         |         |---main         |      |---java/         |      |     |---com.abc.flavorA         |      |                 |-----classA.java         |      |                 |-----classB.java         |      |                 |-----classC.java         |      |                 |-----classD.java         |      |---res/         |      |    |---drawable/         |      |    |---layout/         |      |    |---values/         |      |                  |      |---AndroidManifest.xml         |         |---flavorA 

flavorA will use the source and assets from main completely while flavorA2 has some small changes in classA and classB and the package name is also changed to com.abc.flavorA.mk2.

I had the build.gradle file like this:

... buildTypes {         release {             runProguard false             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'         }     }     productFlavors {         flavorA2 {             packageName "com.abc.flavorA.mk2"             versionCode 2             versionName "1.0.1"         }          flavorA {             packageName "com.abc.flavorA"         }     } ... 

I run the code by selecting the build variant to flavorA2. However the running results shows that the gradle still choose the classes (classA and classB) from main instead of using the changed version inside flavorA2.

Am I missing something here?

like image 659
dumbfingers Avatar asked May 16 '14 15:05

dumbfingers


People also ask

When would you use product flavor in your build setup?

123 let's go Simply put, a product flavor is a variant of your app. It is very useful when you want to create multiple versions of your app. This means you can generate different versions or variants of your app using a single codebase.

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".


1 Answers

Since you have the classes under 2 different packages, these are totally different classes. So the classes aren't replacing each other.

With flavors, you can't override class files. So, one way to accomplish what you want is move these classes out of main, and into flavorA.

So you would have something like this:

project/    |    |---src/         |---flavorA2/         |      |         |      |---java/         |      |     |---com.abc         |      |                 |-----classA.java         |      |                 |-----classB.java         |      |---res/         |      |---AndroidManifest.xml         |         |---main/         |      |---java/         |      |     |---com.abc.flavorA         |      |                 |-----classC.java         |      |                 |-----classD.java         |      |---res/         |      |    |---drawable/         |      |    |---layout/         |      |    |---values/         |      |                  |      |---AndroidManifest.xml         |         |---flavorA/         |      |---java/         |      |     |---com.abc         |      |                 |-----classA.java         |      |                 |-----classB.java 

This way, whenever you pick a flavor, only one version of ClassA and ClassB will be visible.

like image 160
Hassan Ibraheem Avatar answered Oct 13 '22 22:10

Hassan Ibraheem