Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BuildConfig Field generating String incorrectly

Tags:

android

gradle

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?

like image 904
Eefret Avatar asked Mar 07 '16 18:03

Eefret


2 Answers

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";  
}
like image 95
yitai wei Avatar answered Nov 02 '22 04:11

yitai wei


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";
}
like image 41
Eefret Avatar answered Nov 02 '22 02:11

Eefret