I have an android app with 2 flavors: -flavor1 -flavor2
My directory tree is :
/src/main /src/flavor1 /src/flavor2
main has the default source set. flavor1, and flavor2 directories have their own source set, that gradle gets automatically. If I add a resource file into a flavour directory it overrides it from main, and works fine.
However, if I add a java class into flavor2 to override another one from "main", (for example an activity that needs to be different in flavor2), it doesn't replace it and it says there's a duplicated class in my project. I only got this to work by deleting the class from "main" directory and adding it to all flavors folders.
Is there a way I can override java classes just like resource files ? why does this happen?
A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.
Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.
To change the build variant Android Studio uses, select Build > Select Build Variant in the menu bar. For projects without native/C++ code, the Build Variants panel has two columns: Module and Active Build Variant.
NOTE: By default, the Android Studio will generate "debug" and "release" Build Types for your project. So, to change a Build Type, all you need to do is just select your Build Type from the Build Variant and after the project sync, you are good to go.
Is there a way I can override java classes just like resource files ?
No. Xavier Ducrohet from the Android tools team discussed some of this briefly here.
There are ways of accomplishing what you want though- more on that in a second.
why does this happen?
Your Java code needs to compile. Assuming you reference a class Foo
from other classes in your main
source set, that Foo
class has to have the same public API regardless of what flavor is being compiled. If you were able to completely replace the main Foo
with a flavor-specific Foo
, this might not hold true. This is of course not an impossible problem to solve with the tools, but the current build tools' solution is to simply not let you do that.
Of course with resources such as images, this is completely different- as long as some version of the resource exists, Android can use it just fine.
You can, however, create flavor-specific implementations of a class to use within your main
source set. The easiest way is to simply put just a Foo
in each flavor, and not provide a Foo
in your main
source set. For bonus points, create an interface for these implementations within your main
source set to ensure that the flavor-specific implementations conform to the same interface and won't cause build errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With