Kotlin Help

CocoaPods overview

Kotlin/Native provides integration with the CocoaPods dependency manager. You can add dependencies on Pod libraries as well as use a multiplatform project with native targets as a CocoaPods dependency.

You can manage Pod dependencies directly in IntelliJ IDEA and enjoy all the additional features such as code highlighting and completion. You can build the whole Kotlin project with Gradle and not ever have to switch to Xcode.

Use Xcode only when you need to write Swift/Objective-C code or run your application on a simulator or device. To work correctly with Xcode, you should update your Podfile.

Depending on your project and purposes, you can add dependencies between a Kotlin project and a Pod library as well as a Kotlin Gradle project and an Xcode project.

Set up the environment to work with CocoaPods

  1. Install the CocoaPods dependency manager:

    $ sudo gem install cocoapods
  2. Install the cocoapods-generate plugin:

    $ sudo gem install cocoapods-generate

Add and configure Kotlin CocoaPods Gradle plugin

  1. In build.gradle(.kts) of your project, apply the CocoaPods plugin as well as the Kotlin Multiplatform plugin:

    plugins { kotlin("multiplatform") version "1.6.10" kotlin("native.cocoapods") version "1.6.10" }
  2. Configure summary, homepage, and frameworkNameof the Podspec file in the cocoapods block.
    version is a version of the Gradle project:

    plugins { kotlin("multiplatform") version "1.6.10" kotlin("native.cocoapods") version "1.6.10" } // CocoaPods requires the podspec to have a version. version = "1.0" kotlin { cocoapods { framework { // Mandatory properties // Configure fields required by CocoaPods. summary = "Some description for a Kotlin/Native module" homepage = "Link to a Kotlin/Native module homepage" // Framework name configuration. Use this property instead of deprecated 'frameworkName' baseName = "MyFramework" // Optional properties // (Optional) Dynamic framework support isStatic = false // (Optional) Dependency export export(project(":anotherKMMModule")) transitiveExport = true // (Optional) Bitcode embedding embedBitcode(BITCODE) } // Maps custom Xcode configuration to NativeBuildType xcodeConfigurationToNativeBuildType["CUSTOM_DEBUG"] = NativeBuildType.DEBUG xcodeConfigurationToNativeBuildType["CUSTOM_RELEASE"] = NativeBuildType.RELEASE } }
  3. Re-import the project.

  4. Generate the Gradle wrapper to avoid compatibility issues during an Xcode build.

When applied, the CocoaPods plugin does the following:

  • Adds both debug and release frameworks as output binaries for all macOS, iOS, tvOS, and watchOS targets.

  • Creates a podspec task which generates a Podspec file for the project.

The Podspec file includes a path to an output framework and script phases that automate building this framework during the build process of an Xcode project.

Update Podfile for Xcode

If you want to import your Kotlin project in an Xcode project, you need to make some changes to your Podfile:

  • If your project has any Git, HTTP, or custom Podspec repository dependencies, you should also specify the path to the Podspec in the Podfile.

    For example, if you add a dependency on podspecWithFilesExample, declare the path to the Podspec in the Podfile:

    target 'ios-app' do # ... other depedencies ... pod 'podspecWithFilesExample', :path => 'cocoapods/externalSources/url/podspecWithFilesExample' end

    The :path should contain the filepath to the Pod.

  • When you add a library from the custom Podspec repository, you should also specify the location of specs at the beginning of your Podfile:

    source 'https://github.com/Kotlin/kotlin-cocoapods-spec.git' target 'kotlin-cocoapods-xcproj' do # ... other depedencies ... pod 'example' end

If you don't make these changes to the Podfile, the podInstall task will fail, and the CocoaPods plugin will show an error message in the log.

Check out the withXcproject branch of the sample project, which contains an example of Xcode integration with an existing Xcode project named kotlin-cocoapods-xcproj.

Last modified: 20 January 2022