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

Flutter构建Release APK遇Kotlin版本不兼容问题求助

Flutter构建Release APK时Kotlin版本不兼容问题排查

问题详情

错误提示:

module was compiled with an incompatible version of kotlin. the binary version of its metadata is 1.8.0, expected version is 1.6

构建Flutter应用的Release APK时触发该错误,已尝试切换多种Kotlin版本仍未解决,项目配置文件如下:

项目配置文件

android/app/build.gradle

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

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

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

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

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

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


android {
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    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.rewaytech.rewayusers"
        // 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
        targetSdkVersion 33
        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 "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:31.2.3')
    implementation 'com.google.firebase:firebase-analytics-ktx'
}

android/build.gradle

buildscript {
    ext.kotlin_version = '1.8.0'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.15'
    }
}


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
}

排查与解决步骤

  1. 统一Gradle插件与Kotlin版本兼容性
    Kotlin 1.8.0要求Gradle插件版本至少为7.3.0,当前配置中Gradle插件是7.2.0,版本不匹配。修改android/build.gradle中的Gradle插件版本:

    classpath 'com.android.tools.build:gradle:7.3.3' // 可选择更高兼容版本
    

    确保项目中所有模块使用同一kotlin_version,避免版本分裂。

  2. 解决依赖库Kotlin版本冲突
    Firebase BOM 31.2.3可能依赖较低版本的Kotlin,尝试升级Firebase BOM到兼容Kotlin 1.8的版本:

    implementation platform('com.google.firebase:firebase-bom:32.0.0')
    

    或者执行./gradlew app:dependencies查看所有依赖的Kotlin版本,找到强制指定1.6版本的依赖,通过如下配置统一版本:

    // 加入到android/app/build.gradle的android块或顶层
    configurations.all {
        resolutionStrategy.force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0"
        resolutionStrategy.force "org.jetbrains.kotlin:kotlin-stdlib:1.8.0"
    }
    
  3. 清理构建缓存
    执行以下命令彻底清除旧编译缓存,避免残留冲突:

    flutter clean
    ./android/gradlew cleanBuildCache
    
  4. 升级Flutter SDK
    旧版本Flutter对高版本Kotlin支持有限,运行命令升级到稳定版:

    flutter upgrade
    
  5. 检查子模块/插件配置
    如果项目包含第三方Flutter插件,检查插件的android/build.gradle是否指定了独立的Kotlin版本,需统一为项目的1.8.0版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 17:07:26