Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android base flavor without duplicate class error

I have few versions of my app: common version, say mainapp and few builds specific per customer, say custom1, custom2, custom3. And I want to have base flavor for all customX flavors.

I was trying to do like this:

Create 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 common java classes for all customX flavors, say MainActivity.java and SecondActivity.java. But in custom3 I need separate SecondActivity.java.

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

For now I have resolved this using class dupllication:

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

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

How to resolve this situation without code duplication?

like image 870
anber Avatar asked Nov 29 '14 09:11

anber


1 Answers

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

if(BuildConfig.FLAVOR.equals("mainapp")) {
  // mainapp code
} else {
  // coomon flavor code
}
like image 169
anber Avatar answered Sep 20 '22 17:09

anber