Build Variants using Gradle in Android Studio
android, android-gradle-plugin, build.gradle, java
Solution
I have found that only resources get merged this way. Source files get merged into a single build path. You will need to copy your AnotherActivity.java to each flavor as you have done with Constants.java. Look to refactor your source a bit to reduce the duplication is the best you can do. You could probably accomplish this with an AbstractAnotherActivity.java in src/main/java that the others extend.
You will notice that when you select the build variant in Android Studio it will show you only the source from the selected variant in the package view.
Problem
I have a question when it comes to flavors in Android Studio using Gradle... Consider the following: You have 3 flavors of your build; Flavor1, Flavor2, and Flavor3. All 3 flavors rely on the same file, call it MainActivity.java for simplicity. Also, all 3 flavors have their own defined Constants File, call it Constants.java. Other than Constants.java, Flavor1 and Flavor2 rely on the same source code. Namely, Flavor1 and Flavor2 both use the following files from main, MainActivity.java and also another file, call it AnotherActivity.java for simplicity. Now, Flavor3 uses MainActivity.java but on the other hand needs some extra customization and some changes to 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 without getting a duplicate class file error in Android Studio using Gradle? Thanks! Best Regards, Christopher Steven