这不起作用,因为在生成的BuildConfig
中,STORE
最终会在UNLOCKED
和PLAYSTORE
之前定义。我怎么能这样做?
的build.gradle
android {
defaultConfig {
buildConfigField 'int', 'UNLOCKED', '0'
buildConfigField 'int', 'PLAYSTORE', '1'
}
productFlavors {
unlocked {
buildConfigField 'int', 'STORE', 'UNLOCKED'
}
playStore {
buildConfigField 'int', 'STORE', 'PLAYSTORE'
}
}
}
BuildConfig.java (生成,playStore风味)
// Fields from product flavor: playStore
public static final int STORE = PLAYSTORE; // ERROR
// Fields from default config.
public static final int PLAYSTORE = 1;
public static final int UNLOCKED = 0;
示例用例
if(BuildConfig.STORE == BuildConfig.PLAYSTORE)
validatePurchaseOnPlay();
答案 0 :(得分:2)
尝试将STORE构建配置字段声明更改为:
buildConfigField 'int', 'STORE', 'BuildConfig.UNLOCKED'
答案 1 :(得分:0)
一种解决方案是在我的Application
类中定义常量,然后使用
buildConfigField 'int', 'STORE', 'MyApp.UNLOCKED'
答案 2 :(得分:0)
您可以使用以下方法实现值之间的一致性:
android {
def storeUnlocked = '0'
def storePlaystore = '1'
defaultConfig {
buildConfigField 'int', 'UNLOCKED', storeUnlocked
buildConfigField 'int', 'PLAYSTORE', storePlaystore
}
productFlavors {
unlocked {
buildConfigField 'int', 'STORE', storeUnlocked
}
playStore {
buildConfigField 'int', 'STORE', storePlaystore
}
}
}
如果stateUnlocked或statePlaystore发生更改,生成的BuildConfig和值将保持一致。
唯一缺少的部分是BuildConfig源代码不会显示关系,但如果您正确命名字段,这并不重要:
STORE
STORE_UNLOCKED
STORE_PLAYSTORE