我正在导入项目:https://github.com/Kennyc1012/OpenImgur 并且在收到Imgurs客户端ID和API客户端密钥后,建立项目很好,但是然后尝试运行应用程序注意到错误:
在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
:
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文件的情况下,为什么会出现此错误?
答案 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_SECRET
和gradle.properties
。 (这不在回购中。)
我会创建一个gradle.properties
并放在与build.gradle
相同的目录中,如下所示:
API_CLIENT_ID = fd8drdsgdsgsd4
API_CLIENT_SECRET = 5d4f9adab5zdfhfdxzxdfbdbc72a18087ce
FABRIC_KEY = some key
编写项目时只需使用您自己的gradle.properties
轻松换出变量。