Home » Android » Gradle sync failed: Could not find method buildConfigField() android studio

Gradle sync failed: Could not find method buildConfigField() android studio

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am working on an online course and i had problem with this code

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

but i got error like below:

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>
How to&Answers:

If you want to add fields to all build types, you should use this:

android {
    ...
    defaultConfig {
        ...

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

Answer:

use

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

instead of

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

if you not define DARK_SKY_KEY in file then use “DARK_SKY_KEY”

might be helpful to understand.