Java Gradle buildConfigField非法转发引用
这不起作用,因为在生成的Java Gradle buildConfigField非法转发引用,java,android,gradle,android-gradle-plugin,Java,Android,Gradle,Android Gradle Plugin,这不起作用,因为在生成的BuildConfig中,STORE最终是在UNLOCKED和PLAYSTORE之前定义的。我该如何做才能有所不同 build.gradle android { defaultConfig { buildConfigField 'int', 'UNLOCKED', '0' buildConfigField 'int', 'PLAYSTORE', '1' } productFlavors { unlo
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风格)
示例用例
if(BuildConfig.STORE == BuildConfig.PLAYSTORE)
validatePurchaseOnPlay();
一种解决方案是在我的
应用程序类中定义常量,然后使用
buildConfigField 'int', 'STORE', 'MyApp.UNLOCKED'
一种解决方案是在我的应用程序类中定义常量,然后使用
buildConfigField 'int', 'STORE', 'MyApp.UNLOCKED'
尝试将存储构建配置字段声明更改为:
buildConfigField'int'、'STORE'、'BuildConfig.UNLOCKED'
尝试将存储构建配置字段声明更改为:
buildConfigField'int'、'STORE'、'BuildConfig.UNLOCKED'
您可以使用以下方法实现值之间的一致性:
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
您可以使用以下方法实现值之间的一致性:
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
这里的用例是什么?这可能有助于我们找到解决办法。@stkent在问题中添加了一个用例。我猜生成配置字段将始终以“最特定到最不特定”的方式生成,以允许更特定的配置闭包覆盖在不太特定的配置闭包中定义的字段。运行显而易见的测试支持这一想法。考虑到这一点,我认为仅使用build-config字段无法实现您所期望的目标。你建议的解决方案对我来说似乎没问题;您可能可以在任何类中找到常量,而不仅仅是自定义应用程序子类?@stkent是的,但是如果可能的话,我喜欢在gradle脚本中定义和使用常量。我想这是松耦合的问题。这里的用例是什么?这可能有助于我们找到解决办法。@stkent在问题中添加了一个用例。我猜生成配置字段将始终以“最特定到最不特定”的方式生成,以允许更特定的配置闭包覆盖在不太特定的配置闭包中定义的字段。运行显而易见的测试支持这一想法。考虑到这一点,我认为仅使用build-config字段无法实现您所期望的目标。你建议的解决方案对我来说似乎没问题;您可能可以在任何类中找到常量,而不仅仅是自定义应用程序子类?@stkent是的,但是如果可能的话,我喜欢在gradle脚本中定义和使用常量。我想这是一个松散耦合的问题。