首页 > 解决方案 > 我的 build.gradle 文件出现错误

问题描述

我的build.gradle文件如下所示:

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

我的 Logcat 中的错误如下所示:

 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

我尝试了其他一些关于在类中声明API_KEY哪个被声明为 a的东西。我什至尝试将双引号与单引号一起使用,但它根本没有帮助。constantConstant

标签: androidbuild.gradle

解决方案


用这个:

buildConfigField "String", "API", "\"YOUR_SMS_API_KEY\"" 

推荐阅读