androidandroid-gradle-pluginbuildconfig

BuildConfig Failure error ';' expected


I am importing the project: https://github.com/Kennyc1012/OpenImgur and after receiving Imgurs Client ID and API Client Secret, bulding the project is fine, but then attempting to run the app notes the errors:

enter image description here

Within the BuildConfig.java file:

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.kennyc.open.imgur";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 21;
  public static final String VERSION_NAME = "2.6.2";
  // Fields from default config.
  public static final String API_CLIENT_ID = fd8drdsgdsgsd4;
  public static final String API_CLIENT_SECRET = 5d4f9adab5zdfhfdxzxdfbdbc72a18087ce;
}

I had placed the API Client ID and Client Secret just as noted in the build.gradle:

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://dl.bintray.com/kennyc1012/maven' }
    maven { url "http://dl.bintray.com/amulyakhare/maven" }
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
    jcenter()
    flatDir {
        dirs 'libs'
    }
}

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2" // 22.0.1

    defaultConfig {
        applicationId "com.kennyc.open.imgur"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 21
        versionName "2.6.2"
        buildConfigField "String", "API_CLIENT_ID", "fd8drdsgdsgsd4"
        buildConfigField "String", "API_CLIENT_SECRET", "5d4f9adab5zdfhfdxzxdfbdbc72a18087ce"
    }

    buildTypes {

        release {
            ext.enableCrashlytics = true
            minifyEnabled false
            manifestPlaceholders = [fabric_key: ""]
        }

        debug {
            ext.enableCrashlytics = false
            minifyEnabled false
            manifestPlaceholders = [fabric_key: ""]
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

Yet error persists. Given the BuildConfig file is generated, why would this error occur?


Solution

  • Based on your BuildConfig.java, these Strings need to have quotes:

    public static final String API_CLIENT_ID = "fd8drdsgdsgsd4";
    public static final String API_CLIENT_SECRET = "5d4f9adab5zdfhfdxzxdfbdbc72a18087ce";
    

    The original project on Github looks like this:

    buildConfigField "String", "API_CLIENT_ID", project.API_CLIENT_ID
    buildConfigField "String", "API_CLIENT_SECRET", project.API_CLIENT_SECRET
    

    The project. means that API_CLIENT_ID and API_CLIENT_SECRET are defined in a gradle.properties file. (This is not located in the repo).

    I would create a gradle.properties and put in the same directory as the build.gradle, something like this:

    API_CLIENT_ID = fd8drdsgdsgsd4
    API_CLIENT_SECRET = 5d4f9adab5zdfhfdxzxdfbdbc72a18087ce
    FABRIC_KEY = some key
    

    The project is written to just easily swap out the variables with your own gradle.properties.