Add dependencies on a Pod library
You can add dependencies on Pod libraries from different locations in your Kotlin project.
To add a Pod dependency, call the pod()
function in the shared module's build.gradle(.kts)
file. Each dependency requires a 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 your IDE, the library will be connected automatically.
To use your Kotlin project with Xcode, make changes in your project's Podfile first.
From the CocoaPods repository
To add a dependency on a Pod library located in the CocoaPods repository:
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 omit this parameter altogether.Specify the minimum deployment target version for the Pod library.
kotlin { iosArm64() cocoapods { version = "2.0" summary = "CocoaPods test library" homepage = "https://github.com/JetBrains/kotlin" ios.deploymentTarget = "16.0" pod("SDWebImage") { version = "5.20.0" } } }Run Build | Reload All Gradle Projects in IntelliJ IDEA (or File | Sync Project with Gradle Files in Android Studio) to re-import the project.
To use these dependencies from the Kotlin code, import the packages cocoapods.<library-name>
:
On a locally stored library
To add a dependency on a locally stored Pod library:
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 thesource
parameter value.Specify the minimum deployment target version for the Pod library.
kotlin { iosArm64() cocoapods { version = "2.0" summary = "CocoaPods test library" homepage = "https://github.com/JetBrains/kotlin" ios.deploymentTarget = "16.0" pod("pod_dependency") { version = "1.0" extraOpts += listOf("-compiler-option") source = path(project.file("../pod_dependency")) } pod("subspec_dependency/Core") { version = "1.0" extraOpts += listOf("-compiler-option") source = path(project.file("../subspec_dependency")) } pod("SDWebImage") { version = "5.20.0" } } }Run Build | Reload All Gradle Projects in IntelliJ IDEA (or File | Sync Project with Gradle Files in Android Studio) to re-import the project.
To use these dependencies from the Kotlin code, import the packages cocoapods.<library-name>
:
From a custom Git repository
To add a dependency on a Pod library located in the custom Git repository:
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 thesource
parameter value.Additionally, you can specify the following parameters in the block after
git()
:commit
– to use a specific commit from the repositorytag
– to use a specific tag from the repositorybranch
– 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 usesHEAD
from themaster
branch.Specify the minimum deployment target version for the Pod library.
kotlin { iosArm64() cocoapods { version = "2.0" summary = "CocoaPods test library" homepage = "https://github.com/JetBrains/kotlin" ios.deploymentTarget = "16.0" pod("SDWebImage") { source = git("https://github.com/SDWebImage/SDWebImage") { tag = "5.20.0" } } pod("JSONModel") { source = git("https://github.com/jsonmodel/jsonmodel.git") { branch = "key-mapper-class" } } pod("CocoaLumberjack") { source = git("https://github.com/CocoaLumberjack/CocoaLumberjack.git") { commit = "3e7f595e3a459c39b917aacf9856cd2a48c4dbf3" } } } }Run Build | Reload All Gradle Projects in IntelliJ IDEA (or File | Sync Project with Gradle Files in Android Studio) to re-import the project.
To use these dependencies from the Kotlin code, import the packages cocoapods.<library-name>
:
From a custom Podspec repository
To add a dependency on a Pod library located in the custom Podspec repository:
Specify the address of the custom Podspec repository using a
url()
call inside thespecRepos {}
block.Specify the name of a Pod library in the
pod()
function.Specify the minimum deployment target version for the Pod library.
kotlin { iosArm64() cocoapods { version = "2.0" summary = "CocoaPods test library" homepage = "https://github.com/JetBrains/kotlin" ios.deploymentTarget = "16.0" specRepos { url("https://github.com/Kotlin/kotlin-cocoapods-spec.git") } pod("example") } }Run Build | Reload All Gradle Projects in IntelliJ IDEA (or File | Sync Project with Gradle Files in Android Studio) to re-import the project.
To use these dependencies from the Kotlin code, import the packages cocoapods.<library-name>
:
With custom cinterop options
To add a dependency on a Pod library using custom cinterop options:
Specify the name of a Pod library in the
pod()
function.In the configuration block, add the following options:
extraOpts
– to specify the list of options for a Pod library. For example,extraOpts = listOf("-compiler-option")
.packageName
– to import the library directly using the package name withimport <packageName>
.
Specify the minimum deployment target version for the Pod library.
kotlin { iosArm64() cocoapods { version = "2.0" summary = "CocoaPods test library" homepage = "https://github.com/JetBrains/kotlin" ios.deploymentTarget = "16.0" pod("FirebaseAuth") { packageName = "FirebaseAuthWrapper" version = "11.7.0" extraOpts += listOf("-compiler-option", "-fmodules") } } }Run Build | Reload All Gradle Projects in IntelliJ IDEA (or File | Sync Project with Gradle Files in Android Studio) to re-import the project.
To use these dependencies from the Kotlin code, import the packages cocoapods.<library-name>
:
If you use the packageName
parameter, you can import the library using the package name import <packageName>
:
Support for Objective-C headers with @import directives
Some Objective-C libraries, specifically those that serve as wrappers for Swift libraries, have @import
directives in their headers. By default, cinterop doesn't provide support for these directives.
To enable support for @import
directives, specify the -fmodules
option in the configuration block of the pod()
function:
Share Kotlin cinterop between dependent Pods
If you add multiple dependencies on Pods using the pod()
function, you might encounter issues when there are dependencies between APIs of your Pods.
To make the code compile in such cases, use the useInteropBindingFrom()
function. It utilizes the cinterop binding generated for another Pod while building a binding for the new Pod.
You should declare the dependent Pod before setting up the dependency:
If you haven't configured the correct dependencies between cinterops in this case, the code would be invalid because the WebImage
type would be sourced from different cinterop files and, consequently, different packages.