Android BuildConfig字段生成字符串不正确

时间:2016-03-07 18:45:33

标签: android gradle

开始一个新项目的最后一天我在构建配置中创建了一些变量,作为在发布和调试版本之间单独处理它们的一个好习惯,如下所示:

...
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'
    }
}
...

问题是它没有正确创建字符串它是直接打印字符串而没有这样的引号:

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打印字符串?

2 个答案:

答案 0 :(得分:9)

写完之后我找到了方法,看起来这些值的打印方式与它们的定义方式相同,所以如果你向它添加转义引号就可以了,例如:

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'
        }

之后,Rebuild将正确打印Strings值:

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";
}

答案 1 :(得分:1)

在应用程序build.gradle中

def buildTimeAndVersion = releaseTime() + "-" + getSvnVersion()    
buildTypes {
    debug {
        signingConfig signingConfigs.config
        buildConfigField "String", 'BIULD_TIME', "\"${buildTimeAndVersion}\""
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
....
}

static def releaseTime() {
return new Date().format("yyyyMMdd", TimeZone.getDefault())
}

def getSvnVersion() {
def pro = ("svnversion -c " + getBuildDir().parent).execute()
pro.waitFor()
def version = pro.in.text
Pattern p = Pattern.compile("(\\d+\\:)?(\\d+)\\D?")
Matcher m = p.matcher(version)
if (m.find()) {
    version = m.group(m.groupCount())
}
try {
    return version
} catch (e) {
    println e.getMessage()
}
return 0
}

然后在BuildConfig文件中

public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true"); 
public static final String APPLICATION_ID = "xxx.xxxxxxxx.xxx"; 
public static final String BUILD_TYPE = "debug";  
public static final String FLAVOR = "";  
public static final int VERSION_CODE = 53;  
public static final String VERSION_NAME = "5.4.4";  
// Fields from build type: debug  
public static final String BIULD_TIME = "20181030-2595";  
}