Kotlin Help

Use a Kotlin Gradle project as a CocoaPods dependency

You can use a Kotlin Multiplatform project with native targets as a CocoaPods dependency. You can include such a dependency in the Podfile of the Xcode project by its name and path to the project directory containing the generated Podspec.

This dependency will be automatically built (and rebuilt) along with this project. Such an approach simplifies importing to Xcode by removing a need to write the corresponding Gradle tasks and Xcode build steps manually.

You can add dependencies between a Kotlin Gradle project and an Xcode project with one or several targets. It's also possible to add dependencies between a Gradle project and multiple Xcode projects. However, in this case, you need to add a dependency by calling pod install manually for each Xcode project. In other cases, it's done automatically.

Xcode project with one target

  1. Create an Xcode project with a Podfile if you haven’t done so yet.

  2. Add the path to your Xcode project Podfile with podfile = project.file(..) to build.gradle.kts (build.gradle) of your Kotlin project. This step helps synchronize your Xcode project with Gradle project dependencies by calling pod install for your Podfile.

  3. Specify the minimum deployment target version for the Pod library.

    kotlin { ios() cocoapods { summary = "CocoaPods test library" homepage = "https://github.com/JetBrains/kotlin" ios.deploymentTarget = "13.5" pod("AFNetworking") { version = "~> 4.0.0" } podfile = project.file("../ios-app/Podfile") } }
  4. Add the name and path of the Gradle project you want to include in the Xcode project to Podfile.

    use_frameworks! platform :ios, '13.5' target 'ios-app' do pod 'kotlin_library', :path => '../kotlin-library' end
  5. Re-import the project.

Xcode project with several targets

  1. Create an Xcode project with a Podfile if you haven’t done so yet.

  2. Add the path to your Xcode project Podfile with podfile = project.file(..) to build.gradle.kts (build.gradle) of your Kotlin project. This step helps synchronize your Xcode project with Gradle project dependencies by calling pod install for your Podfile.

  3. Add dependencies to the Pod libraries you want to use in your project with pod().

  4. For each target, specify the minimum deployment target version for the Pod library.

    kotlin { ios() tvos() cocoapods { summary = "CocoaPods test library" homepage = "https://github.com/JetBrains/kotlin" ios.deploymentTarget = "13.5" tvos.deploymentTarget = "13.4" pod("AFNetworking") { version = "~> 4.0.0" } podfile = project.file("../severalTargetsXcodeProject/Podfile") // specify the path to the Podfile } }
  5. Add the name and path of the Gradle project you want to include in the Xcode project to the Podfile.

    target 'iosApp' do use_frameworks! platform :ios, '13.5' # Pods for iosApp pod 'kotlin_library', :path => '../kotlin-library' end target 'TVosApp' do use_frameworks! platform :tvos, '13.4' # Pods for TVosApp pod 'kotlin_library', :path => '../kotlin-library' end
  6. Re-import the project.

You can find a sample project here.

Last modified: 04 April 2022