CocoaPods 概述与设置

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 an environment to work with CocoaPods

Install the CocoaPods dependency manager using the installation tool of your choice:


【RVM】

  1. Install Ruby version manager in case you don't have it yet.
  2. Install Ruby. You can choose a specific version:

     rvm install ruby 3.0.0
    
  3. Install CocoaPods:

     sudo gem install -n /usr/local/bin cocoapods
    

【Rbenv】

  1. Install rbenv from GitHub in case you don't have it yet.
  2. Install Ruby. You can choose a specific version:

     rbenv install 3.0.0
    
  3. Set the Ruby version as local for a particular directory or global for the whole machine:

     rbenv global 3.0.0
    
  4. Install CocoaPods:

     sudo gem install -n /usr/local/bin cocoapods
    

【Default Ruby】

This way of installation doesn't work on devices with Apple M chips. Use other tools to set up an environment to work with CocoaPods.

You can install the CocoaPods dependency manager with the default Ruby that should be available on macOS:

sudo gem install cocoapods

【Homebrew】

The CocoaPods installation with Homebrew might result in compatibility issues.

When installing CocoaPods, Homebrew also installs the Xcodeproj gem that is necessary for working with Xcode. However, it cannot be updated with Homebrew, and if the installed Xcodeproj doesn't support the newest Xcode version yet, you'll get errors with Pod installation. If this is the case, try other tools to install CocoaPods.

  1. Install Homebrew in case you don't have it yet.

  2. Install CocoaPods:

     brew install cocoapods
    

If your current version of Kotlin is earlier than 1.7.0, additionally install the cocoapods-generate plugin:

sudo gem install -n /usr/local/bin cocoapods-generate

Mind that cocoapods-generate couldn't be installed on Ruby 3.0.0 and later. If it's your case, downgrade Ruby or upgrade Kotlin to 1.7.0 or later.

If you encounter problems during the installation, check the Possible issues and solutions section.

Add and configure Kotlin CocoaPods Gradle plugin

If your environment is set up correctly, you can create a new Kotlin Multiplatform project and choose CocoaPods Dependency Manager as the iOS framework distribution option. The plugin will automatically generate the project for you.

If you want to configure your project manually:

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

     plugins {
         kotlin("multiplatform") version "1.9.24"
         kotlin("native.cocoapods") version "1.9.24"
     }
    
  2. Configure version, summary, homepage, and baseName of the Podspec file in the cocoapods block:

     plugins {
         kotlin("multiplatform") version "1.9.24"
         kotlin("native.cocoapods") version "1.9.24"
     }
    
     kotlin {
         cocoapods {
             // Required properties
             // Specify the required Pod version here. Otherwise, the Gradle project version is used.
             version = "1.0"
             summary = "Some description for a Kotlin/Native module"
             homepage = "Link to a Kotlin/Native module homepage"
    
             // Optional properties
             // Configure the Pod name here instead of changing the Gradle project name
             name = "MyCocoaPod"
    
             framework {
                 // Required properties              
                 // Framework name configuration. Use this property instead of deprecated 'frameworkName'
                 baseName = "MyFramework"
    
                 // Optional properties
                 // Specify the framework linking type. It's dynamic by default. 
                 isStatic = false
                 // Dependency export
                 export(project(":anotherKMMModule"))
                 transitiveExport = false // This is default.
                 // Bitcode embedding
                 embedBitcode(BITCODE)
             }
    
             // Maps custom Xcode configuration to NativeBuildType
             xcodeConfigurationToNativeBuildType["CUSTOM_DEBUG"] = NativeBuildType.DEBUG
             xcodeConfigurationToNativeBuildType["CUSTOM_RELEASE"] = NativeBuildType.RELEASE
         }
     }
    

    See the full syntax of Kotlin DSL in the Kotlin Gradle plugin repository.

  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 dependencies ...
          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 dependencies ...
          pod 'example'
      end
    

Re-import the project after making changes in the 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.

Possible issues and solutions

CocoaPods installation

Ruby installation

CocoaPods is built with Ruby, and you can install it with the default Ruby that should be available on macOS. Ruby 1.9 or later has a built-in RubyGems package management framework that helps you install the CocoaPods dependency manager.

If you're experiencing problems installing CocoaPods and getting it to work, follow this guide to install Ruby or refer to the RubyGems website to install the framework.

Version compatibility

We recommend using the latest Kotlin version. If your current version is earlier than 1.7.0, you'll need to additionally install the cocoapods-generate plugin.

However, cocoapods-generate is not compatible with Ruby 3.0.0 or later. In this case, downgrade Ruby or upgrade Kotlin to 1.7.0 or later.

Module not found

You may encounter a module 'SomeSDK' not found error that is connected with the C-interop issue. Try these workarounds to avoid this error:

Specify the framework name

  1. Look through the downloaded Pod directory [shared_module_name]/build/cocoapods/synthetic/IOS/Pods/... for the module.modulemap file.
  2. Check the framework name inside the module, for example AppsFlyerLib {}. If the framework name doesn't match the Pod name, specify it explicitly:

     pod("FirebaseAuth") {
         moduleName = "AppsFlyerLib"
     }
    

    Specify headers

If the Pod doesn't contain a .modulemap file, like the pod("NearbyMessages"), specify the main header explicitly:

pod("NearbyMessages") {
    version = "1.1.1"
    headers = "GNSMessages.h"
}

Check the CocoaPods documentation for more information. If nothing works, and you still encounter this error, report an issue in YouTrack.

Rsync error

You might encounter the rsync error: some files could not be transferred error. It's a known issue that occurs if the application target in Xcode has sandboxing of the user scripts enabled.

To solve this issue:

  1. Disable sandboxing of user scripts in the application target:

    Disable sandboxing CocoaPods

  2. Stop the Gradle daemon process that might have been sandboxed:

     ./gradlew --stop