解决“jvm target compatibility should be set to the same Java version”

引入image_compression_flutter插件后,在Android端编译报错了:

Execution failed for task ':image_compression_flutter:compileDebugKotlin'.
> 'compileDebugJavaWithJavac' task (current target is 1.8) and 'compileDebugKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.
  Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain

这个错误是由于Java和Kotlin的JVM目标版本不一致导致的。

修改本工程的android/app/build.gradle文件:

1、build.gradle文件中设置Java和Kotlin的JVM目标版本一致

android {
    // ... 其他配置 ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
            kotlinOptions.jvmTarget = "1.8"
        }
    }
}

2、使用Kotlin的JVM工具链

kotlin {
    jvmToolchain {
        languageVersion.set(JavaLanguageVersion.of(8))
    }
}

3、遍历并修改所有Kotlin编译任务的JVM目标版本

allprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
}

修改image_compression_flutter插件的gradle 

编辑Pub\Cache\hosted\pub.dev\image_compression_flutter-1.0.4\android\build.gradle

android {
    // ... 其他配置 ...
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注