Android BuildConfig字段错误地生成了字符串 [英] Android BuildConfig Field generating String incorrectly

查看:159
本文介绍了Android BuildConfig字段错误地生成了字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始新项目的最后一天,我在构建配置中创建了一些变量,这是一种很好的做法,可以在发行版和调试版之间分别进行如下处理:

The last day starting a new project I was creating some variables in the build config as a good practice to handle them separately between release and debug builds like this:

...
buildTypes {
    release {
        minifyEnabled false
        buildConfigField("String", "PARSE_APP_ID", "xxxxxxxxxxxxxxxxxxxxxxxxxxx")
        buildConfigField("String", "PARSE_CLIENT_ID", "xxxxxxxxxxxxxxxxxxxxxxxxxx")
        buildConfigField("String", "FACEBOOK_APP_ID", "999999999999999")
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug{
        minifyEnabled false
        buildConfigField("String", "PARSE_APP_ID", "xxxxxxxxxxxxxxxxxxxxxxxxxxx")
        buildConfigField("String", "PARSE_CLIENT_ID", "xxxxxxxxxxxxxxxxxxxxxxxxxx")
        buildConfigField("String", "FACEBOOK_APP_ID", "999999999999999")
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
...

问题是它没有正确创建字符串,而是直接打印字符串而没有引号,如下所示:

The problem is that it was not creating the Strings correctly it was printing the strings directly without the quotation marks like this:

public final class BuildConfig {
  .....
  // Fields from build type: debug
  public static final String FACEBOOK_APP_ID = 99999999999999999;
  public static final String PARSE_APP_ID = xxxxxxxxxxxxxxxxxxxxxxxx;
  public static final String PARSE_CLIENT_ID = xxxxxxxxxxxxxxxxxxxxxxxxx;
}

显然,这会给您带来编译错误,我试图在stackoverflow中找到如何解决此问题的方法,因为这在我的构建中引起了错误,还没有找到任何相关信息.如何使gradle用引号正确打印字符串?

Obviously this will give you compile errors, I tried to find here in stackoverflow how to fix this since this was causing erros in my build, haven't found anything about this. How to make gradle print the Strings correctly with the quotation marks?

推荐答案

写完此后,我发现了方法,似乎值的打印方式与定义的方式相同,因此,如果对此添加转义的引号,它将起作用,例如:

After writing this I have found the way, it seems that the values are printed in the same way they are defined so if you add escaped quotation marks to this it will work, example:

buildTypes {
        release {
            minifyEnabled false
            buildConfigField("String", "PARSE_APP_ID", '"xxxxxxxxxxxxxxxxxxxxxxxx"')
            buildConfigField("String", "PARSE_CLIENT_ID", '"xxxxxxxxxxxxxxxxxxxxxxxxx"')
            buildConfigField("String", "FACEBOOK_APP_ID", '"999999999999999"')
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

之后,重新构建,它将正确打印Strings值:

After that, Rebuild and it will print the Strings values correctly:

public final class BuildConfig {
  ....
  // Fields from build type: debug
  public static final String FACEBOOK_APP_ID = "999999999999999999";
  public static final String PARSE_APP_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
  public static final String PARSE_CLIENT_ID = "xxxxxxxxxxxxxxxxxxxxxxxx";
}

这篇关于Android BuildConfig字段错误地生成了字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