简体   繁体   English

错误:Gradle 同步失败:无法为 DefaultConfig_Decorated 获取未知属性“API_KEY”

[英]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:或者在全球范围内:

build.gradle构建.gradle

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.

相关问题 错误:(134, 0) 无法为 org.gradle.api.internal.FactoryNamedDomainObjectContainer 类型的 SigningConfig 容器获取未知属性“释放”。 - Error:(134, 0) Could not get unknown property 'release' for SigningConfig container of type org.gradle.api.internal.FactoryNamedDomainObjectContainer. Gradle Build Error无法获得未知属性supportVersion - Gradle Build Error Could not get unknown property supportVersion 无法获取未知属性“运行时”Gradle 7.0 - Could not get unknown property "runtime" Gradle 7.0 无法在Gradle中获得任务的未知属性? - Could not get unknown property for task in Gradle? 如何在Android Transliteration中获取API_KEY - How to get API_KEY in Android Transliteration Android Studio:“无法获得类型为 org.gradle.api.Project 的项目的未知属性‘VERSION_NAME’” - Android Studio : “Could not get unknown property 'VERSION_NAME' for project of type org.gradle.api.Project” 无法为org.gradle.api.Project类型的根项目“MyApplication3”获取未知属性“删除” - Could not get unknown property 'Delete' for root project 'MyApplication3' of type org.gradle.api.Project 无法获取 org.gradle.api.internal.artifacts.configurations 类型的配置容器的未知属性“运行时” - Could not get unknown property 'runtime' for configuration container of type org.gradle.api.internal.artifacts.configurations 无法为 org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler 类型的 object 获取未知属性“实现” - Could not get unknown property 'implementation' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler 无法在org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@397740e0上找到属性'compile' - Could not find property 'compile' on org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@397740e0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM