BuildConfig失败错误';'预期

时间:2015-04-27 00:55:49

标签: android android-gradle buildconfig

我正在导入项目:https://github.com/Kennyc1012/OpenImgur 并且在收到Imgurs客户端ID和API客户端密钥后,建立项目很好,但是然后尝试运行应用程序注意到错误:

enter image description here

在BuildConfig.java文件中:

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

我已按照build.gradle

中的说明放置了API客户端ID和客户端密钥
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
    }
}

然而错误仍然存​​在。在生成BuildConfig文件的情况下,为什么会出现此错误?

2 个答案:

答案 0 :(得分:5)

尝试像这样添加单引号:

buildConfigField "String", "API_CLIENT_ID", '"fd8drdsgdsgsd4"'
buildConfigField "String", "API_CLIENT_SECRET", '"5d4f9adab5zdfhfdxzxdfbdbc72a18087ce"'

添加单引号将在BuildConfig类生成后保持双引号不变

答案 1 :(得分:1)

根据您的BuildConfig.java,这些Strings需要有引号:

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

Github上的原始项目如下所示:

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

project.表示在API_CLIENT_ID文件中定义API_CLIENT_SECRETgradle.properties。 (这不在回购中。)

我会创建一个gradle.properties并放在与build.gradle相同的目录中,如下所示:

API_CLIENT_ID = fd8drdsgdsgsd4
API_CLIENT_SECRET = 5d4f9adab5zdfhfdxzxdfbdbc72a18087ce
FABRIC_KEY = some key

编写项目时只需使用您自己的gradle.properties轻松换出变量。