首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >您的项目需要更新版本的Kotlin Gradle插件。(Android Studio)

您的项目需要更新版本的Kotlin Gradle插件。(Android Studio)
EN

Stack Overflow用户
提问于 2022-01-30 21:33:24
回答 10查看 97.2K关注 0票数 101

我刚刚更新了我的flutter项目包,使其符合零安全标准,现在Android希望我更新我的项目,以使用Kotling Gradle插件的最新版本。不过,我看不出该把它改在哪里。我试图将"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"转换为"org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10",但这没有任何效果。

我的build.grade-file看起来如下:

代码语言:javascript
运行
复制
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'

生成输出:

代码语言:javascript
运行
复制
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
EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2022-01-30 22:06:36

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

更改ext.kotlin_version行的版本:

代码语言:javascript
运行
复制
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,并且遇到以下错误,上述解决方案也会有效:

代码语言:javascript
运行
复制
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.

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

票数 173
EN

Stack Overflow用户

发布于 2022-03-01 09:20:10

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

代码语言:javascript
运行
复制
ext.kotlin_version = '1.6.10' //

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

票数 18
EN

Stack Overflow用户

发布于 2022-01-30 21:52:39

对此更改:

代码语言:javascript
运行
复制
classpath 'com.android.tools.build:gradle:4.1.0'

然后把它包装起来:

代码语言:javascript
运行
复制
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70919127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档