您的项目需要更新版本的 Kotlin Gradle 插件。(安卓工作室)

Tha*_*nos 170 kotlin android-studio flutter gradle-kotlin-dsl

我刚刚将我的 flutter 项目包更新为空安全兼容,现在 Android Studio 希望我更新我的项目以使用最新版本的 Kodling 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'
Run Code Online (Sandbox Code Playgroud)

构建输出:

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
Run Code Online (Sandbox Code Playgroud)

ישו*_*ותך 247

您需要更改 Android 根项目中的 kotlin 版本,projectName/android/build.gradle而不是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"
    }
}
Run Code Online (Sandbox Code Playgroud)

更新

如果项目的 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.
Run Code Online (Sandbox Code Playgroud)

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

注意:要查看 Kotlin 的新版本,请查看此链接: https ://kotlinlang.org/docs/releases.html#release-details

  • 它可以工作,但是我如何根据sdk识别kotlin_version (9认同)
  • @YeasinSheikh:&gt;工具-&gt;Kotlin-&gt;配置 Kotling 插件更新 (2认同)

Ume*_*qas 28

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

ext.kotlin_version = '1.6.10' //
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述


Ary*_*nzh 12

将构建 gradle 更改为:

classpath 'com.android.tools.build:gradle:4.1.0'
Run Code Online (Sandbox Code Playgroud)

和 gradle-wrapper :

distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
Run Code Online (Sandbox Code Playgroud)


小智 12

我将 Flutter 更新到 2.10.1,发现我的所有项目在 app /build.gradl 中都有相同的错误,您将其更改为 ext.kotlin_version = '1.6.10'在此输入图像描述

在此输入图像描述

  • 正如目前所写的,您的答案尚不清楚。请[编辑]添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以[在帮助中心](/help/how-to-answer)找到有关如何写出好的答案的更多信息。 (3认同)

Ana*_*nan 9

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

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"
    }
}
Run Code Online (Sandbox Code Playgroud)

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

distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
Run Code Online (Sandbox Code Playgroud)

您也可以在这里获取最新版本


Ans*_*kki 8

就我而言,我不在我的 flutter 项目中使用 kotlin,但我的一些依赖项使用它(它适合assets_audio_player我),所以我必须去build.gradle寻找该依赖项并更新 kotlin 版本,如其他答案所示:

ext.kotlin_version = '<latest-version>'
Run Code Online (Sandbox Code Playgroud)

注 1:您可以build.gradle通过在 Android Studio 中打开 android 模块,然后从“项目”选项卡中打开所需的库Gradle Scripts

在此输入图像描述

注2:您可以通过查看以下内容来了解​​哪个库导致了错误:

Execution failed for task ':assets_audio_player:compileDebugKotlin'.
Run Code Online (Sandbox Code Playgroud)

它可能是多个库,因此在更改 kotlin 版本后,错误将会更改。


Sai*_*i N 7

将 kotlin 版本更改为ext.kotlin_version = '1.8.21'inside android/build.gradle,它可以解决您的问题。

在这里检查所有版本查找最新的 Kotlin 版本


Ade*_*del 5

1- 第一次ext.kotlin_version更改projectName/android/build.gradle

buildscript {
    ext.kotlin_version = '1.6.10'
}
Run Code Online (Sandbox Code Playgroud)

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

distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
Run Code Online (Sandbox Code Playgroud)


小智 5

更改 projectName/android/build.gradle 上的 ext.kotlin_version

buildscript {
   ext.kotlin_version = '1.8.22' //latest version
   repositories {
      google()
      mavenCentral()
   }

   dependencies {
      classpath 'com.android.tools.build:gradle:7.1.2'
      classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
   }
}
Run Code Online (Sandbox Code Playgroud)

您可以使用以下链接获取“最新的 kotlin_version” https://kotlinlang.org/docs/gradle-configure-project.html#apply-the-plugin