Cocoapods integration in Kotlin Multiplatform project
In a Kotlin Multiplatform project we can add dependencies on Pod libraries stored in the Cocoapods repository or stored locally. We can manage the pod dependencies directly in Intellij Idea/Android Studio without having to switch to Xcode ever. In this post, I will show how to add Firebase Pods in a KMP project.
- Add Cocoapods plugin in build.gradle
plugins {
kotlin("native.cocoapods")
kotlin("multiplatform")
}
2. Add version
Version is required for .podspec file
version = "1.0.0"
3. Add cocoapods block inside Kotlin target
kotlin {
iOSTarget("ios") {}
cocoapods{
summary = "Sample KMP Project"
homepage = "https://github.com/sample/KMP-Project"
frameworkName = "SampleKMP"
pod("FirebaseCore")
pod("FirebaseAnalytics")
pod("FirebaseMessaging")
}}
Make sure to not define framework name inside iOSTarget block as this will conflict with frameworkName inside cocoapods block.
4. Run gradle task — ./gradlew podspec
5. Invalidate Caches/Restart and sync gradle
6. Finally import pods dependencies in Kotlin file
import cocoapods.FirebaseCore.FIRApp
import cocoapods.FirebaseMessaging.FIRMessaging
Below is a sample gradle file with all the above changes.
Hope this post helps someone in adding Cocoapods dependencies to KMP project!!