Reputation: 5243
If you need flavor you should go to build gradle
and add the flavors that you need
Like this
productFlavors {
mock {
applicationIdSuffix = ".mock"
}
prod {}
}
and then you need to create corresponding dir like this /src/prod/java/
How I thought it should work, according to build variant that was choosen for example prodDebug
androidStudio will take as a base main source and substitute coresponding classes from dir according to choosen build variant.
But then I found this snippet which said next
Files in the flavor-specific folders do not replace files in the main source set. Trying to do that will result in a duplicate class exception. This is a common misconception because it's how resources are merged.
Upvotes: 0
Views: 1228
Reputation: 4775
Ok, so with basic configuration with flavors, you have two kinds of source sets:
main
source setmock
and prod
With standard buildTypes
configuration (debug and release), this gives you the following build variants (combinations of build types and product flavors):
Each one of them uses every source set that corresponds with flavor/type name and the main set, so for example, the prodRelease will use all of the following source sets at once:
Effectively, the build system will 'merge' all of these into one source set, and that means if there are classes with the same path and name in these sets, a name clash occurs and compiler will fail.
The way to use source sets correctly is to omit the class that you need to be different for each set from the main set, but instead provide it with all the sets for each flavor / each buildType, for example:
This behavior doesn't limit to classes, you can use flavor or type specific resources, AndroidManifest.xml files and just about anything that goes into the source dir.
Tip: In Android Studio you can see in the 'project files' section which files will be chosen to compile for a specific variant. To switch build variants, hit Cmd+Shift+A (mac keymap) and search for Build Variants phrase. It usually shows also on the left bottom side of the Android Studio window.
Upvotes: 3
Reputation: 40193
The code from the main
source set will always make it into the APK. The source files in other source sets will only be merged if the correct build variant is used. For example, you can create two files:
Depending on whether you're building prod
or mock
variant, one of those classes will be compiled and packaged with the APK. Same works for debug
and release
: you can have code and resources that are only packaged into debug or release versions of the app.
Upvotes: 2