I've an android project with multiple flavors that I'm working on.
This works fine and I can customize elements of the app such as colors and string resources.
I'm wanting to make it so that some of the flavors are based on the AppCompat light theme and some on the AppCompat dark theme.
I know I could do this by repeating all the <items>
in my style and in each flavor setting the theme to point to each one in a custom manifest for each app but that seems overkill.
Is there some easy way to set up a theme as such
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/primary_colour_tone1</item>
...lots more settings
</style>
But instead of directly pointing to Theme.AppCompat.NoActionBar point to a reference that in each gradle build can be set differently?
So somethings like:
<style name="AppTheme" parent="**Theme reference here**">
<item name="colorPrimary">@color/primary_colour_tone1</item>
...lots more settings
</style>
I've tried using things like @string/theme_name but that doesn't work, is there a simple way to do this?
You can define
sourceSets
for common resources (also sources) for one or more flavors which means you don't have to repeat all resources for all flavors.For instance, you have 3 flavors.
flavor1
andflavor2
uses same theme butflavor3
. Then you can define extra source set for common resources, such ascommonA
(for flavor1, flavor2) andcommonB
(for flavor3):Also you should create the folders
src/commanA
andsrc/commonB
then put your common resources into theirres
folders.better way to create a base style, that contains the default values, then in the main source style and now style extending it (but likely doing nothing). In the flavor you can then extend it and override what you need.