【问题标题】:Your project requires a newer version of the Kotlin Gradle plugin. (Android Studio)您的项目需要更新版本的 Kotlin Gradle 插件。 (安卓工作室)
【发布时间】:2022-08-04 14:43:32
【问题描述】:

我刚刚更新了我的颤振项目包以符合空安全性,现在 Android Studio 希望我更新我的项目以使用最新版本的 Kotling Gradle 插件。看不到在哪里改变它。我试图将\"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version\" 更改为\"org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10\" 但这没有效果。

我的build.grade-文件如下所示:

def localProperties = new Properties()
def localPropertiesFile = rootProject.file(\'local.properties\')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader(\'UTF-8\') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty(\'flutter.sdk\')
if (flutterRoot == null) {
    throw new GradleException(\"Flutter SDK not found. Define location with flutter.sdk in the local.properties file.\")
}

def flutterVersionCode = localProperties.getProperty(\'flutter.versionCode\')
if (flutterVersionCode == null) {
    flutterVersionCode = \'1\'
}

def flutterVersionName = localProperties.getProperty(\'flutter.versionName\')
if (flutterVersionName == null) {
    flutterVersionName = \'1.0\'
}

apply plugin: \'com.android.application\'
apply plugin: \'kotlin-android\'
apply from: \"$flutterRoot/packages/flutter_tools/gradle/flutter.gradle\"

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file(\'key.properties\')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion 31

    sourceSets {
        main.java.srcDirs += \'src/main/kotlin\'
    }

    lintOptions {
        disable \'InvalidPackage\'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId \"*********\"
        minSdkVersion 30
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner \"androidx.test.runner.AndroidJUnitRunner\"
    }



    signingConfigs {
        release {
            keyAlias keystoreProperties[\'keyAlias\']
            keyPassword keystoreProperties[\'keyPassword\']
            storeFile keystoreProperties[\'storeFile\'] ? file(keystoreProperties[\'storeFile\']) : null
            storePassword keystoreProperties[\'storePassword\']
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }

}

flutter {
    source \'../..\'
}

dependencies {
    implementation \"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version\"
    testImplementation \'junit:junit:4.12\'
    androidTestImplementation \'androidx.test:runner:1.1.1\'
    androidTestImplementation \'androidx.test.espresso:espresso-core:3.1.1\'
    implementation \'com.google.firebase:firebase-analytics:17.2.2\'
}
apply plugin: \'com.google.gms.google-services\'

构建输出:

BUILD FAILED in 8s
[!] Your project requires a newer version of the Kotlin Gradle plugin.
    Find the latest version on https://kotlinlang.org/docs/gradle.html#plugin-and-versions, then update project/android/build.gradle:
    ext.kotlin_version = \'<latest-version>\'
Exception: Gradle task assembleDebug failed with exit code 1

    标签: flutter android-studio kotlin gradle-kotlin-dsl


    【解决方案1】:

    您需要在您的 android 根项目 projectName/android/build.gradle 中更改 kotlin 版本,而不是 projectName/android/app/build.gradle

    ext.kotlin_version 行更改版本:

    buildscript {
        ext.kotlin_version = '1.6.10' // Change here
        repositories {
            google()
            jcenter()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:4.1.0'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    

    更新

    如果您的项目的 android 部分使用 java 而依赖项使用 kotlin 并且您遇到以下错误,则上述解决方案也将起作用:

    Incompatible classes were found in dependencies. Remove them from the classpath or use '-Xskip-metadata-version-check' to suppress errors
    e: /home/user/.gradle/caches/transforms-3/36814238b86d8b6b6f9e4e1263bce879/transformed/jetified-kotlinx-coroutines-core-jvm-1.5.2.jar!/META-INF/kotlinx-coroutines-core.kotlin_module: 
    Module was compiled with an incompatible version of Kotlin. 
    The binary version of its metadata is 1.5.1, expected version is 1.1.15.
    

    如果 kotlin 与 IDE 中安装的 kotlin 不一致,AndroidStudio 或 IntelliJ Idea 将在项目 build.grade 文件中给出提示。

    【讨论】:

    • 它可以工作,但是如何根据 sdk 识别 kotlin_version
    • 我还将gradle-wrapper中的distributionUrl更改为7.3.3-all.zip
    • @YeasinSheikh:如果build.grade 中的 kotlin 与 IDE 中安装的 kotlin 不同,afaik、AndroidStudio 或 IntelliJ Idea 将发出警告。
    • 谢啦。升级我的飞镖和颤振版本后,我在这个问题上已经有 2 天了
    • @YeasinSheikh:>工具->Kotlin->配置 Kotling 插件更新
    【解决方案2】:

    将您的 Kotlin 更新到最新版本。您可以在此处查看最新版本

    ext.kotlin_version = '1.6.10' //
    

    您可以从以下链接查看最新版本的 Kotlin https://kotlinlang.org/docs/gradle.html#plugin-and-versions

    【讨论】:

      【解决方案3】:

      我将 Flutter 更新到 2.10.1 我发现我所有的项目都有相同的错误 在应用程序/build.gradl 您更改为 ext.kotlin_version = '1.6.10'

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案4】:

      将 build gradle 更改为此:

      classpath 'com.android.tools.build:gradle:4.1.0'
      

      和 gradle-wrapper 到这个:

      distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
      

      【讨论】:

        【解决方案5】:

        在我的例子中,我没有在我的颤振项目中使用 kotlin,但是我的一些依赖项使用它(对我来说是 assets_audio_player),所以我不得不去 build.gradle 获取该依赖项并将 kotlin 版本更新为这里的其他答案:

        ext.kotlin_version = '<latest-version>'
        

        注1:您可以通过在 Android Studio 中打开 android 模块为您想要的库打开 build.gradle,然后从 Project 选项卡转到 Gradle Scripts

        笔记2:您可以通过查看以下内容来了解​​导致错误的库:

        Execution failed for task ':assets_audio_player:compileDebugKotlin'.
        

        它可能不止一个库,因此在您更改 kotlin 版本后,错误将被更改。

        【讨论】:

        • 如何解决我更新到最新版本但仍然收到错误
        • @RajaEhtisham 你能在我的回答中写下注释2中的错误行吗?
        • 上帝保佑你,你拯救了我的一天,它帮助我解决了这个问题
        【解决方案6】:

        1-首先将ext.kotlin_version更改为projectName/android/build.gradle

        buildscript {
            ext.kotlin_version = '1.6.10'
        }
        

        2-第二次将构建等级更改为:

        distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
        

        【讨论】:

          【解决方案7】:
          • 将构建 gradle 版本更改为4.1.0项目级别 build.gradle 文件中的或更高版本:

            classpath 'com.android.tools.build:gradle:4.1.0'
            
          • 将 gradle 包装器版本更改为6.7-全部gradle-wrapper.properties 文件中的或更高版本:

            distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
            
          • ext.kotlin_version 更改为1.6.10项目级别或更高级别的build.gradle 文件:

            buildscript {
              ext.kotlin_version = '1.6.10' // Change like this
              repositories {
                  google()
                  jcenter()
              }
            
              dependencies {
                  classpath 'com.android.tools.build:gradle:4.1.0'
                  classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
              }
            }
            

          【讨论】:

            【解决方案8】:

            添加最新的 kotlin 版本在你的项目中

             buildscript {
            ext.kotlin_version = '1.6.10'
            repositories {
                google()
                mavenCentral()
            }
            

            【讨论】:

              【解决方案9】:

              也许其他人在升级颤振后偶然发现了一个问题: 如果您将颤振与 aws_amplify 一起使用,您还必须升级 amplify。这是我遇到相同错误消息的问题。

              【讨论】:

              【解决方案10】:

              您必须在 android 根目录中更改 kotlin version,转到

              projectName/android/build.gradle
              
              buildscript {
                  ext.kotlin_version = '1.7.0'
                  repositories {
                      google()
                      jcenter()
                  }
              
                  dependencies {
                      classpath 'com.android.tools.build:gradle:4.1.0'
                      classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
                  }
              }
              

              并在gradle-wrapper.properties 中将 gradle 包装器版本更改为 6.7.1-all 或更高版本

              distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
              

              【讨论】:

                猜你喜欢
                • 2022-11-01
                • 2017-09-08
                • 1970-01-01
                • 1970-01-01
                • 2017-11-30
                • 1970-01-01
                • 1970-01-01
                • 2015-06-10
                • 1970-01-01
                相关资源
                最近更新 更多