Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android flavors with different base themes

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?

like image 510
Andrew Avatar asked Dec 03 '15 14:12

Andrew


People also ask

How do you make different flavors on Android?

Creating Product Flavor is pretty much easy in Android Studio, we simply need to add productFlavors block inside the Android block in the application-level build. gradle file. It's that simple to create product flavors.

What is BuildConfig flavor?

BuildConfig.FLAVOR gives you combined product flavor. So if you have only one flavor dimension: productFlavors { normal { } admin { } } Then you can just check it: if (BuildConfig. FLAVOR.

What are Android Flavours?

Android Product Flavors are used to create different app versions. App versions can be free or paid. They can have different themes and texts. They can use different environments or APIs. Let's assign two product flavors free and paid in our application.

What is missingDimensionStrategy?

void missingDimensionStrategy ( String dimension, String requestedValue) Specifies a flavor that the plugin should try to use from a given dimension in a dependency. Android plugin 3.0. 0 and higher try to match each variant of your module with the same one from its dependencies.


1 Answers

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 and flavor2 uses same theme but flavor3. Then you can define extra source set for common resources, such as commonA (for flavor1, flavor2) and commonB(for flavor3):

...
android {
    ...
    productFlavors {
        flavor1{

        }
        flavor2{

        }
        flavor3{

        }
    }

    sourceSets.flavor1{
        res.srcDirs = ['res', 'src/commonA/res']
    }
    sourceSets.flavor2{
        res.srcDirs = ['res', 'src/commonA/res']
    }
    sourceSets.flavor3{
        res.srcDirs = ['res', 'src/commonB/res']
    }
}

Also you should create the folders src/commanA and src/commonB then put your common resources into their res folders.

like image 171
Devrim Avatar answered Oct 03 '22 19:10

Devrim