Android flavors with different base themes

后端 未结 3 958
死守一世寂寞
死守一世寂寞 2021-01-05 22:02

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.

相关标签:
3条回答
  • 2021-01-05 22:13

    Just FYI in case anyone is looking for an easy way to do this going forward.

    In your manifest, update your theme to something like this...

    android:theme="${appTheme}"
    

    Then in your app.gradle do something like this...

    productFlavors {
        flavorA{
            manifestPlaceholders = [appTheme: "@style/AppThemeA"]
        }
        flavorB{
            manifestPlaceholders = [appTheme: "@style/AppThemeB"]
        }
    }
    

    Then you can use whatever styles you want there and they will tie to the flavor selected.

    0 讨论(0)
  • 2021-01-05 22:19

    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.

    0 讨论(0)
  • 2021-01-05 22:28

    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.

    0 讨论(0)
提交回复
热议问题