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

Flutter构建APK遇Kotlin版本不兼容及JAR版本一致性问题求解

解决方案

1. 统一项目Kotlin版本(解决元数据版本不兼容问题)

问题核心是项目依赖的Kotlin版本和项目配置版本不一致,需全局统一:

  • 打开android/build.gradle,在buildscript块中指定统一的Kotlin版本(建议用依赖中最高的版本,比如1.9.0):
    buildscript {
        ext.kotlin_version = '1.9.0'
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            // 确保Gradle插件版本与Kotlin版本兼容,比如Kotlin 1.9.0对应Gradle插件7.4+
            classpath "com.android.tools.build:gradle:7.4.2"
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
  • 打开app/build.gradle,确保所有Kotlin相关依赖都使用上述统一版本:
    dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        // 其他依赖保持不变
    }
    

2. 强制统一Runtime JAR版本(解决类路径版本冲突)

通过Gradle解析策略强制所有依赖使用统一版本的Kotlin库:

  • 在android/build.gradle的allprojects块中添加以下配置:
    allprojects {
        repositories {
            google()
            mavenCentral()
        }
        configurations.all {
            resolutionStrategy.force "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
            resolutionStrategy.force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
            resolutionStrategy.force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
            resolutionStrategy.force "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
        }
    }
    

3. 清理缓存并重新构建

旧缓存可能导致版本残留,执行以下命令:

  • 清理Flutter项目缓存:
    flutter clean
    
  • 清理Gradle构建缓存:
    ./gradlew cleanBuildCache
    
  • 重新构建APK:
    flutter build apk
    

4. 检查环境兼容性

运行flutter doctor确认Flutter、Android Studio、Gradle等环境组件版本匹配,避免因环境不兼容引发的隐性问题。

内容的提问来源于stack exchange,提问作者Muhammad Akbar Fauzi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 18:42:01