Java – Build variants using Gradle in Android Studio

Build variants using Gradle in Android Studio… here is a solution to the problem.

Build variants using Gradle in Android Studio

I have a question about using Gradle’s style in Android Studio…
Consider the following:

There are 3 styles of your build; flavor 1, flavor 2, and flavor 3.

All 3 flavors depend on the same file, which is referred to as MainActivity .java for simplicity.
In addition, all 3 styles have their own defined constant files, called

Except for, Flavor1 and Flavor2 rely on the same source code. That is, both Flavor1 and Flavor2 use the following file from main,, and another file, which for simplicity is called the AnotherActivity .java.

Now, Flavor3 uses the MainActivity .java, but on the other hand requires some additional customization and some changes to the AnotherActivity .java.

File Structure:


Is there a way to accomplish this type of build dependency in Android Studio using Gradle without duplicate class file errors?


Best regards,
Christopher Steven


I found that only resources can be merged in this way. The source files are merged into a single build path. You need to copy to each flavor, just as you would with Looking at refactoring your source code to reduce duplication is the best thing you can do. You can probably do this using in src/main/java of other extensions.

You’ll notice that when you select a build variant in Android Studio, it only displays the source code for the selected variant in the package view.

Related Problems and Solutions