Redeclaration error with same classname in different Source Sets

When you have created a product flavor you should not place your varianted class into main source set.

Keep only common for all variants files into main

For example, you defined:

  flavorDimensions("api")
  productFlavors {
      create("real") {
          dimension = "api"
      }
      create("mock") {
          dimension = "api"
      }
  }

Since that, you have exactly TWO build variants: real OR mock.

So, you have to have exactly TWO versions of your class (if it should be different):

/src/real/kotlin/com/example/SomeApi.kt
/src/main/kotlin/com/example/ (no file here)
/src/mock/kotlin/com/example/SomeApi.kt

UPD:

All above is correct only for sources! If you create 4 flavors, you must copy your Java/Kotlin class into 4 folders, you don’t have a ‘default’ one.

For resources, the logic is different. Gradle, at first, takes the resource file (some XML file) from the default folder ‘main’ and then replaces it with the overridden one from the flavor folder if the file exists there. If you create 4 flavors, you can keep a default in ‘main’ and you can override it only once.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top