kotlin多平台项目中的build apk不起作用:/它说..buildconfig类型定义了多次

whlutmcx  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(356)

我有一个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(:and id_项目):

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(:共享\u kotlin\u模块):

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

任何帮助都将不胜感激。

nqwrtyyt

nqwrtyyt1#

我会尝试以下方法:
确保您的 sharedandroid 模块具有不同的包名称
尝试进行干净的构建/重建

相关问题