CocoaPods integration

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 (Kotlin Pod).

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 Pod and an Xcode project.

You can also add dependencies between a Kotlin Pod 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.

Install the CocoaPods dependency manager and plugin

  1. Install the CocoaPods dependency manager.

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

    1. $ sudo gem install cocoapods-generate
  3. In build.gradle.kts (or build.gradle) of your IDEA project, apply the CocoaPods plugin as well as the Kotlin Multiplatform plugin.

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

    1. plugins {
    2. kotlin("multiplatform") version "1.4.21"
    3. kotlin("native.cocoapods") version "1.4.21"
    4. }
    5. // CocoaPods requires the podspec to have a version.
    6. version = "1.0"
    7. kotlin {
    8. cocoapods {
    9. // Configure fields required by CocoaPods.
    10. summary = "Some description for a Kotlin/Native module"
    11. homepage = "Link to a Kotlin/Native module homepage"
    12. // You can change the name of the produced framework.
    13. // By default, it is the name of the Gradle project.
    14. frameworkName = "my_framework"
    15. }
    16. }
  5. Re-import the project.

  6. 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.

Add dependencies on Pod libraries

To add dependencies between a Kotlin project and a Pod library, you should complete the initial configuration. This allows you to add dependencies on the following types of Pod libraries:

A Kotlin project requires the pod() function call in build.gradle.kts (build.gradle) for adding a Pod dependency. Each dependency requires its own separate function call. You can specify the parameters for the dependency in the configuration block of the function.

When you add a new dependency and re-import the project in IntelliJ IDEA, the new dependency will be added automatically. No additional steps are required.

To use your Kotlin project with Xcode, you should make changes in your project Podfile.

Add a dependency on a Pod library from the CocoaPods repository

You can add dependencies on a Pod library from the CocoaPods repository with pod() to build.gradle.kts (build.gradle) of your project:

  1. Specify the name of a Pod library in the pod() function. In the configuration block you can specify the version of the library using the version parameter. To use the latest version of the library, you can just omit this parameter all-together.

    You can add dependencies on subspecs.

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

    If you don’t specify the minimum deployment target version and a dependency Pod requires a higher deployment target, you will get an error.

    1. kotlin {
    2. ios()
    3. cocoapods {
    4. ios.deploymentTarget = "13.5"
    5. summary = "CocoaPods test library"
    6. homepage = "https://github.com/JetBrains/kotlin"
    7. pod("AFNetworking") {
    8. version = "~> 4.0.1"
    9. }
    10. }
    11. }
  3. Re-import the project.

To use these dependencies from the Kotlin code, import the packages cocoapods.<library-name>.

  1. import cocoapods.AFNetworking.*

You can find a sample project here.

Add a dependency on a Pod library stored locally

You can add a dependency on a Pod library stored locally with pod() to build.gradle.kts (build.gradle) of your project:

  1. Specify the name of a Pod library in the pod() function. In the configuration block specify the path to the local Pod library: use the path() function in the source parameter value.

    You can add local dependencies on subspecs as well. The cocoapods block can include dependencies to Pods stored locally and Pods from the CocoaPods repository at the same time.

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

    If you don’t specify the minimum deployment target version and a dependency Pod requires a higher deployment target, you will get an error.

    1. kotlin {
    2. ios()
    3. cocoapods {
    4. summary = "CocoaPods test library"
    5. homepage = "https://github.com/JetBrains/kotlin"
    6. ios.deploymentTarget = "13.5"
    7. pod("pod_dependency") {
    8. version = "1.0"
    9. source = path(project.file("../pod_dependency/pod_dependency.podspec"))
    10. }
    11. pod("subspec_dependency/Core") {
    12. version = "1.0"
    13. source = path(project.file("../subspec_dependency/subspec_dependency.podspec"))
    14. }
    15. pod("AFNetworking") {
    16. version = "~> 4.0.1"
    17. }
    18. }
    19. }

    You can also specify the version of the library using version parameter in the configuration block. To use the latest version of the library, omit the parameter.

  3. Re-import the project.

To use these dependencies from the Kotlin code, import the packages cocoapods.<library-name>.

  1. import cocoapods.pod_dependency.*
  2. import cocoapods.subspec_dependency.*
  3. import cocoapods.AFNetworking.*

You can find a sample project here.

Add a dependency on a Pod library from the Git repository

