使用 Kotlin Gradle 项目作为 CocoaPods 依赖项
To use a Kotlin Multiplatform project with native targets as a CocoaPods dependency, complete the initial configuration. 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 a Kotlin Gradle project and an Xcode project with one or several targets. It’s also possible to add dependencies between a Gradle project 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.
- To correctly import the dependencies into the Kotlin/Native module, the
Podfile
must contain either use_modular_headers! or use_frameworks! directive.- If you don’t specify the minimum deployment target version and a dependency Pod requires a higher deployment target, you will get an error.
Xcode project with one target
- Create an Xcode project with a
Podfile
if you haven’t done so yet. - Add the path to your Xcode project
Podfile
withpodfile = project.file(..)
tobuild.gradle(.kts)
of your Kotlin project. This step helps synchronize your Xcode project with Gradle project dependencies by callingpod install
for yourPodfile
. Specify the minimum deployment target version for the Pod library.
kotlin {
ios()
cocoapods {
summary = "CocoaPods test library"
homepage = "https://github.com/JetBrains/kotlin"
ios.deploymentTarget = "13.5"
pod("Alamofire") {
version = "5.7.0"
}
podfile = project.file("../ios-app/Podfile")
}
}
Add the name and path of the Gradle project you want to include in the Xcode project to
Podfile
.use_frameworks!
platform :ios, '13.5'
target 'ios-app' do
pod 'kotlin_library', :path => '../kotlin-library'
end
Re-import the project.
Xcode project with several targets
- Create an Xcode project with a
Podfile
if you haven’t done so yet. - Add the path to your Xcode project
Podfile
withpodfile = project.file(..)
tobuild.gradle(.kts)
of your Kotlin project. This step helps synchronize your Xcode project with Gradle project dependencies by callingpod install
for yourPodfile
. - Add dependencies to the Pod libraries you want to use in your project with
pod()
. For each target, specify the minimum deployment 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("Alamofire") {
version = "5.7.0"
}
podfile = project.file("../severalTargetsXcodeProject/Podfile") // specify the path to the Podfile
}
}
Add the name and path of the Gradle project 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
Re-import the project.
You can find a sample project here.