Gradle sync失败:无法找到方法buildConfigField()android studio

时间:2017-07-21 11:06:09

标签: android android-gradle build.gradle

我正在开设一个在线课程,我对此代码有疑问

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    buildTypes.each {
        it.buildConfigField 'String', 'b35f87cc72b82388ed9276e41fd55dd0', DARK_SKY_KEY
        it.buildConfigField 'String', 'b35f87cc72b82388ed9276e41fd55dd0', FLICKR_API_KEY
    }
}

但我收到如下错误:

Error:(22, 0) Could not find method buildConfigField() for arguments [String, b35f87cc72b82388ed9276e41fd55dd0, BuildType_Decorated{name=DARK_SKY_KEY, debuggable=false, testCoverageEnabled=false, jniDebuggable=false, pseudoLocalesEnabled=false, renderscriptDebuggable=false, renderscriptOptimLevel=3, minifyEnabled=false, zipAlignEnabled=true, signingConfig=null, embedMicroApp=true, mBuildConfigFields={}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}}] on BuildType_Decorated{name=debug, debuggable=true, testCoverageEnabled=false, jniDebuggable=false, pseudoLocalesEnabled=false, renderscriptDebuggable=false, renderscriptOptimLevel=3, minifyEnabled=false, zipAlignEnabled=true, signingConfig=SigningConfig_Decorated{name=debug, storeFile=C:\Users\pc\.android\debug.keystore, storePassword=android, keyAlias=AndroidDebugKey, keyPassword=android, storeType=C:\Users\pc\.android\debug.keystore, v1SigningEnabled=true, v2SigningEnabled=true}, embedMicroApp=false, mBuildConfigFields={}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}} of type com.android.build.gradle.internal.dsl.BuildType.
<a href="openFile:C:\Users\pc\Desktop\Weather-master\app\build.gradle">Open File</a>

2 个答案:

答案 0 :(得分:9)

如果要向所有构建类型添加字段,则应使用:

android {
    ...
    defaultConfig {
        ...

        buildConfigField "String", "DARK_SKY_KEY", '"b35f87cc72b82388ed9276e41fd55dd0"'
        buildConfigField "String", "FLICKR_API_KEY", '"b35f87cc72b82388ed9276e41fd55dd0"'    
    }
}

答案 1 :(得分:3)

使用

buildTypes {
release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
       }
  buildTypes.each {
            it.buildConfigField ("String", DARK_SKY_KEY, "\"b35f87cc72b82388ed9276e41fd55dd0\"")
            it.buildConfigField ("String", FLICKR_API_KEY, "\"b35f87cc72b82388ed9276e41fd55dd0\"")
                  }
}

而不是

buildTypes {
release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
 buildTypes.each {
    it.buildConfigField 'String', 'b35f87cc72b82388ed9276e41fd55dd0', DARK_SKY_KEY
    it.buildConfigField 'String', 'b35f87cc72b82388ed9276e41fd55dd0', FLICKR_API_KEY
         }
}

如果您未在文件中定义 DARK_SKY_KEY ,请使用&#34; DARK_SKY_KEY&#34;

可能有助于理解。