简体   繁体   中英

I am getting an error in my build.gradle file

My build.gradle file looks like this:

 apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.tejas.otppassword"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
         each{ type ->
             type.buildConfigField 'String', 'API', SMS_API_KEY


         }
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {

            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

dependencies {
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.android.volley:volley:1.1.0'
}

The error in my Logcat looks like this:

 ERROR: Could not get unknown property 'SMS_API_KEY' for DefaultConfig_Decorated{name=main, dimension=null, minSdkVersion=DefaultApiVersion{mApiLevel=19, mCodename='null'}, targetSdkVersion=DefaultApiVersion{mApiLevel=29, mCodename='null'}, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null, renderscriptNdkModeEnabled=null, versionCode=1, versionName=1.0, applicationId=com.tejas.otppassword, 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.
Open File

I tried some other stuff regarding declaring the API_KEY which is declared as a constant in a Constant class. I even tried to put double quote along with single but it didn't help at all.

Use this:

buildConfigField "String", "API", "\"YOUR_SMS_API_KEY\"" 
buildConfigField("String", "BUILD_TIME", "\"${minutesSinceEpoch}\"")

Above line is from Android Official Documentation for Gradle Tips .

You have to update your build.gradle from:

each{ type ->
         type.buildConfigField 'String', 'API', SMS_API_KEY
     }

to:

each{ type ->
         type.buildConfigField 'String', 'API', "\"SMS_API_KEY\""
     }

Hope that works to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM