|
| 1 | +[//]: # (title: Using Kotlin from local Swift packages) |
| 2 | + |
| 3 | +In this tutorial, you'll learn how to add a dependency on a Kotlin framework in a local package in the Swift package manager. |
| 4 | +This is useful if you want to simultaneously connect your Kotlin Multiplatform module to several SPM modules |
| 5 | +in your Xcode project. |
| 6 | + |
| 7 | +> If you aren't familiar with Kotlin Multiplatform, learn how to [set up the environment](multiplatform-setup.md) |
| 8 | +> and [create a cross-platform application from scratch](multiplatform-create-first-app.md) first. |
| 9 | +> |
| 10 | +{type="tip"} |
| 11 | + |
| 12 | +## Set up the project |
| 13 | + |
| 14 | +The feature is available starting with Kotlin 2.0.0-Beta2. |
| 15 | + |
| 16 | +If you still haven't upgraded to Kotlin 2.0, you can use a special Kotlin version, `1.9.24-spm` from the |
| 17 | +`https://packages.jetbrains.team/maven/p/mpp/dev` Maven repository. You can add the repository, for instance, |
| 18 | +in your `settings.gradle.kts`: |
| 19 | + |
| 20 | +```kotlin |
| 21 | +pluginManagement { |
| 22 | + repositories { |
| 23 | + maven("https://packages.jetbrains.team/maven/p/mpp/dev") |
| 24 | + //... |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +dependencyResolutionManagement { |
| 29 | + repositories { |
| 30 | + maven("https://packages.jetbrains.team/maven/p/mpp/dev") |
| 31 | + //... |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +The tutorial assumes that your project is using [direct integration](multiplatform-project-configuration.md#connect-a-kotlin-multiplatform-module-to-an-ios-app) |
| 37 | +approach with the `embedAndSignAppleFrameworkForXcode` task. If you're connecting a Kotlin framework through CocoaPods |
| 38 | +plugin, migrate first. |
| 39 | + |
| 40 | +### Migrate from CocoaPods plugin to direct integration {initial-collapse-state="collapsed"} |
| 41 | + |
| 42 | +> If you have dependencies on other Pods in the `cocoapods` block, you have to resort to the CocoaPods integration approach. |
| 43 | +> Currently, it's impossible to both have dependencies on Pods and on the Kotlin framework in a multimodal SPM project. |
| 44 | +> |
| 45 | +{type="warning"} |
| 46 | + |
| 47 | +To migrate from the CocoaPods plugin: |
| 48 | + |
| 49 | +1. In the directory with `Podfile`, run the following command: |
| 50 | + |
| 51 | + ```none |
| 52 | + pod deintegrate |
| 53 | + ``` |
| 54 | + |
| 55 | +2. Remove the `cocoapods {}` block from your `build.gradle(.kts)` files. |
| 56 | +3. Delete the `.podspec` and `Podfile` files. |
| 57 | +4. To set up direct integration, follow the steps described in [this tutorial](multiplatform-integrate-in-existing-app.md#connect-the-framework-to-your-ios-project). |
| 58 | + |
| 59 | +## Connect the framework to your project |
| 60 | + |
| 61 | +> The integration into `swift build` is currently not supported. |
| 62 | +> |
| 63 | +{type="note"} |
| 64 | + |
| 65 | +To connect the Kotlin framework to your Xcode project and use the Kotlin code in a local Swift package: |
| 66 | + |
| 67 | +1. In Xcode, go to **Product** | **Scheme** | **Edit scheme** or click the schemes icon in the top bar and select **Edit scheme**: |
| 68 | + |
| 69 | + {width=700} |
| 70 | + |
| 71 | +2. Select the **Build** | **Pre-actions** item, then click **+** | **New Run Script Action**: |
| 72 | + |
| 73 | + {width=700} |
| 74 | + |
| 75 | +3. Add the following script and replace `:shared` with your Gradle project name: |
| 76 | + |
| 77 | + ```bash |
| 78 | + cd "$SRCROOT/.." |
| 79 | + ./gradlew :shared:embedAndSignAppleFrameworkForXcode |
| 80 | + ``` |
| 81 | +4. Choose your app's target in the **Provide build settings from** section: |
| 82 | + |
| 83 | + {width=700} |
| 84 | + |
| 85 | +5. Use the code from Kotlin in the local Swift package added to your Xcode project: |
| 86 | + |
| 87 | + {width=700} |
| 88 | + |
| 89 | +6. Build the project in Xcode. If everything is set up correctly, the project build will be successful. |
| 90 | + |
| 91 | +> If you have a custom build configuration that is different from the default `Debug` or `Release`, on the **Build Settings** |
| 92 | +> tab, add the `KOTLIN_FRAMEWORK_BUILD_TYPE` setting under **User-Defined** and set it to `Debug` or `Release`. |
| 93 | +> |
| 94 | +{type="note"} |
| 95 | + |
| 96 | +## What's next |
| 97 | + |
| 98 | +* [Choose your project configuration](multiplatform-project-configuration.md) |
| 99 | +* [Learn how to set up Swift package export](https://kotlinlang.org/docs/native-spm.html) |
0 commit comments