Switch Kotlin Multiplatform project from CocoaPods to SwiftPM dependencies
If you have a KMP module with CocoaPods dependencies and want to switch to Swift packages using SwiftPM import, follow these steps:
Update your build script to include SwiftPM dependencies and the corresponding configuration
Reconfigure your Xcode project to use direct integration with the help of SwiftPM import tooling
Disable the CocoaPods integration entirely or partially, depending on your project structure
Update your build script
To update your build.gradle.kts file, follow the instructions on the SwiftPM import page:
For example, if you are using the FirebaseAnalytics pod:
Ensure you've set up the Kotlin Multiplatform Gradle plugin to use version 2.3.20-titan-222.
Add the
FirebaseAnalyticsSwift package to theswiftPMDependencies {}block:// projectDir/sharedLogic/build.gradle.kts kotlin { swiftPMDependencies { swiftPackage( url = url("https://github.com/firebase/firebase-ios-sdk.git"), version = from("12.5.0"), products = listOf(product("FirebaseAnalytics")), ) } cocoapods { // ... pod("FirebaseAnalytics") { version = "12.5.0" // ... } } }Run the Sync Project with Gradle Files action to import the APIs from the Swift package.
Update your code to use the APIs imported from the Swift package. If the pod and the corresponding Swift package offer exactly the same APIs, you should only need to update the Kotlin import directives, for example:
import cocoapods.FirebaseAnalytics.FIRAnalyticsimport swiftPMImport.org.example.package.FIRAnalyticsIf you are using the
cocoapods.framework {}block in your build script, move that configuration to thebinaries.framework {}block, for example:kotlin { iosArm64() iosSimulatorArm64() iosX64() cocoapods { framework { baseName = "Shared" isStatic = true } } }kotlin { listOf( iosArm64(), iosSimulatorArm64(), iosX64(), ).forEach { iosTarget -> iosTarget.binaries.framework { baseName = "Shared" isStatic = true } } }
Reconfigure your Xcode project
If you are using the CocoaPods Gradle plugin (kotlin("native.cocoapods")), you need to reconfigure your Xcode project to use direct integration before switching to SwiftPM. The SwiftPM import tooling can generate the shell command to make the necessary changes to your .xcodeproj file.
Open the project in Xcode (in IntelliJ IDEA, select File | Open Project in Xcode).
Build the project in Xcode (Product | Build). The build should fail, but the build error contains the necessary command.
To see the build errors in Xcode, select View | Navigators | Report, then choose Errors Only in the filter at the top. The command looks like this and includes the correct paths to your project:
XCODEPROJ_PATH='/path/to/project/iosApp/iosApp.xcodeproj' GRADLE_PROJECT_PATH=':kotlin-library' '/path/to/project/gradlew' -p '/path/to/project' ':kotlin-library:integrateEmbedAndSign' ':kotlin-library:integrateLinkagePackage'The
grepcall at the end finds the specific error message and the command that you need to run.In the
/path/to/project/iosAppdirectory, run the generated command in the terminal. It modifies the.xcodeprojfile of youriosAppproject to trigger theembedAndSignAppleFrameworkForXcodetask during the build, which inserts a Kotlin Multiplatform compilation phase into your iOS build.In IntelliJ IDEA, select Tools | Swift Package Manager | Resolve Dependencies to resolve the SwiftPM dependencies declared in your
build.gradle.ktsfile.
Now the iOS app uses SwiftPM dependencies. You can disable the CocoaPods plugin and deintegrate the pod.
Remove the CocoaPods KMP integration
If you have replaced all your CocoaPods dependencies with Swift packages, you can now deintegrate the pod by running the following command in the /path/to/project/iosApp directory:
If you want to continue using CocoaPods for dependencies that don't intersect with SwiftPM dependencies, edit your Podfile to remove only the line that mentions the KMP module, then run pod install. For example:
Finally, remove mentions of CocoaPods from your Gradle build configuration:
Remove the entire
cocoapods {}block from thebuild.gradle.ktsfile in your shared code module, since all dependencies are now managed by SwiftPM import tooling.If your project does not rely on CocoaPods anymore, remove references to the CocoaPods Gradle plugin from the
plugins {}block in both the rootbuild.gradle.ktsfile andbuild.gradle.ktsin the shared module.