-->

Android BuildConfig Field generating String incorr

2020-08-26 11:05发布

问题:

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

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?

回答1:

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

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


回答2:

in app 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
}

then in BuildConfig file

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