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

升级Gradle后flutter_phone_direct_caller出现JVM目标兼容性不一致错误

Flutter构建错误:JVM目标兼容性不一致问题解决

错误信息

FAILURE: Build failed with an exception.

  • 错误原因:任务':flutter_phone_direct_caller:compileDebugKotlin'执行失败。检测到任务'compileDebugJavaWithJavac'(1.8)与'compileDebugKotlin'(17)的JVM目标兼容性不一致。

建议使用JVM Toolchain
了解更多JVM目标验证信息

  • 排查建议:
    使用--stacktrace选项获取堆栈跟踪信息。
    使用--info或--debug选项获取更多日志输出。
    使用--scan选项获取完整分析信息。

项目配置文件

根目录build.gradle

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.1.0' // Ensure this is a version 7.0.0 or above
        classpath 'com.google.gms:google-services:4.3.15'
        // NOTE: Do not place your application dependencies here; they belong in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

app模块build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
    id "com.google.gms.google-services"
    id "com.google.firebase.crashlytics"
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    namespace "com.app.campusadda"
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = "17"
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        applicationId "com.app.campusadda"
        minSdkVersion 23
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    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 platform('com.google.firebase:firebase-bom:32.1.0')
    implementation 'com.google.firebase:firebase-analytics-ktx'
}

settings.gradle

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }()

    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.3.0" apply false
    id "org.jetbrains.kotlin.android" version "2.0.21" apply false
    id "com.google.gms.google-services" version "4.3.15" apply false
    id "com.google.firebase.crashlytics" version "2.9.9" apply false
}

include ":app"

问题背景

项目原本运行正常,升级Gradle后出现上述错误,尝试过JVM Toolchain方案但未解决,求可行解决方案。

解决方案

1. 统一所有子模块JVM目标版本

修改根目录build.gradle的subprojects块,强制第三方插件与主项目使用相同的JVM版本:

subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    // 添加以下配置
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            project.android {
                compileOptions {
                    sourceCompatibility JavaVersion.VERSION_17
                    targetCompatibility JavaVersion.VERSION_17
                }
                kotlinOptions {
                    jvmTarget = "17"
                }
            }
        }
    }
}
subprojects {
    project.evaluationDependsOn(':app')
}

2. 正确配置JVM Toolchain

在app模块build.gradle的android块内添加Toolchain配置,确保Java和Kotlin版本统一:

android {
    // ... 原有配置
    kotlin {
        jvmToolchain(17)
    }
}

3. 更新第三方插件

检查flutter_phone_direct_caller插件版本,在pubspec.yaml中更新到最新版本后执行flutter pub get,新版本可能已适配高版本JVM。

4. 降级Gradle版本(临时方案)

若上述方案无效,可降级Gradle到之前稳定运行的版本:

  • 修改根目录build.gradle中com.android.tools.build:gradle的版本
  • 修改settings.gradle中com.android.application的版本以匹配Gradle版本

内容的提问来源于stack exchange,提问作者Techy Sujit

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 23:05:07