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

集成Google Mobile Ads的Flutter项目打包时出现重复类错误求助

构建Flutter APK时的Kotlin依赖冲突问题

错误详情

构建Release版APK时触发重复类检测失败,核心错误日志如下:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkReleaseDuplicateClasses'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable
   > Duplicate class kotlin.collections.jdk8.CollectionsJDK8Kt found in modules jetified-kotlin-stdlib-1.8.10 (org.jetbrains.kotlin:kotlin-stdlib:1.8.10) and jetified-kotlin-stdlib-jdk8-1.7.10 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10)
     Duplicate class kotlin.internal.jdk7.JDK7PlatformImplementations found in modules jetified-kotlin-stdlib-1.8.10 (org.jetbrains.kotlin:kotlin-stdlib:1.8.10) and jetified-kotlin-stdlib-jdk7-1.7.10 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.10)
     // 其余重复类日志省略
     Go to the documentation to learn how to Fix dependency resolution errors.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 27s
Running Gradle task 'assembleRelease'...                           28.9s
Gradle task assembleRelease failed with exit code 1

相关配置文件

应用级build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    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.example.test"
    compileSdkVersion 33
    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 {
        applicationId "com.example.test"
        minSdkVersion 28
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation 'com.google.android.gms:play-services-ads:22.3.0'
}

项目级build.gradle

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

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

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
}

pubspec.yaml

name: test
description: A new Flutter project.
publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: '>=3.1.0 <4.0.0'

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  firebase_auth: ^4.8.0
  firebase_core: ^2.15.1
  cloud_firestore: ^4.8.0
  http: ^1.1.0
  carousel_slider: ^4.2.1
  intl: ^0.18.1
  firebase_messaging: ^14.6.7
  get: ^4.6.5
  photo_view: ^0.14.0
  flutter_local_notifications: ^15.1.1
  file_picker: ^5.3.4
  firebase_storage: ^11.2.6
  image_cropper: ^5.0.0
  flutter_reorderable_grid_view: ^4.0.0
  share_plus: ^7.1.0
  google_mobile_ads: ^3.0.0
  
dev_dependencies:
  flutter_test:
    sdk: flutter

已尝试操作及新问题

曾尝试将项目Kotlin版本升级至1.8.0,但触发新错误:模块元数据版本为1.8.0,而预期版本是1.6.0。

解决方案

方案1:统一Kotlin版本并强制依赖对齐

  1. 更新项目级build.gradle:
    将Kotlin版本升级至与当前Gradle兼容的稳定版(如1.8.22),修改buildscript块:

    buildscript {
        ext.kotlin_version = '1.8.22'
        repositories {
            google()
            mavenCentral()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:7.3.0'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
  2. 强制所有依赖使用统一Kotlin版本:
    在应用级build.gradle的android块内添加以下配置,避免依赖引入不同版本的Kotlin库:

    configurations.all {
        resolutionStrategy {
            force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
            force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
            force "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        }
    }
    
  3. 清理缓存并重建:
    执行以下命令清除旧缓存后重新构建:

    flutter clean
    flutter pub get
    flutter build apk --release
    

方案2:升级Gradle版本(方案1无效时使用)

若仍出现元数据版本不匹配,需升级Gradle至支持Kotlin 1.8+的版本(如7.4.2):

  1. 修改项目级build.gradle中的Gradle插件版本:
    classpath 'com.android.tools.build:gradle:7.4.2'
    
  2. 更新gradle/wrapper/gradle-wrapper.properties中的下载链接:
    distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
    
  3. 再次执行清理和构建命令。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 18:10:53