Kotlin Multiplatform Mobile:项目已经有一个名为 SDWebImage 的 CocoaPods 依赖项

问题描述

我创建了一个演示项目来将 cocoapods 集成到 KMM 项目中。我已从官方网站关注此 link。在 step 3 重新导入项目时,我收到以下错误

Project already has a CocoaPods dependency with name SDWebImage ,之后我什至无法在 KMM 中导入这个库。

有人可以帮忙吗?

更新:添加 build.gradle.kts

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

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

// CocoaPods requires the podspec to have a version.
 version = "1.0"

kotlin {
    android()
    ios {
        cocoapods {
            // Configure fields required by CocoaPods.
            summary = "Some description for a Kotlin/Native module"
            homepage = "Link to a Kotlin/Native module homepage"

            pod("SDWebImage")

            // You can change the name of the produced framework.
            // By default,it is the name of the Gradle project.
            frameworkName = "shared"
        }
    }
    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("com.google.android.material:material:1.2.1")
            }
        }
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13")
            }
        }
        val iosMain by getting
        val iosTest by getting
    }
}

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

val packForXcode by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONfigURATION") ?: "DEBUG"
    val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
    val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
    val framework = kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
    inputs.property("mode",mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir,"xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)
}

tasks.getByName("build").dependsOn(packForXcode)

解决方法

当您使用 cocoapods 插件时,您不需要手动声明 packForXcode 目标,也许这就是问题所在。尝试在 val packForXcode

之后删除所有内容

cocoapods 部分应该在 kotlin 内,而不是在 ios