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

Flutter首次构建APK时Kotlin版本不兼容问题求助

问题描述

首次执行flutter build apk时,出现Kotlin版本不兼容错误:模块元数据二进制版本为1.9.0,预期版本为1.7.1;重复构建时无此问题,但执行flutter clean、flutter pub cache clean、flutter pub get后再次构建会复现该错误。

已尝试以下操作但未解决:

  • 升级Flutter与Dart
  • 重装Android Studio
  • 升级所有依赖包
  • 添加dependency_overrides配置最新依赖版本

项目配置文件

pubspec.yaml

name: example_app
description: app description

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 0.0.1+1

environment:
  sdk: '>=3.4.0 <4.0.0'

dependencies:
  cached_network_image: ^3.3.1
  carousel_slider: ^4.2.1
  cmp_sdk: ^0.1.0
  compute: ^1.0.2
  connectivity_plus: ^6.0.3
  equatable: ^2.0.5
  firebase_analytics: ^10.10.6
  firebase_core: ^2.31.1
  firebase_messaging: ^14.9.3
  flutter:
    sdk: flutter
  flutter_bloc: ^8.1.5
  flutter_breadcrumb: ^1.0.1
  flutter_dotenv: ^5.1.0
  flutter_local_notifications: ^17.1.2
  flutter_localizations:
    sdk: flutter
  flutter_widget_from_html: ^0.15.0
  http: ^1.2.1
  image_picker: ^1.1.1
  intl: any
  location: ^6.0.2
  path: any
  provider: ^6.1.2
  share_plus: ^9.0.0
  shared_preferences: ^2.2.3
  transparent_image: ^2.0.1
  url_launcher: ^6.2.6
  youtube_player_iframe: ^5.1.2

dependency_overrides:
  js: ^0.7.1
  leak_tracker: ^10.0.5
  leak_tracker_flutter_testing: ^3.0.5
  material_color_utilities: ^0.11.1
  meta: ^1.15.0
  package_info_plus: ^8.0.0
  test_api: ^0.7.1
  vm_service: ^14.2.2

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^4.0.0

flutter:
  generate: true
  uses-material-design: true
  assets:
    - lib/.env

android/build.gradle

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
}

android/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"
}

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"
}

android {
    namespace = "com.exmaple.application_1"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

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

    kotlinOptions {
        jvmTarget = '1.8'
    }

    // kotlin {
    //     jvmToolchain(11)
    // }

    // buildFeatures {
    //     compose true
    // }
    
    // composeOptions {
    //     kotlinCompilerExtensionVersion '1.5.2'
    // }
    
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.example.application_1"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion = 21 //flutter.minSdkVersion
        targetSdkVersion = flutter.targetSdkVersion
        versionCode = flutterVersionCode.toInteger()
        versionName = flutterVersionName
        multiDexEnabled = true
    }

    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 = '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.0"
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'androidx.core:core-ktx:1.6.0'
    //implementation 'com.google.firebase:firebase-messaging:23.0.99'
    implementation 'com.google.firebase:firebase-bom:30.3.1'
    implementation 'net.consentmanager.sdk:android:2.4.0'
}

android\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 "8.0.0" apply false
    id "org.jetbrains.kotlin.android" version "1.8.22" apply false
    id "com.google.gms.google-services" version "4.3.13" apply false
}

include ":app"

android\gradle\wrapper\gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-all.zip
解决方案

问题核心是Kotlin插件版本与依赖的Kotlin标准库版本不匹配,且部分第三方依赖可能引入低版本Kotlin元数据,导致首次构建时冲突。按以下步骤修复:

1. 统一Kotlin版本

确保settings.gradle中的Kotlin插件版本和android/app/build.gradle中的Kotlin标准库版本完全一致:

  • 修改settings.gradle中的Kotlin插件版本为1.9.0:
plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.0.0" apply false
    id "org.jetbrains.kotlin.android" version "1.9.0" apply false
    id "com.google.gms.google-services" version "4.3.13" apply false
}
  • 确认android/app/build.gradle中的stdlib版本保持1.9.0:
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.0"
    // 其他依赖...
}

2. 强制所有子项目使用统一Kotlin版本

在android/build.gradle中添加配置,强制所有Flutter插件子项目使用指定版本,避免依赖引入低版本:

allprojects {
    repositories {
        google()
        mavenCentral()
    }
    // 新增版本统一配置
    configurations.all {
        resolutionStrategy {
            eachDependency { details ->
                if (details.requested.group == 'org.jetbrains.kotlin') {
                    details.useVersion '1.9.0'
                }
            }
        }
    }
}

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

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

3. 调整Gradle工具链(可选但推荐)

在android/app/build.gradle中启用Kotlin工具链,确保编译环境一致:

android {
    // 其他配置...
    kotlin {
        jvmToolchain(11)
    }
}

4. 彻底清理并重建

执行以下命令彻底清理后重新构建:

flutter clean
flutter pub cache clean
flutter pub get
cd android
./gradlew clean
cd ..
flutter build apk

额外排查点

检查cmp_sdk(版本^0.1.0),它可能是引入低版本Kotlin元数据的源头。如果上述步骤无效:

  • 查看该SDK文档确认支持的Kotlin版本
  • 联系SDK提供者获取适配高版本Kotlin的更新
  • 暂时移除该依赖,验证是否是它导致的冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 13:25:06