You can add dependencies on a Pod library from a custom Git repository with pod() to build.gradle.kts (build.gradle) of your project:

  1. Specify the name of a Pod library in the pod() function. In the configuration block specify the path to the git repository: use the git() function in the source parameter value.

    Additionally, you can specify the following parameters in the block after git():

    • commit – to use a specific commit from the repository
    • tag – to use a specific tag from the repository
    • branch – to use a specific branch from the repository

    The git() function prioritizes passed parameters in the following order: commit, tag, branch. If you don’t specify a parameter, the Kotlin plugin uses HEAD from the master branch.

    You can combine branch, commit, and tag parameters to get the specific version of a Pod.

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

    If you don’t specify the minimum deployment target version and a dependency Pod requires a higher deployment target, you will get an error.

    1. kotlin {
    2. ios()
    3. cocoapods {
    4. summary = "CocoaPods test library"
    5. homepage = "https://github.com/JetBrains/kotlin"
    6. ios.deploymentTarget = "13.5"
    7. pod("AFNetworking") {
    8. source = git("https://github.com/AFNetworking/AFNetworking") {
    9. tag = "4.0.0"
    10. }
    11. }
    12. pod("JSONModel") {
    13. source = git("https://github.com/jsonmodel/jsonmodel.git") {
    14. branch = "key-mapper-class"
    15. }
    16. }
    17. pod("CocoaLumberjack") {
    18. source = git("https://github.com/CocoaLumberjack/CocoaLumberjack.git") {
    19. commit = "3e7f595e3a459c39b917aacf9856cd2a48c4dbf3"
    20. }
    21. }
    22. }
    23. }
  3. Re-import the project.

To work correctly with Xcode, you should specify the path to the Podspec in your Podfile. For example:

  1. target 'ios-app' do
  2. # ... other pod depedencies ...
  3. pod 'JSONModel', :path => '../cocoapods/kotlin-with-cocoapods-sample/kotlin-library/build/cocoapods/externalSources/git/JSONModel'
  4. end

To use these dependencies from the Kotlin code, import the packages cocoapods.<library-name>.

  1. import cocoapods.AFNetworking.*
  2. import cocoapods.JSONModel.*
  3. import cocoapods.CocoaLumberjack.*

You can find a sample project here.

Add a dependency on a Pod library from an archive

You can add dependencies on a Pod library from zip, tar, or jar archive with pod() to build.gradle.kts (build.gradle) of your project:

  1. Specify the name of a Pod library in the pod() function. In the configuration block specify the path to the archive: use the url() function with an arbitrary HTTP address in the source parameter value.

    Additionally, you can specify the boolean flatten parameter as a second argument for the url() function This parameter indicates that all the Pod files are located in the root directory of the archive.

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

    If you don’t specify the minimum deployment target version and a dependency Pod requires a higher deployment target, you will get an error.

    1. kotlin {
    2. ios()
    3. cocoapods {
    4. summary = "CocoaPods test library"
    5. homepage = "https://github.com/JetBrains/kotlin"
    6. ios.deploymentTarget = "13.5"
    7. pod("pod_dependency") {
    8. source = url("https://github.com/Kotlin/kotlin-with-cocoapods-sample/raw/cocoapods-zip/cocoapodSourcesZip.zip", flatten = true)
    9. }
    10. }
    11. }
  3. Re-import the project.

To work correctly with Xcode, you should specify the path to the Podspec in your Podfile. For example:

  1. target 'ios-app' do
  2. # ... other pod depedencies ...
  3. pod 'podspecWithFilesExample', :path => '../cocoapods/kotlin-with-cocoapods-sample/pod_dependency'
  4. end

To use these dependencies from the Kotlin code, import the packages cocoapods.<library-name>.

  1. import cocoapods.pod_dependency.*

You can find a sample project here.

Add a dependency on a Pod library from a custom Podspec repository

You can add dependencies on a Pod library from a custom Podspec repository with pod() and specRepos to build.gradle.kts (build.gradle) of your project:

  1. Specify the HTTP address to the custom Podspec repository using the url() inside the specRepos block.

  2. Specify the name of a Pod library in the pod() function.

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

    If you don’t specify the minimum deployment target version and a dependency Pod requires a higher deployment target, you will get an error.

    1. kotlin {
    2. ios()
    3. cocoapods {
    4. summary = "CocoaPods test library"
    5. homepage = "https://github.com/JetBrains/kotlin"
    6. ios.deploymentTarget = "13.5"
    7. specRepos {
    8. url("https://github.com/Kotlin/kotlin-cocoapods-spec.git")
    9. }
    10. pod("example")
    11. }
    12. }
  4. Re-import the project.

To work correctly with Xcode, you should specify the location of specs at the beginning of your Podfile. For example, source 'https://github.com/Kotlin/kotlin-cocoapods-spec.git'

You should also specify the path to the Podspec in your Podfile. For example:

  1. target 'ios-app' do
  2. # ... other pod depedencies ...
  3. pod 'podspecWithFilesExample', :path => '../cocoapods/kotlin-with-cocoapods-sample/pod_dependency'
  4. end

To use these dependencies from the Kotlin code, import the packages cocoapods.<library-name>.

  1. import cocoapods.example.*

You can find a sample project here.

Add a dependency on a Pod library with custom cinterop options

