Android base flavor without duplicate class error

I have several versions of my application: the general version, say mainapp and several assemblies for each client, for example custom1 , custom2 , custom3 . And I want to have a basic taste for all the tastes of customX.

I tried to do like this:

Create a project structure:

 app\src\main app\src\mainapp app\src\commonflavor app\src\custom3 

And config:

 productFlavors { mainapp { } custom1 { } custom2 { } custom3 { } } sourceSets { custom1 { java.srcDirs = ['src/commonflavor/java'] } custom2 { java.srcDirs = ['src/commonflavor/java'] } custom3 { java.srcDirs = ['src/commonflavor/java', 'src/custom3/java'] } } 

In commonflavor I put the general java classes for all customX flutes, say MainActivity.java and SecondActivity.java . But in custom3 I need a separate SecondActivity.java .

In this case, I have the error error: duplicate class: SecondActivity from src/commonflavor/java and src/custom3/java .

I have currently resolved this with class duplication:

 sourceSets { custom1 { java.srcDirs = ['src/commonflavor/java'] } custom2 { java.srcDirs = ['src/commonflavor/java'] } custom3 { java.srcDirs = ['src/custom3/java'] } } 

I copied src/commonflavor/java from srcDirs and copied MainActivity.java in custom3 . The problem is that I have 2 clones of the MainActivity.java file in src/commonflavor/java and src/custom3/java

How to resolve this situation without duplication of code?

+5
source share
1 answer

The best solution I founded is to write all classes in app \ src \ main and add where necessary:

 if(BuildConfig.FLAVOR.equals("mainapp")) { // mainapp code } else { // coomon flavor code } 
+2
source

Source: https://habr.com/ru/post/1207926/


All Articles