我很难找到一种方法,使用Gradle为我的BuildConfig添加多行.看来,当我第二次调用buildConfig时,第一次消失.
我最初是从另一个地方添加这个buildConfig,但是如果我这样做的话,我能够创建一个最小的可重现测试:
buildTypes { debug { versionNameSuffix "-DEBUG" buildConfig "public static final int THING_ONE = 1;" buildConfig "public static final int THING_TWO = 2;" } release { zipAlign true buildConfig "public static final int THING_ONE = 3;" buildConfig "public static final int THING_TWO = 4;" } }
然后当我尝试在代码中使用它时:
public class Thing { public static final int THING = com.example.BuildConfig.THING_ONE + com.example.BuildConfig.THING_TWO; }
我会收到这个错误:
/Example/src/main/java/com/example/Thing.java:2: cannot find symbol symbol : variable THING_ONE location: class com.example.BuildConfig public static final int THING = com.example.BuildConfig.THING_ONE + com.example.BuildConfig.THING_TWO;
有没有办法为buildConlavor或buildType添加多个不同的行到buildConfig(使用多次调用buildConfig – 而不是多行字符串)?
解决方法
正如@CommonsWare指出的那样,从Gradle 1.9(Android Studio 0.4.0)开始,你必须声明你的BuildConfig字段:
buildTypes { debug { versionNameSuffix "-DEBUG" buildConfigField "int","THING_ONE","1" buildConfigField "int","THING_TWO","2" } }