Gradle sync failed: Cause: compileSdkVersion is not specified

问题描述 投票:0回答:12

我正在尝试在 android studio 中测试我的离子应用程序。它抛出以下错误。

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.+'
}
android cordova ionic-framework
12个回答
23
投票

您正在使用 27.+

android 支持
库,因此您必须提供
sdk
版本
27
作为
compileSdkVersion
targetSdkVersion
否则您的项目不知道应该为哪个平台构建您的项目。这些参数应该在 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'
这一行


4
投票

就我而言,我通过将 Android gradle 插件版本和 gradle 版本设置为最新来修复它。


2
投票

如果您正在处理旧项目并使用最新版本的 Android studio,那么只需更改

compileSdk 27

compileSdkVersion 27

在您的应用程序级 build.gradle 文件中。它会修复这个错误


1
投票

就我而言,我通过替换旧插件应用语法解决了这个问题:

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

我所有

build.gradle
文件中的新插件DSL:

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

0
投票

Please Add Below Line in your gradle file

  compileSdkVersion 26

请检查以下代码以供参考

android {
        compileSdkVersion 26
        buildToolsVersion '27.0.3'

        defaultConfig {
            applicationId ""
            minSdkVersion 15
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }

    }

0
投票

注意:我的 app/build.gradle 中有一个错误,因此它不是 读取 compileSDKVersion 属性。当我评论这样一行时, 错误消失了:

//def enableHermes = project.ext.react.get("enableHermes", false);

首先我要点击这个链接:

ReactNative:从源代码构建

然后这个:

ReactNative:安卓开发环境

然后您可以将其添加到您的 settings.gradle 文件中:

//
include ':ReactAndroid'
//
project(':ReactAndroid').projectDir = new File(
    rootProject.projectDir, '../node_modules/react-native/ReactAndroid')

对于我的 ReactViro 示例项目,我还必须从 react-native node_modules 目录添加依赖项:

    implementation project(':arcore_client') // remove this if AR not required
    implementation project(':gvr_common')
    implementation project(path: ':viro_renderer')
    implementation project(path: ':react_viro')

在我的settings.gradle中:

//
include ':react_viro', ':arcore_client', ':gvr_common', ':viro_renderer'
project(':arcore_client').projectDir = new File('../node_modules/react-viro/android/arcore_client')
project(':gvr_common').projectDir = new File('../node_modules/react-viro/android/gvr_common')
project(':viro_renderer').projectDir = new File('../node_modules/react-viro/android/viro_renderer')
project(':react_viro').projectDir = new File('../node_modules/react-viro/android/react_viro')
//

0
投票

在我的例子中,错误来自我自己的插件,我通过将以下行添加到我的 plugin.xml 文件来修复它:(我发现这比更新插件的 gradle 文件更简单)

    <platform name="android">
        <preference name="android-compileSdkVersion" value="30" />
         ...

0
投票

Updating required Package.json libraries 对我有用试试看。大多数错误都会得到解决。

Jcenter()
中删除
build.gradle


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 {
                 // Remove this override in 0.66, as a proper fix is included in react-native itself.
                 force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
             }
         }
    

0
投票

您不需要从构建工具中指定 android.buildToolsVersion For

buildToolsVersion
在版本
27.0.3
或更高版本中不需要指定
buildToolsVersion
.

构建工具 27.0.3 或更高版本。请记住,您不再需要 使用指定构建工具的版本 android.buildToolsVersion 属性——插件使用最小的 默认需要版本。

来源(developer.android.com


0
投票

在我的例子中,使用 Capacitor 和错误“compileSdkVersion is not specified. Please add it to build.gradle”,我不得不通过在

android/app/build.gradle
:
中添加
compileSdk 33
来编辑文件
defaultConfig

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 {
            […]
        }
    }
}

0
投票

在文件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'
}
© www.soinside.com 2019 - 2024. All rights reserved.