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

Flutter项目Kotlin版本不兼容:预期1.7.10却用1.9.0

解决Flutter项目中mobile_scanner插件Kotlin版本不兼容问题

核心问题

mobile_scanner-3.4.1插件依赖Kotlin 1.9.0版本的标准库,但你的项目指定使用1.7.10,导致元数据版本不匹配触发编译错误。以下是几种可行的解决办法:

1. 强制所有依赖统一使用项目指定的Kotlin版本

在项目根目录的build.gradle文件中,添加依赖版本强制策略,覆盖插件自带的Kotlin版本:

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.4.2" // 与Kotlin 1.7.10兼容的Gradle版本
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
    configurations.all {
        resolutionStrategy {
            // 强制所有依赖使用指定版本的Kotlin标准库
            force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10"
            force "org.jetbrains.kotlin:kotlin-stdlib:1.7.10"
            force "org.jetbrains.kotlin:kotlin-stdlib-common:1.7.10"
        }
    }
}

修改后执行flutter clean,再重新构建项目。

2. 降级mobile_scanner插件到兼容Kotlin 1.7.10的版本

mobile_scanner 3.4.1对Kotlin版本要求较高,你可以降级到更早的兼容版本(比如3.2.0),修改pubspec.yaml:

dependencies:
  mobile_scanner: ^3.2.0

执行flutter pub get更新依赖,再尝试编译。

3. 升级项目Kotlin版本到1.9.0(若项目允许)

如果项目没有必须使用1.7.10的限制,可以直接升级Kotlin版本:

  • 修改根目录build.gradle中的ext.kotlin_version为'1.9.0'
  • 同步Gradle插件版本(比如使用8.1.0版本的Android Gradle插件):
classpath "com.android.tools.build:gradle:8.1.0"

执行flutter clean后重新构建。

4. 确认Android Studio的Kotlin插件版本匹配

打开Android Studio的Settings -> Plugins -> Kotlin,确保插件版本和项目指定的1.7.10一致。若版本不匹配,切换到对应版本的插件并重启IDE。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 13:03:34