2

I have created a demo project to integrate cocoapods into the KMM project. I have followed this link from the official website. At step 3 while reimporting the project, I am receiving the following error.

Project already has a CocoaPods dependency with name SDWebImage , after which I am unable to even import this lib in KMM.

Can anyone please help with this?

Update: Adding 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)
Fatin Wasta
  • 424
  • 1
  • 6
  • 20

1 Answers1

1

When you’re using cocoapods plugin you don’t need to manually declare packForXcode target, maybe that’s the problem. Try to remove everything after val packForXcode

cocoapods section should be inside kotlin, not inside ios

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
  • Nope. Commenting PackForXcode didnt work. Still same errors. – Fatin Wasta Mar 30 '21 at 05:08
  • ok, I've found it. `cocoapods` section should be inside `kotlin`, not inside `ios` – Phil Dukhov Mar 30 '21 at 07:13
  • @Phillip yep. That was an issue. But I still can't import SDWebImage anywhere, not sure why? – Fatin Wasta Mar 30 '21 at 07:41
  • this is an known issue, you can see possible solutions here: https://medium.com/icerock/enable-iosmain-sourceset-in-kotlin-multiplatform-mobile-projects-91168db9ac5c – Phil Dukhov Mar 30 '21 at 07:49
  • having same issue for me. @FatinWasta Any info how you fixed it. tried above solutions but didn’t work for me. – Jitty Aandyan Apr 08 '21 at 09:52
  • @JittyAandyan have you tried the last line "`cocoapods` section should be inside `kotlin`, not inside `ios`"? That was the problem with the author code. You may have a different one, please provide your gradle file. Or are you talking about import not being available? – Phil Dukhov Apr 10 '21 at 04:10