Search code examples
androidandroid-build-flavors

Building different apps using Flavors - different code.


I'm building an app with several different flavors. My question is that if I use different code for activities based on flavor, do I need to include a copy of that code in all flavor directores, or if I just leave the copy of the code in the app/src/main/java directory, will all of the other flavors use the file in main/java if I do not include it in the flavor directories.

I have

app/src/main/java/com/mycompany/CodeA.java

and

app/src/flavor1/java/com/mycompany/CodeA.java

and no special source sets folder for flavor2, will flavor2 use the code file in app/src/main/java/com/mycompany/CodeA.java? Or do I need to create a source sets folder with a copy of the code in each flavor? I use different resource folders for a couple of flavors but for all other flavors, the resources folder defaults to the files in the app/src/main directories.


Solution

  • 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. - the quote from https://developer.android.com/studio/build/build-variants. This is exactly what you're asking. So the answer - it won't compile, you have to define the code file in both flavors and not in main.