Android using Gradle Build flavors in the code like an if case

前端 未结 3 1257
礼貌的吻别
礼貌的吻别 2020-12-02 10:53

I\'m trying to work with build flavors. In my build.gradle I\'ve defined 2 flavors, a normal flavor and an admin flavor.

Basicly, the admin flavor has an extra button

3条回答
  •  不思量自难忘°
    2020-12-02 11:48

    To avoid plain string in the condition, you can define a boolean property:

    productFlavors {
        normal {
            flavorDimension "access"
            buildConfigField 'boolean', 'IS_ADMIN', 'false'
        }
    
        admin {
            flavorDimension "access"
            buildConfigField 'boolean', 'IS_ADMIN', 'true'
        }
    }
    

    Then you can use it like this:

    if (BuildConfig.IS_ADMIN) {
        ...
    }
    

提交回复
热议问题