Build Flavors for Different Version of Same Class

Build flavors for different version of same class

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.

Android Studio build flavors - How to have same source files in diverse flavors

I think you can't have same class in main flavor and your other flavor. you should just create another flavor, then move your Hello class from main flavor to that new flavor. this rule is just for .java files. I mean you can have an xml file in main flavor and another version in your custom flavor but you can't do this with java files.

here is a useful link with further explanation.

This same class for two different product flavor

Create two copies of your Http class, one in src/first/com.androidapp.testproject/repo and the other one in src/second/com.androidapp.testproject/repo, and remove the copy from src/main/....

The first version of your class in src/first/... will get built for your flavor first, and the one in src/second/... will get built for your flavor second. Of course, you have to adjust the value for CODE in the appropriate copy of your classes.

See the following answer for more information https://stackoverflow.com/a/16746755/3286819

Use different class for android build variant

You need to create your flavor directory for it. For example, if you want to create specific class for fukusima flavor, you need to create the fukusima directory inside your src directory. Like this:

src/fukusima/java/your/package/name/bottommenu

then you create the BottomMenuButtons there. Change your/package/name/bottommenu to your BottomMenuButtons path.

The other way is using a flag for adding specific flag for your flavor. First, add a specific flag to your flavor. For example, we use USE_FIVE_TAB. Change the flavor by adding the flag:

productFlavors {
stanby {
buildConfigField "boolean", "USE_FIVE_TAB", "false"
dimension "type"
applicationId "${PACKAGE_NAME}"
}
part {
buildConfigField "boolean", "USE_FIVE_TAB", "false"
dimension "type"
applicationId "${PACKAGE_NAME}.part"
}

full {
buildConfigField "boolean", "USE_FIVE_TAB", "true"
dimension "type"
applicationId "${PACKAGE_NAME}.full"
}

// add the same flag to all of your flavors
...

Then, you can use it something like this:

if(BuildConfig.USE_FIVE_TAB) {
// set to use five tabs
} else {
// use four tabs
}

Creating flavor specific classes

To be able to compile files for a specific flavor, you have to:

  1. Include it into your build.gradle script.
  2. Click on Sync Project with Gradle Files.
  3. Switch to a build variant based on that flavor in the Build Variants panel (either ending with Debug or Release, even it the first one is the common choice while developing).

This is pretty annoying, as you can't compile more than one build variant while you code. If you add a class to a flavor referencing it from the main code and forget to add it to the other flavors in the same flavor dimension, you will break them without seeing any error. You can always build more than one build variant from the Build > Generate Signed APK wizard though (on a Mac, just press cmd while you select all the variants you want to build), so some of the APKs may be built and others not.



Related Topics



Leave a reply



Submit