BuildConfig 失败错误 ';'预期的

BuildConfig Failure error ';' expected

我正在导入项目:https://github.com/Kennyc1012/OpenImgur 在收到 Imgurs Client ID 和 API Client Secret 后,构建项目没问题,但随后尝试 运行 应用程序注意到错误:

在 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文件,为什么会出现这个错误呢?

根据你的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_IDAPI_CLIENT_SECRET定义在gradle.properties文件中。 (这不在回购协议中)。

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

API_CLIENT_ID = fd8drdsgdsgsd4
API_CLIENT_SECRET = 5d4f9adab5zdfhfdxzxdfbdbc72a18087ce
FABRIC_KEY = some key

编写该项目只是为了轻松地用您自己的变量替换变量 gradle.properties

尝试像这样添加单引号:

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

添加单引号会在 BuildConfig class 生成后保持双引号完整