我正在尝试根据flavor buildType定义一个buildConfigVariable.理想情况下,这就是我想要的
productFlavors { strawberry { buildConfigField "String","WS_API_KEY",name + variant.buildType.name } ... more flavors .. }
名称确实包含“草莓”,但我不知道是否可以访问该变体的buildType.
放置在Android关闭之外,我可以访问BuildType和变体,但是我无法调用buildConfigField
android.applicationVariants.all { variant -> println "****************************" println "variant: ${variant.name}" println "flavor: ${variant.flavorName}" println "****************************" if (variant.buildType.name == 'release') { if (variant.flavorName == 'strawberry') { buildConfigField "String",'"strawberry_release"' } else { buildConfigField "String",'"chocolate_release"' } } else if(variant.buildType.name == 'debug') { if (variant.flavorName == 'strawberry') { buildConfigField "String",'"strawberry_debug"' } else { buildConfigField "String",'"chocolate_debug"' } } **************************** variant: strawberryRelease flavor: strawberry **************************** org.gradle.api.internal.MissingMethodException: Could not find method buildConfigField() for arguments [String,WS_API_KEY,"strawberry_release"]
我可以轻松地创建一个Java工厂,并根据一些BuildConfig常量返回相应的API_KEY,但我宁愿保持代码配置不可知.
解决方法
Edit2:0.14.2之后的版本将允许这样做:
applicationVariants.all { variant -> variant.buildConfigField "int","VALUE","1" }
所以你可以做这样的事情(以匹配原来的问题):
applicationVariants.all { variant -> variant.buildConfigField "String",variant.productFlavors.get(0).name + '_' + variant.buildType.name }
编辑:目前不可能.缺少API.
错误:https://code.google.com/p/android/issues/detail?id=67416
尝试这个:
applicationVariants.all { variant -> variant.mergedFlavor.buildConfigField "String","NAME",'"VALUE"' }