How to generate buildConfigField with String type

AndroidAndroid Gradle-Plugin

Android Problem Overview


In my Android Studio project there are two build configuration with some buildConfigField:

    buildTypes {
    def SERVER_URL = "SERVER_URL"
    def APP_VERSION = "APP_VERSION"

    debug {
        buildConfigField "String", SERVER_URL, "http://dev.myserver.com"
        buildConfigField "String", APP_VERSION, "0.0.1"
    }

    release {
        buildConfigField "String", SERVER_URL, "https://myserver.com"
        buildConfigField "String", APP_VERSION, "0.0.1"

        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

I am getting and error as follows:

/path/to/generated/BuildConfig.java
    Error:(14, 47) error: ';' expected
    Error:(15, 47) error: ';' expected

the generated BuildConfig.java is as follows:

public final class BuildConfig {
    public static final boolean DEBUG = Boolean.parseBoolean("true");
    public static final String APPLICATION_ID = "com.mycuteoffice.mcoapp";
    public static final String BUILD_TYPE = "debug";
    public static final String FLAVOR = "";
    public static final int VERSION_CODE = 1;
    public static final String VERSION_NAME = "1.0";
    // Fields from build type: debug
    public static final String APP_VERSION = 0.0.1;
    public static final String SERVER_URL = http://dev.mycuteoffice.com;
}

I think the APP_VERSION and SERVER_URL are not getting generated properly as being String type they do not have quotes.

I am not sure why it is being generated in such a way. Please let me know how can I resolve this issues.

Android Solutions


Solution 1 - Android

String type build config fields should be declared like this:

buildConfigField "String", "SERVER_URL", "\"http://dev.myserver.com\""

the field name in quotes, the field value in escaped quotes additionally.

Solution 2 - Android

Why everybody is so mad about escaping double quotes? Looks ugly! This is Groovy, guys, you can just mix single and double quotes:

buildConfigField "String", 'SERVER_URL', '"http://dev.myserver.com"'
buildConfigField "String", 'APP_VERSION', '"0.0.1"'

Solution 3 - Android

If by "resolving the issues" you mean not having to double quote literals, I haven't come across anything as it seems to be working as designed.

I've been experimenting with moving the literals into "gradle.properties" as a workaround, turning potentially multiple ugly lines into one ugly line.

Like so:

buildTypes {
def SERVER_URL = "SERVER_URL"
def APP_VERSION = "APP_VERSION"

def CONFIG = { k -> "\"${project.properties.get(k)}\"" }

debug {
    buildConfigField "String", SERVER_URL, CONFIG("debug.server.url")
    buildConfigField "String", APP_VERSION, CONFIG("version")
}

release {
    buildConfigField "String", SERVER_URL, CONFIG("release.server.url")
    buildConfigField "String", APP_VERSION, CONFIG("version")

    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

gradle.properties

version=0.1.1
...
debug.server.url=http://dev.myserver.com
...
release.server.url=http://myserver.com
...

Further thoughts:


def CONFIG = { b,k -> "\"${project.properties.get(b+'.'+k)}\"" }
def CONFIG_DEBUG = { k -> CONFIG('debug',k) }
def CONFIG_RELEASE = { k -> CONFIG('release',k) }

def CONFIG = { b,k -> "\"${project.properties.get(b+'.'+k)}\"" }
def CONFIG_INT = { b,k -> "${project.properties.get(b+'.'+k)}" }
...

Solution 4 - Android

Use

 buildConfigField "String", "FILE_NAME", "\"{$fileName}\"" 

for variable. Reference from here

Solution 5 - Android

Escape your string quotes:

buildConfigField "String", 'SERVER_URL', "\"http://dev.myserver.com\""
buildConfigField "String", 'APP_VERSION', "\"0.0.1\""

Solution 6 - Android

I was confused as well. But there is a sense - "String" defines the field's type, whereas the field value isn't get quoted automatically in order to allow us to use expressions here:

buildConfigField "String", "TEST", "new Integer(10).toString()"

Otherwise, it wouldn't be possible.

Solution 7 - Android

We should scape our Gradle constant defined in our Gradle properties or somewhere else:

buildConfigField "String", "CONSTANT_NAME", "\"${CONSTANT_VALUE}\""

Where CONSTANT_VALUE is defined in our gradle.properties or somewhere else:

CONSTANT_VALUE=string_goes_here

It applies the same way when getting constants gotten from our environment:

buildConfigField "String", "CONSTANT_NAME", "\"${System.getenv('PATH')}\""

The most voted solution works in case we just need to add a String manually, this solution just goes a step further.

Solution 8 - Android

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

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

Solution 9 - Android

I need the variable in buildConfigField and manifestPaceholder. To solve this I do

def appAuthScheme= "appauth.myscheme"
buildConfigField 'String', 'APP_AUTH_SCHEME',"\"$appAuthScheme\""

manifestPlaceholders = [lowerApplicationId : applicationId.toLowerCase(),
                        appAuthRedirectScheme : appAuthScheme]

BuildConfig.APP_AUTH_SCHEME is a String !

Solution 10 - Android

Only "my stuff" worked for me. And I have all sorts of weird characters in my stuff.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbdullahView Question on Stackoverflow
Solution 1 - AndroidVladyslav MatviienkoView Answer on Stackoverflow
Solution 2 - AndroidmadheadView Answer on Stackoverflow
Solution 3 - AndroidPrimexxView Answer on Stackoverflow
Solution 4 - AndroidAdolf DsilvaView Answer on Stackoverflow
Solution 5 - AndroidSimasView Answer on Stackoverflow
Solution 6 - AndroidgeigerView Answer on Stackoverflow
Solution 7 - AndroidcesardsView Answer on Stackoverflow
Solution 8 - Androidyitai weiView Answer on Stackoverflow
Solution 9 - AndroidGugelhupfView Answer on Stackoverflow
Solution 10 - AndroidSevastyan SavanyukView Answer on Stackoverflow