【问题标题】:ERROR: Gradle sync failed: Could not get unknown property 'API_KEY' for DefaultConfig_Decorated错误:Gradle 同步失败:无法获取 DefaultConfig_Decorated 的未知属性“API_KEY”
【发布时间】:2018-08-09 09:23:37
【问题描述】:

错误:无法获取 DefaultConfig_Decorated{name=main, dimension=null, minSdkVersion=null, targetSdkVersion=null, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null, renderscriptNdkModeEnabled=null, versionCode= 的未知属性“API_KEY” 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} 类型为 com.android.build.gradle.internal.dsl.DefaultConfig。 应用插件:'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
}

【问题讨论】:

  • 包 com.gpads.gautham.imagetotextanalysis; public final class BuildConfig { public static final boolean DEBUG = Boolean.parseBoolean("true");公共静态最终字符串 APPLICATION_ID = "com.gpads.gautham.imagetotextanalysis";公共静态最终字符串BUILD_TYPE =“调试”;公共静态最终字符串 FLAVOR = "";公共静态最终 int VERSION_CODE = 2;公共静态最终字符串 VERSION_NAME = "2.0"; } @HemantParmar 这是我的 BuildConfig.java

标签: java android android-studio gradle build.gradle


【解决方案1】:

更改此 buildConfigField("String", "API_KEY", API_KEY) 进入这个

    buildConfigField "String", "API_KEY", "\" API_KEY\"" 

【讨论】:

    【解决方案2】:

    为避免您的代码出现任何错误,请添加此 sn-p,发现这是我的电影项目的解决方案:

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

    【讨论】:

    • 这是唯一对我有用的解决方案,谢谢
    【解决方案3】:

    错误在于您正在尝试使用您未定义的东西。

    作为一种解决方案,您有几种方法来初始化配置字段:


    。在相同的范围内,它可能是:

    看看下面定义API_KEY_2的代码。

    android {
        //...
        defaultConfig {
            //...
            def API_KEY_2 = "API_KEY_2"
            buildConfigField("String", "API_KEY", API_KEY_2)
        }
    }
    

    。或者在全局范围内:

    build.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)
        }
    }
    

    注意:我不建议使用 BuildConfig.SOMETHING 来初始化 buildConfigField

    GL

    【讨论】:

      猜你喜欢
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 2020-09-06
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      相关资源
      最近更新 更多