我刚刚更新了我的Ffltter项目包,使其符合零安全标准,现在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

推荐答案

您需要在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"
    }
}

UPDATE

如果项目中的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将在project build.grade文件中给出提示.

Flutter相关问答推荐

Flutter:如何从Bottom Navigator中删除此内容

将目标平台设置为Flutter 构建

为什么Build_Runner的输出为0?

分页控制器在到达页面末尾之前或在初始化页面时对每个索引调用API

我怎样才能在Ffltter中显示html布局链接?

Flutter 文件拾取器Android SingleInstance模式它返回的文件路径为空

通过Ffltter应用程序与Docker服务器进行交互以进行身份验证

如何在 Flutter 中创建厚度沿着容器逐渐减小的边框

SliverAppbar 呈蓝色

尽管设置了对齐方式,如何对齐列中的行?

我怎样才能消除我的 Column 子元素之间的差距?

我的第一个 flutter 项目出错,启动时为空值?

应用程序和应用程序内的 webview 之间的单点登录

如何从 Flutter Slider Widget 中移除发光效果?

Android: 从 URI 获取音频元数据

Firestore:缓存和读取数据

Flutter 如何要求用户在应用程序中设置电话密码?

如何获取 android 和 Ios (flutter) 的 CircularProgress 指示器?

构建失败 - 无法解析 io.grpc:grpc-core:[1.28.0]. (是的,我已经升级到 mavenCentral())

是否可以在 Flutter 中使用 ToggleButtons 在页面 PageView 之间切换?