Gradle同步失败:原因:未指定compileSdkVersion。

46

我正在尝试在Android Studio中测试我的Ionic应用程序。它会抛出以下错误。

Gradle sync failed: Cause: compileSdkVersion is not specified.

有什么解决办法吗?我做错了什么。

这是我的 build.gradle 文件

apply plugin: 'com.android.application'

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
    }
}

// Allow plugins to declare Maven dependencies via build-extras.gradle.

allprojects {
    repositories {
        mavenCentral();
        jcenter()
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '4.1.0'
}

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:+'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:+'
    implementation 'com.android.support:appcompat-v7:27.+'
}

请提供编译SDK版本,例如在应用级别的Gradle中。 - debo.stackoverflow
1
请分享您的Gradle文件代码。 - Yogesh Borhade
1
compileSdkVersion 25 buildToolsVersion '26.0.2' defaultConfig { applicationId "com.pt.planner" minSdkVersion 21 targetSdkVersion 25 versionCode 24 versionName "2.4.0" multiDexEnabled true - debo.stackoverflow
1
编译SDK版本应与目标SDK版本相同。 - debo.stackoverflow
1
@Yogesh Borhade - 请检查一下,我已经分享了文件的详细信息。 - Amit Anand
显示剩余3条评论
14个回答

28

您正在使用 Android支持 库的 27.+ 版本,因此您必须将 sdk 版本设置为 27,并将 compileSdkVersiontargetSdkVersion 参数指定为同样的版本,否则您的项目将不知道应该建立在哪个平台上。 这些参数应该在 build.gradle(app) 文件中的 android 目录下进行配置:

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    defaultConfig {
        applicationId "com.example.abc.test"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

只需将以下代码粘贴到此行apply plugin: 'com.android.application'之下即可


1
谢谢Ghulam Moinul Quadir,现在它可以工作了。但是构建仍然失败并显示错误:
  1. 找不到符号类CallbackContext
  2. 找不到符号类CordovaPlugin
  3. 找不到符号类PluginResult
  4. 找不到符号类CordovaPlugin 请帮忙解决。
- Amit Anand
@AmitAnand请分享你的build.gradle(ProjectName)文件。 - Ghulam Moinul Quadir

6
在我的情况下,我通过替换应用语法的旧插件来解决了这个问题:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

使用所有build.gradle文件中的新插件DSL:

plugins {
    id "com.android.application"
    id "kotlin-android"
}

5
在我的情况下,我通过将Android gradle插件版本和gradle版本设置为最新版本来解决了问题。enter image description here

4

如果您正在处理一个旧项目并使用最新版本的Android Studio,则只需更改:

compileSdk 27

to

compileSdkVersion 27

在您的应用级别的build.gradle文件中,添加以下代码即可解决此错误。


3
如果有人在升级到Flamingo AGP 8后遇到了这个问题,他们需要将compileSdkVersion字段更新为compileSdk,因此您需要将gradle.build中的此行更新为以下内容:
compileSdkVersion 33

2
在我的情况下,使用 Capacitor 并出现错误 "compileSdkVersion 未指定。请将其添加到 build.gradle" ,我不得不通过在 defaultConfig 中添加 compileSdk 33 来编辑文件 android/app/build.gradle
apply plugin: 'com.android.application'

android {
    signingConfigs {
        debug {
            […]
        }
        release {
            […]
        }
    }
    compileSdk compileSdkVersion
    defaultConfig {
        applicationId "[…]"
        minSdk 25
        targetSdk 33
        compileSdk 33
        versionCode 33
        versionName '33'
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        signingConfig signingConfigs.release
    }
    buildTypes {
        release {
            […]
        }
    }
}

1
在我的情况下,错误来自于我的插件,我通过将以下行添加到我的plugin.xml文件中进行了修复:(我发现这比更新插件的gradle文件更简单)
    <platform name="android">
        <preference name="android-compileSdkVersion" value="30" />
         ...

它又停止工作了。不知道为什么 Cordova 对我来说有点间歇性。 - MIWMIB
cordova platform add android 报错,但构建仍在运行。将插件的 gradle 文件更新为 dependencies { implementation "androidx.biometric:biometric:1.1.0" } 解决了我的问题。 - MIWMIB

1

在文件variables.gradle中更改compileSdkVersion和targetSdkVersion,该文件位于android/variables.gradle中。

ext {
    minSdkVersion = 22
    compileSdkVersion = 32
    targetSdkVersion = 32
    androidxActivityVersion = '1.4.0'
    androidxAppCompatVersion = '1.4.2'
    androidxCoordinatorLayoutVersion = '1.2.0'
    androidxCoreVersion = '1.8.0'
    androidxFragmentVersion = '1.4.1'
    coreSplashScreenVersion = '1.0.0-rc01'
    androidxWebkitVersion = '1.4.0'
    junitVersion = '4.13.2'
    androidxJunitVersion = '1.1.3'
    androidxEspressoCoreVersion = '3.4.0'
    cordovaAndroidVersion = '10.1.1'
}

1

compileSdkVersion已过时

从SDK 33及以上不要使用

compileSdkVersion 33

但使用:
 android {
   // compileSdkVersion 33 and above
    defaultConfig {
        compileSdk 33
    }
...}

0

将以下代码添加到您的android/build.gradle文件中:

  1. 首先,保存您的代码并卸载您的Android Studio,然后重新安装。

  2. 在android/build.gradle中添加以下行:

    def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())

     allprojects {
         configurations.all {
             resolutionStrategy {
                 // 在0.66版本中删除此覆盖,因为react-native本身已包含适当的修复。
                 force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
             }
         }
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接