未指定compileSdkVersion。请将其添加到 build.gradle:即使我在 build.gradle 文件中定义了它

Fab*_*oPE 5 java android gradle kotlin android-gradle-plugin

我尝试添加 openweather api,当我尝试同步文件时,build.gradle会弹出此错误

未指定compileSdkVersion。请将其添加到build.gradle

我的文件:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    namespace 'com.example.myapplication'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.6.0'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
Run Code Online (Sandbox Code Playgroud)

我已经定义了 CompileSDK,所以不知道为什么仍然会弹出这个问题。

小智 3

此错误定义您没有提到compileSdkVersion而不是compileSdk。虽然两者的含义相同,但如果您遇到此问题,请compileSdkVersion在 build.gradle 文件中提及。

\n

此问题与 7.0.0 中的新 gradle 更改有关,因此您可以使用带有字符串和 int 值的compileSdkVersion,如下所示。

\n
android {\ncompileSdkVersion = "S"\n//or with same number version\n}\n
Run Code Online (Sandbox Code Playgroud)\n