Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent Duplicate Class error while using same named class in different flavors?

First of all, I have just checked a lot of topics in StackOverflow about this issue but none of them solved my problem. My problem is; I have 2 different flavors which are main and experimental named flavors. main is default flavor. I just want to override or use my A.java in experimental build variant but this causes me duplicate class found an error. How can I prevent or fix this issue because I believe that using same named class can be possible via flavors? Or did I do something wrong? I hope you can help me with my problem. Thanks in advance.

Project Structure

  • app
    • src
      • main
        • java
          • core
            • A.java
      • experimental
        • java
          • core
            • A.java

build.gradle


productFlavors{
    main{
        dimension = "default"
        applicationId "com.example.yekta.basicApp.main"
    }
    experimental{
        dimension = "default"
        applicationId "com.example.yekta.basicApp.experimental"
    }
}

sourceSets {
    main {
        java.srcDirs = ['src/main/java']
        res.srcDirs = ['src/main/res']
        assets.srcDirs = ['src/main/assets']
    }

    experimental {
        java.srcDirs = ['src/experimental/java']
        res.srcDirs = ['src/experimental/res']
        assets.srcDirs = ['src/experimental/assets']
    }
}

like image 726
Yekta Sarıoğlu Avatar asked Sep 15 '25 04:09

Yekta Sarıoğlu


1 Answers

Step #1: Rename your one flavor from main to any other valid value that is not main, test, or androidTest (or, in your case, experimental, since you already have a flavor by that name)

Step #2: Set up a source set for your newly-renamed flavor, using the name that you chose from Step #1

Step #3: Move your A.java from main to the source set that you created in Step #2

The main source set is for code that will be used across all product flavors and build types. You cannot change that by attempting to turn main into a product flavor.

Note that on Android Studio 3.2, it appears that attempting to define a product flavor named main results in a build crash — at least, it is crashing when I tried reproducing this problem.

like image 138
CommonsWare Avatar answered Sep 16 '25 18:09

CommonsWare