Product Flavor: Duplicate class found Product Flavor: Duplicate class found android android

Product Flavor: Duplicate class found


The documentation for variants states (emphasis mine):

Note: For a given build variant, Gradle throws a build error if it encounters two or more source set directories that have defined the same Java class. For example, when building a debug APK, you cannot define both src/debug/Utility.java and src/main/Utility.java. This is because Gradle looks at both these directories during the build process and throws a 'duplicate class' error. If you want different versions of Utility.java for different build types, you can have each build type define its own version of the file and not include it in the main/ source set.

So the solution is to have it's own version of Bar.kt per variant and exclude it from main source set.


As stated by miensol you cannot put your file to main and flavor specific folders and expect gradle to work the same way android resource system works. But I found a way to do this without code duplication so you don't have to copy your Bar.kt to every flavor folder you have.

So let's say you have three flavors dev, prod and mock. You want your special mocked Bar.kt in mock but the normal implementation in dev and prod flavors. You put your mocked file to the mock flavor specific folder mock/java/com/something/ and you put your "default" implementation to new folder with some random name like non-mock/java/com/something/ naming it something like "common" would also make sense. Now you have to tell gradle where should those flavors look for their Bar.kt class.

Put this in to your build.gradle:

android {    ...    sourceSets {        prod {            java.srcDirs('src/non-mock/java')        }        dev {            java.srcDirs('src/non-mock/java')        }    }}


If you have multiple flavors, Like A, B and C

and your main code contains all activities, and for A and C flavor you want to change some functionality of some activity ex- ShoppingCartActivity

then you need to make some changes as below, put ShoppingCartActivity in all three flavors (including B as well) and remove from main and declare the file into all manifest file other than main manifest

for more details check Build with source sets

- A  - java    - com      - example        - ShoppingCartActivity.kt(some changes)- B  - java    - com      - example        - ShoppingCartActivity.kt- C  - java    - com      - example        - ShoppingCartActivity.kt(new changes added)- main  - java    - com      - example        **(remove from here)**