You can add dependencies on a Pod library with custom cinterop options with pod() to build.gradle.kts (build.gradle) of your project:

  1. Specify the name of a Pod library in the pod() function. In the configuration block specify the cinterop options:

    • extraOpts – to specify the list of options for a Pod library. For example, specific flags: extraOpts = listOf("-compiler-option")
    • packageName – to specify the package name. If you specify this, you can import the library using the package name: import <packageName>.
  2. Specify the minimum deployment target version for the Pod library.

    If you don’t specify the minimum deployment target version and a dependency Pod requires a higher deployment target, you will get an error.

    1. kotlin {
    2. ios()
    3. cocoapods {
    4. summary = "CocoaPods test library"
    5. homepage = "https://github.com/JetBrains/kotlin"
    6. ios.deploymentTarget = "13.5"
    7. useLibraries()
    8. pod("YandexMapKit") {
    9. packageName = "YandexMK"
    10. }
    11. }
    12. }
  3. Re-import the project.

To use these dependencies from the Kotlin code, import the packages cocoapods.<library-name>.

  1. import cocoapods.YandexMapKit.*

If you use the packageName parameter, you can import the library using the package name: import <packageName>:

  1. import YandexMK.YMKPoint
  2. import YandexMK.YMKDistance

Add a dependency on a static Pod library

You can add dependencies on a static Pod library with pod() and useLibraries() to build.gradle.kts (build.gradle) of your project:

  1. Specify the name of the library using the pod() function.

  2. Call the useLibraries() function: it enables a special flag for static libraries.

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

    If you don’t specify the minimum deployment target version and a dependency Pod requires a higher deployment target, you will get an error.

    1. kotlin {
    2. ios()
    3. cocoapods {
    4. summary = "CocoaPods test library"
    5. homepage = "https://github.com/JetBrains/kotlin"
    6. ios.deploymentTarget = "13.5"
    7. pod("YandexMapKit") {
    8. version = "~> 3.2"
    9. }
    10. useLibraries()
    11. }
    12. }
  4. Re-import the project.

To use these dependencies from the Kotlin code, import the packages cocoapods.<library-name>.

  1. import cocoapods.YandexMapKit.*

Update Podfile for Xcode

If you want to import your Kotlin project in an Xcode project, you’ll need to make some changes to your Podfile for it to work correctly:

  • 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:

    1. target 'ios-app' do
    2. # ... other depedencies ...
    3. pod 'podspecWithFilesExample', :path => 'cocoapods/externalSources/url/podspecWithFilesExample'
    4. 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:

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

Re-import the project after making changes in Podfile.

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.

Use a Kotlin Gradle project as a CocoaPods dependency

You can use a Kotlin Multiplatform project with native targets as a CocoaPods dependency (Kotlin Pod). 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:

To correctly import the dependencies into the Kotlin/Native module, the Podfile must contain either use_modular_headers! or use_frameworks! directive.

Add a dependency between a Kotlin Pod and 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 Kotlin Pod dependencies by calling pod install for your Podfile.
  3. Specify the minimum deployment target version for the Pod library.

    If you don’t specify the minimum deployment target version and a dependency Pod requires a higher deployment target, you will get an error.

    1. kotlin {
    2. ios()
    3. cocoapods {
    4. summary = "CocoaPods test library"
    5. homepage = "https://github.com/JetBrains/kotlin"
    6. ios.deploymentTarget = "13.5"
    7. pod("AFNetworking") {
    8. version = "~> 4.0.0"
    9. }
    10. podfile = project.file("../ios-app/Podfile")
    11. }
    12. }
  4. Add the name and path of the Kotlin Pod you want to include in the Xcode project to Podfile.

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

Add a dependency between a Kotlin Pod with an 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 Kotlin Pod dependencies by calling pod install for your Podfile.
  3. Add dependencies to the Pod libraries that you want to use in your project with pod().
  4. For each target, specify the minimum deployment target version for the Pod library.

    If you don’t specify the minimum deployment target version and a dependency Pod requires a higher deployment target, you will get an error.

    1. kotlin {
    2. ios()
    3. tvos()
    4. cocoapods {
    5. summary = "CocoaPods test library"
    6. homepage = "https://github.com/JetBrains/kotlin"
    7. ios.deploymentTarget = "13.5"
    8. tvos.deploymentTarget = "13.4"
    9. pod("AFNetworking") {
    10. version = "~> 4.0.0"
    11. }
    12. podfile = project.file("../severalTargetsXcodeProject/Podfile") // specify the path to Podfile
    13. }
    14. }
  5. Add the name and path of the Kotlin Pod you want to include in the Xcode project to the Podfile.

    1. target 'iosApp' do
    2. use_frameworks!
    3. platform :ios, '13.5'
    4. # Pods for iosApp
    5. pod 'kotlin_library', :path => '../kotlin-library'
    6. end
    7. target 'TVosApp' do
    8. use_frameworks!
    9. platform :tvos, '13.4'
    10. # Pods for TVosApp
    11. pod 'kotlin_library', :path => '../kotlin-library'
    12. end
  6. Re-import the project.

You can find a sample project here.