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 constants.java.
Except for Constants.java, Flavor1 and Flavor2 rely on the same source code. That is, both Flavor1 and Flavor2 use the following file from main, MainActivity.java, 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:
src
-main
--.java
---MainActivity.java
---AnotherActivity.java
-Flavor1
--.java
---Constants.java
-Flavor2
--.java
---Constants.java
-Flavor3
--.java
---Constants.java
---AnotherActivity.java
Is there a way to accomplish this type of build dependency in Android Studio using Gradle without duplicate class file errors?
Thanks!
Best regards,
Christopher Steven
Solution
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 AnotherActivity.java to each flavor, just as you would with Constants.java. Looking at refactoring your source code to reduce duplication is the best thing you can do. You can probably do this using AbstractAnotherActivity.java 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.