在 Kotlin 多平台项目中构建 APK 不工作:/它说类型 xxx.xxx.BuildConfig 被定义了多次

Build APK in Kotlin Multi-Platform Project is not working :/ It says Type xxx.xxx.BuildConfig is defined multiple times


我有一个 kotlin 多平台项目
当我 运行 它在模拟器或 android 设备上它工作正常,但是当我试图构建一个 APK 它不工作 :)) 它说:

> A failure occurred while executing com.android.build.gradle.internal.tasks.D8MainDexListTask$MainDexListWorkerAction
   > Error while merging dex archives: 
     Learn how to resolve the issue at
         https://developer.android.com/studio/build/dependencies#duplicate_classes.
     Type xxx.xxx.BuildConfig is defined multiple times:
         /Users/macbook/AndroidStudioProjects/XProject-KMM/shared/build/intermediates/runtime_library_classes_jar/debug/classes.jar:com/xproject/BuildConfig.class, 
         /Users/macbook/AndroidStudioProjects/XProject-KMM/XProject/build/intermediates/transforms/RealmTransformer/xxx/debug/0/com/xxxx/BuildConfig.class

这是我的 Gradle 个文件:

build.gradle.kts (:Andorid_Project):

import org.jetbrains.kotlin.gradle.targets.js.npm.includedRange

plugins {
    id("com.android.application")
    kotlin("android")
}

dependencies {
    implementation(project(":shared"))
    implementation("com.google.android.material:material:1.3.0")
    implementation("androidx.appcompat:appcompat:1.3.0")
    implementation("androidx.constraintlayout:constraintlayout:2.0.4")
    .
    .
    .

    implementation ("com.github.Cutta:GifView:1.4")

    implementation ("com.novoda:merlin:1.2.0")

    testImplementation ("junit:junit:4.13.2")
    androidTestImplementation ("androidx.test.ext:junit:1.1.2")
    androidTestImplementation ("androidx.test.espresso:espresso-core:3.3.0")
    annotationProcessor ("io.realm:realm-android:0.82.2")
}

android {
    signingConfigs {
        getByName("debug") {
            storeFile = file("/Users/macbook/Desktop/key.jks")
            storePassword = "xxxxxx"
            keyAlias = "xxxx"
            keyPassword  = "xxxxxx"
        }
        create("release") {
            storeFile = file("/Users/macbook/Desktop/key.jks")
            storePassword = "xxxxxx"
            keyAlias = "xxxx"
            keyPassword  = "xxxxxx"
        }
    }
    compileSdkVersion(29)
    defaultConfig {
        applicationId = "com.xxxxxx"
        minSdkVersion(19)
        targetSdkVersion(29)
        versionCode = 14
        versionName = "2.2.7"
        multiDexEnabled = true
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        signingConfig = signingConfigs.findByName("debug")
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
    flavorDimensions("default")
    productFlavors {
        create("xx") {
            dimension("default")
            applicationIdSuffix = ".xx"
        }
        create("xxx") {
            dimension("default")
            applicationIdSuffix = ".xxx"
        }
        create("xxxx") {
            dimension("default")
            applicationIdSuffix = ".xxxx"
        }
    }
}

apply(mapOf("plugin" to "com.google.firebase.crashlytics"))
apply(mapOf("plugin" to "kotlin-kapt"))
apply(mapOf("plugin" to "realm-android"))
apply(mapOf("plugin" to "com.google.gms.google-services"))


build.gradle.kts (:Shared_Kotlin_Module):

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
}

version = "1.0"

kotlin {
    android()

    val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget = ::iosArm64

    iosTarget("ios") {}

    cocoapods {
        summary = "Some description for the Shared Module"
        homepage = "Link to the Shared Module homepage"
        ios.deploymentTarget = "14.1"
        frameworkName = "shared"
        podfile = project.file("../xxxx/Podfile")
    }
    
    sourceSets {
        val commonMain by getting {
            dependencies {
//                implementation(kotlin("stdlib-jdk8:1.4.10"))
                implementation("io.islandtime:core:0.6.0")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13.2")
            }
        }
        val iosMain by getting
        val iosTest by getting
    }
}

val packForXcode by tasks.creating(Sync::class) {
    group = "build"

    //selecting the right configuration for the iOS framework depending on the Xcode environment variables
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val framework = kotlin.targets.getByName<KotlinNativeTarget>("ios").binaries.getFramework(mode)

    inputs.property("mode", mode)
    dependsOn(framework.linkTask)

    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)

    doLast {
        val gradlew = File(targetDir, "gradlew")
        gradlew.writeText("#!/bin/bash\nexport 'JAVA_HOME=${System.getProperty("java.home")}'\ncd '${rootProject.rootDir}'\n./gradlew $@\n")
        gradlew.setExecutable(true)
    }
}

android {
    compileSdkVersion(29)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(19)
        targetSdkVersion(29)
    }
}


并且build.gradle.kts(项目级别):

buildscript {
//    ext.kotlin_version = "1.3.50"
    repositories {
        gradlePluginPortal()
        google()
        jcenter()
        mavenCentral()
        maven (url = "http://oss.jfrog.org/artifactory/oss-snapshot-local")
    }
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10")
        classpath("com.android.tools.build:gradle:4.2.1")
        classpath("io.realm:realm-gradle-plugin:10.0.0")
        classpath("com.google.gms:google-services:4.3.8")
        classpath("com.google.firebase:firebase-crashlytics-gradle:2.7.0")
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven (url = "https://jitpack.io")
        mavenCentral()
        maven (url = "http://oss.jfrog.org/artifactory/oss-snapshot-local")
    }
}

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

如有任何帮助,我们将不胜感激。

我会尝试以下操作:

  1. 确保您的 sharedandroid 模块具有不同的包名称
  2. 尝试clean-build/rebuild