Product Flavor: Duplicate class found

sockeqwe picture sockeqwe · May 25, 2016 · Viewed 20.7k times · Source

I have a question, but I'm sitting here in front of my app since hours but I can't understand what the problem is.

I have an android app (written in kotlin) and I want to make two product flavors and override a class / file in the product flavor:

So my gradle script is that:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'


android {
  ...
  productFlavors {
    foo {
      applicationId "com.foo"
    }
  }
}

My files are structured as follows:

 - src
    - androidTest
    - foo
      - java
        - com
          - example
            - Bar.kt
    - main
      - java
        - com
          - example
            - Bar.kt
    - test

So basically I would like to override Bar.kt file in foo product flavor, but somehow it doesn't work: It says class Bar is duplicated.

Any hint?

Answer

miensol picture miensol · May 25, 2016

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.