Kotlin/Native provides integration with the CocoaPods dependency manager. You can add dependencies on Pod libraries stored in the CocoaPods repository or locally 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.
Depending on your project and purposes, you can add dependencies between:
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.
$ sudo gem install cocoapods
Install the cocoapods-generate
plugin.
$ sudo gem install cocoapods-generate
In build.gradle.kts
(or build.gradle
) of your IDEA project, apply the CocoaPods plugin as well as the Kotlin Multiplatform plugin.
plugins { kotlin("multiplatform") version "1.4.10" kotlin("native.cocoapods") version "1.4.10" }
Configure summary
, homepage
, and frameworkName
of the Podspec
file in the cocoapods
block.
version
is a version of the Gradle project.
plugins { kotlin("multiplatform") version "1.4.10" kotlin("native.cocoapods") version "1.4.10" } // CocoaPods requires the podspec to have a version. version = "1.0" kotlin { cocoapods { // Configure fields required by CocoaPods. summary = "Some description for a Kotlin/Native module" homepage = "Link to a Kotlin/Native module homepage" // You can change the name of the produced framework. // By default, it is the name of the Gradle project. frameworkName = "my_framework" } }
Re-import the project.
When applied, the CocoaPods plugin does the following:
debug
and release
frameworks as output binaries for all macOS, iOS, tvOS, and watchOS targets.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.
You can add dependencies between a Kotlin project and Pod libraries stored in the CocoaPods repository and stored locally.
Complete the initial configuration, and when you add a new dependency and re-import the project in IntelliJ IDEA; the new dependency will be added automatically. There are no additional steps required.
pod()
to build.gradle.kts
(build.gradle
) of your project. You can also add dependencies on subspecs. {:.note} >
kotlin { ios() cocoapods { summary = "CocoaPods test library" homepage = "https://github.com/JetBrains/kotlin" pod("AFNetworking", "~> 4.0.0") pod("SDWebImage/MapKit") } }
To use these dependencies from the Kotlin code, import the packages cocoapods.<library-name>
.
import cocoapods.AFNetworking.* import cocoapods.SDWebImage.*
You can find a sample project here.
pod()
to build.gradle.kts
(build.gradle
) of your project.Podspec
of the local Pod using project.file(..)
. You can add local dependencies on subspecs as well.
Thecocoapods
block can include dependencies to Pods stored locally and Pods from the CocoaPods repository at the same time.
kotlin { ios() cocoapods { summary = "CocoaPods test library" homepage = "https://github.com/JetBrains/kotlin" pod("pod_dependency", "1.0", project.file("../pod_dependency/pod_dependency.podspec")) pod("subspec_dependency/Core", "1.0", project.file("../subspec_dependency/subspec_dependency.podspec")) pod("AFNetworking", "~> 4.0.0") pod("SDWebImage/MapKit") } }
If you want to use dependencies on local pods from Kotlin code, import the corresponding packages.
import cocoapods.pod_dependency.* import cocoapods.subspec_dependency.*
You can find a sample project here.
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 eitheruse_modular_headers!
oruse_frameworks!
directive.
Podfile
if you haven’t done so yet.Podfile
with podfile = project.file(..)
to build.gradle.kts
(build.gradle
) of your Kotlin project.pod install
for your Podfile
.If you don't specify the minimum target version and a dependency Pod requires a higher deployment target, you may get an error.
kotlin { ios() cocoapods { summary = "CocoaPods test library" homepage = "https://github.com/JetBrains/kotlin" ios.deploymentTarget = "13.5" pod("AFNetworking", "~> 4.0.0") podfile = project.file("../ios-app/Podfile") } }
Add the name and path of the Kotlin Pod you want to include in the Xcode project to Podfile
.
use_frameworks! platform :ios, '9.0' target 'ios-app' do pod 'kotlin_library', :path => '../kotlin-library' end
Podfile
if you haven’t done so yet.Podfile
with podfile = project.file(..)
to build.gradle.kts
(build.gradle
) of your Kotlin project.pod install
for your Podfile
.pod()
.For each target, specify the minimum 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", "~> 4.0.0") podfile = project.file("../severalTargetsXcodeProject/Podfile") // specify the path to Podfile } }
Add the name and path of the Kotlin Pod 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
You can find a sample project here.
© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/docs/reference/native/cocoapods.html