直接集成
* You've already set up a Kotlin Multiplatform project targeting iOS on your local machine. * Your Kotlin Multiplatform project does not have CocoaPods dependencies.
[Choose the integration method that suits you best](multiplatform-ios-integration-overview.md)
If you want to develop your Kotlin Multiplatform project and an iOS project simultaneously by sharing code between them, you can set up direct integration using a special script.
This script automates the process of connecting the Kotlin framework to iOS projects in Xcode:
The script uses the embedAndSignAppleFrameworkForXcode
Gradle task designed specifically for the Xcode environment.
During the setup, you add it to the run script phase of the iOS app build. After that, the Kotlin artifact
is built and included in the derived data before running the iOS app build.
In general, the script:
- Copies the compiled Kotlin framework into the correct directory within the iOS project structure.
- Handles the code signing process of the embedded framework.
- Ensures that code changes in the Kotlin framework are reflected in the iOS app in Xcode.
How to set up
If you're currently using the CocoaPods plugin to connect your Kotlin framework, migrate first. If your project doesn't have CocoaPods dependencies, skip this step.
Migrate from the CocoaPods plugin
To migrate from the CocoaPods plugin:
- In Xcode, clean build directories using Product | Clean Build Folder or with the
Cmd + Shift + K shortcut. In the directory with the Podfile, run the following command:
pod deintegrate
Remove the
cocoapods {}
block from yourbuild.gradle(.kts)
files.- Delete the
.podspec
file and the Podfile.
Connect the framework to your project
To connect the Kotlin framework generated from the multiplatform project to your Xcode project:
- The
embedAndSignAppleFrameworkForXcode
task only registers if thebinaries.framework
configuration option is declared. In your Kotlin Multiplatform project, check the iOS target declaration in thebuild.gradle.kts
file. - In Xcode, open the iOS project settings by double-clicking the project name.
On the Build Phases tab of the project settings, click + and select New Run Script Phase.
Adjust the following script and copy the result to the run script phase:
cd "
" ./gradlew : :embedAndSignAppleFrameworkForXcode - In the
cd
command, specify the path to the root of your Kotlin Multiplatform project, for example,$SRCROOT/..
. - In the
./gradlew
command, specify the name of the shared module, for example,:shared
or:composeApp
.
- In the
Drag the Run Script phase before the Compile Sources phase.
On the Build Settings tab, disable the User Script Sandboxing option under Build Options:
This may require restarting your Gradle daemon if you built the iOS project without disabling sandboxing first. Stop the Gradle daemon process that might have been sandboxed:
./gradlew --stop
{style="tip"}
Build the project in Xcode. If everything is set up correctly, the project will successfully build.
If you have a custom build configuration different from the default
Debug
orRelease
, on the Build Settings tab, add theKOTLIN_FRAMEWORK_BUILD_TYPE
setting under User-Defined and set it toDebug
orRelease
.{style="note"}
What's next?
You can also take advantage of local integration when working with the Swift package manager. Learn how to add a dependency on a Kotlin framework in a local package.