Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'compileDebugKotlin' (17).

flutter 错误信息:
Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'compileDebugKotlin' (17).

解决方法:

不用更改android的plugin,因为问题根本没出在这里,网上的答案都是错误的
不用更改android的plugin,因为问题根本没出在这里,网上的答案都是错误的
不用更改android的plugin,因为问题根本没出在这里,网上的答案都是错误的

你只需要强制所有的包指定Kotlin版本即可。

更改android目录下的build.gradle

 1 allprojects {
 2     repositories {
 3         gradlePluginPortal()
 4         google()
 5         mavenCentral()
 6     }
 7 
 8     // 修复由于高版本导致namespace检测为空的问题,没遇到可不添加
 9     subprojects {
10         afterEvaluate { project ->
11             if (project.hasProperty('android')) {
12                 project.android {
13                     if (namespace == null) {
14                         namespace project.group
15                     }
16                 }
17             }
18         }
19     }
20     // 修复由于高版本导致namespace检测为空的问题,没遇到可不添加
21 
22     // 强制指定Kotilin版本
23     subprojects {
24         project.buildDir = "${rootProject.buildDir}/${project.name}"
25         afterEvaluate {
26             if (it.hasProperty('android')) {
27                 if (it.android.namespace == null) {
28                     def manifest = new XmlSlurper().parse(file(it.android.sourceSets.main.manifest.srcFile))
29                     def packageName = manifest.@package.text()
30 //                    println("Setting ${packageName} as android namespace")
31                     android.namespace = packageName
32                 }
33                 // 指定Kotilin版本
34                 def javaVersion = JavaVersion.VERSION_17
35                 android {
36                     compileOptions {
37                         sourceCompatibility javaVersion
38                         targetCompatibility javaVersion
39                     }
40                     tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
41                         kotlinOptions {
42                             jvmTarget = javaVersion.toString()
43                         }
44                     }
45 //                    println("Setting java version to ${javaVersion.toString()} which is $javaVersion")
46                 }
47             }
48         }
49     }
50     // 强制指定Kotilin版本
51     
52     subprojects {
53         project.evaluationDependsOn(':app')
54     }
55 }
View Code

它会指定所有子项目Kotlin使用的版本为17,其他版本对照修改即可。

参考资料:https://github.com/flutter/flutter/issues/125181

posted @ 2025-10-31 09:59  sikewang  阅读(45)  评论(0)    收藏  举报