You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用on_audio_query:^2.9.0构建APK时出现JVM目标兼容性不一致错误

解决Flutter项目JVM-target兼容性错误

问题场景

使用on_audio_query: ^2.9.0开发离线音乐播放器,添加权限后构建APK时出现以下错误:

Execution failed for task ':on_audio_query_android:compileDebugKotlin'.
> Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'compileDebugKotlin' (17). 

项目的app/build.gradle内容:

plugins {
    id "com.android.application"
    id "kotlin-android"
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    id "dev.flutter.flutter-gradle-plugin"
}

android {
    namespace = "com.example.melodymate"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.example.melodymate"
        // You can update the following values to match your application needs.
        // For more information, see: https://flutter.dev/to/review-gradle-config.
        minSdk = flutter.minSdkVersion
        targetSdk = flutter.targetSdkVersion
        versionCode = flutter.versionCode
        versionName = flutter.versionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig = signingConfigs.debug
        }
    }
}

flutter {
    source = "../.."
}

settings.gradle插件配置:

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.1.0" apply false
    id "org.jetbrains.kotlin.android" version "2.0.20" apply false
}

解决方案

错误原因是on_audio_query_android模块默认使用JVM 17编译,但你的项目配置的是JVM 1.8,导致版本不一致。以下两种方法均可解决:

方法一:升级项目JVM目标版本到17

修改app/build.gradle中的compileOptions和kotlinOptions:

android {
    // ... 其他配置不变
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_17
    }
    // ... 其他配置不变
}

方法二:强制所有模块使用统一JVM版本

在项目根目录的build.gradle(与settings.gradle同级)中添加以下配置,强制所有模块使用JVM 17编译:

subprojects {
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            android {
                compileOptions {
                    sourceCompatibility = JavaVersion.VERSION_17
                    targetCompatibility = JavaVersion.VERSION_17
                }
                kotlinOptions {
                    jvmTarget = JavaVersion.VERSION_17
                }
            }
        }
    }
}

修改完成后,重新执行构建命令即可。

内容的提问来源于stack exchange,提问作者yash gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 13:35:57