flutter 'compileReleaseJavaWithJavac' task (current target is 1.8) and 'compileReleaseKotlin' ta...

flutter项目打包 apk时遇到下面的错误:


截屏2024-11-07 09.46.24.png

这是因为Kotlin版本不一致导致的,可在andiord/build.gradle里面强制指定使用Kotlin17版本即可解决

subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    afterEvaluate {
        if (it.hasProperty('android')) {
            if (it.android.namespace == null) {
                def manifest = new XmlSlurper().parse(file(it.android.sourceSets.main.manifest.srcFile))
                def packageName = manifest.@package.text()
                android.namespace = packageName
            }
            // 指定Kotilin版本
            def javaVersion = JavaVersion.VERSION_17
            android {
                compileOptions {
                    sourceCompatibility javaVersion
                    targetCompatibility javaVersion
                }
                tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
                    kotlinOptions {
                        jvmTarget = javaVersion.toString()
                    }
                }
            }
        }
    }
}

注:修改完第一次打包可能出现报错,不用管,再来一次即可。
参考资料:https://github.com/flutter/flutter/issues/125181

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容