找不到Gradle DSL方法:'buildConfigField()'

时间:2016-04-26 16:26:47

标签: android gradle

我有这个gradle错误

错误:(21,0)未找到Gradle DSL方法:'buildConfigField()' 可能的原因:

<ul><li>The project 'Sunshine' may be using a version of Gradle that does not contain the method.

<a href="openGradleSettings">Gradle settings</a></li><li>The build file may be missing a Gradle plugin.

<a href="apply.gradle.plugin">Apply Gradle plugin</a></li>

我的build.gradle代码在这里

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.boxerrebellion.sunshine.app"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildTypes.each {
         it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.3.0'
}
谁有任何想法? 提前谢谢

4 个答案:

答案 0 :(得分:2)

buildConfigField定义如下

void buildConfigField(String type, String name, String value)

它需要一个类型,字段的名称和值。如果有前两个,而不是第三个。您可以阅读更多相关信息here

答案 1 :(得分:0)

在build.gradle代码上添加'OPEN_WEATHER_MAP_API_KEY'值

 buildTypes.each {
         it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', 'xxxx'
    }

答案 2 :(得分:0)

只需在list块中添加它:

seq

答案 3 :(得分:-1)

复制粘贴MyOpenWeatherAPIKey

时,您可能错过了parenthisis

最初它看起来像

buildTypes.each {
    it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', MyOpenWeatherAPIKey
}

https://home.openweathermap.org/api_keys复制MyOpenWeatherAPIKey后,它应该

buildTypes.each {
    it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
}