[英]ERROR: Gradle sync failed: Could not get unknown property 'API_KEY' for DefaultConfig_Decorated
ERROR: Could not get unknown property 'API_KEY' for DefaultConfig_Decorated{name=main, dimension=null, minSdkVersion=null, targetSdkVersion=null, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null, renderscriptNdkModeEnabled=null, versionCode=null, versionName=null, applicationId=null, testApplicationId=null, testInstrumentationRunner=null, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=null, mBuildConfigFields={}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}, mWearAppUnbundled=null} of type com.android.build.gradle.internal.dsl.DefaultConfig.
错误:无法为 DefaultConfig_Decorated{name=main、dimension=null、minSdkVersion=null、targetSdkVersion=null、renderscriptTargetApi=null、renderscriptSupportModeEnabled=null、renderscriptSupportModeBlasEnabled=null、renderscriptNdkModeEnabled=null、versionCode=null、versionName 获取未知属性“API_KEY” =null、applicationId=null、testApplicationId=null、testInstrumentationRunner=null、testInstrumentationRunnerArguments={}、testHandleProfiling=null、testFunctionalTest=null、signingConfig=null、resConfig=null、mBuildConfigFields={}、mResValues={}、mProguardFiles=[] , mConsumerProguardFiles=[], mManifestPlaceholders={}, mWearAppUnbundled=null} 类型为 com.android.build.gradle.internal.dsl.DefaultConfig。 apply plugin: 'com.android.application'
应用插件:'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion '27.0.2'
defaultConfig {
buildConfigField("String", "API_KEY", API_KEY) //error here
buildConfigField("String", "ER_API_KEY", ER_API_KEY)
applicationId "com.gpads.gautham.imagetotextanalysis"
minSdkVersion 15
targetSdkVersion 27
versionCode 2
versionName "2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
// ... other values
}
把这个 buildConfigField("String", "API_KEY", API_KEY) 改成这个
buildConfigField "String", "API_KEY", "\" API_KEY\""
To avoid any errors with your code add this snippet, found this as a solution for my movie project:为了避免您的代码出现任何错误,请添加此代码段,发现这是我的电影项目的解决方案:
def getProperty(String filename, String propName) {
def propsFile = rootProject.file(filename)
if (propsFile.exists()) {
def props = new Properties()
props.load(new FileInputStream(propsFile))
if (props[propName] != null) {
return props[propName]
} else {
print("No such property " + propName + " in file " + filename);
}
} else {
print(filename + " does not exist!")
}
}
android {
compileSdkVersion 27
buildToolsVersion '27.0.2'
defaultConfig {
buildConfigField "String", "API_KEY", "\"${getProperty("local.properties", API_KEY)}\""
buildConfigField "String", "\"${getProperty("local.properties", ER_API_KEY)}\""
applicationId "com.gpads.gautham.imagetotextanalysis"
minSdkVersion 15
targetSdkVersion 27
versionCode 2
versionName "2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
The error is that you are trying to use something you did not define.错误是您正在尝试使用您没有定义的东西。
As a solution, you have several ways to initialize the configuration field:作为解决方案,您可以通过多种方式来初始化配置字段:
. . In the same scope it could be:
在同一范围内,它可能是:
Look at the following code where API_KEY_2
is defined.查看以下代码,其中定义了
API_KEY_2
。
android {
//...
defaultConfig {
//...
def API_KEY_2 = "API_KEY_2"
buildConfigField("String", "API_KEY", API_KEY_2)
}
}
. . Or in a global scope:
或者在全球范围内:
class Globals {
static String API_KEY_2 = "API_KEY_2"
}
android {
//...
defaultConfig {
//...
def API_KEY_2 = "API_KEY_2"
buildConfigField("String", "API_KEY", API_KEY_2)
}
}
Note: I don't recommend using BuildConfig.SOMETHING
to initialize a buildConfigField注意:我不建议使用
BuildConfig.SOMETHING
来初始化 buildConfigField
GL GL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.