Skip to content

Commit 92f78fa

Browse files
authored
update: recommendations on ios dependencies and minor changes (#4699)
1 parent c0eb896 commit 92f78fa

File tree

4 files changed

+83
-87
lines changed

4 files changed

+83
-87
lines changed

docs/topics/multiplatform/multiplatform-direct-integration.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ In general, the script:
2929
## How to set up
3030

3131
If you're currently using the CocoaPods plugin to connect your Kotlin framework, migrate first.
32+
If your project doesn't have CocoaPods dependencies, [skip this step](#connect-the-framework-to-your-project).
3233

33-
### Migrate from CocoaPods plugin to direct integration {initial-collapse-state="collapsed" collapsible="true"}
34+
### Migrate from the CocoaPods plugin
3435

3536
To migrate from the CocoaPods plugin:
3637

@@ -76,7 +77,7 @@ To connect the Kotlin framework generated from the multiplatform project to your
7677

7778
![User Script Sandboxing](disable-sandboxing-in-xcode-project-settings.png){width=700}
7879

79-
> This may require restarting your Gradle daemon, if you built the iOS project without disabling sandboxing first.
80+
> This may require restarting your Gradle daemon if you built the iOS project without disabling sandboxing first.
8081
> Stop the Gradle daemon process that might have been sandboxed:
8182
> ```shell
8283
> ./gradlew --stop

docs/topics/multiplatform/multiplatform-ios-dependencies.md

Lines changed: 71 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,103 +7,43 @@ You can also reuse other libraries and frameworks from the iOS ecosystem in your
77
interoperability with Objective-C dependencies and Swift dependencies if their APIs are exported to Objective-C with
88
the `@objc` attribute. Pure Swift dependencies are not yet supported.
99

10-
Integration with the CocoaPods dependency manager is also supported with the same limitation – you cannot use pure Swift
11-
pods.
10+
To handle iOS dependencies in Kotlin Multiplatform projects, you can manage them with the [cinterop tool](#with-cinterop)
11+
or use the [CocoaPods dependency manager](#with-cocoapods) (pure Swift pods are not supported).
1212

13-
We recommend [using CocoaPods](#with-cocoapods) to handle iOS dependencies in Kotlin Multiplatform projects.
14-
[Manage dependencies manually](#without-cocoapods) only if you want to tune the interop process specifically or if you
15-
have some other strong reason to do so.
13+
### With cinterop
1614

17-
### With CocoaPods
18-
19-
1. Perform [initial CocoaPods integration setup](native-cocoapods.md#set-up-an-environment-to-work-with-cocoapods).
20-
2. Add a dependency on a Pod library from the CocoaPods repository that you want to use by including the `pod()`
21-
function call in `build.gradle(.kts)` of your project.
22-
23-
<tabs group="build-script">
24-
<tab title="Kotlin" group-key="kotlin">
25-
26-
```kotlin
27-
kotlin {
28-
cocoapods {
29-
version = "2.0"
30-
//..
31-
pod("SDWebImage") {
32-
version = "5.20.0"
33-
}
34-
}
35-
}
36-
```
37-
38-
</tab>
39-
<tab title="Groovy" group-key="groovy">
40-
41-
```groovy
42-
kotlin {
43-
cocoapods {
44-
version = '2.0'
45-
//..
46-
pod('SDWebImage') {
47-
version = '5.20.0'
48-
}
49-
}
50-
}
51-
```
52-
53-
</tab>
54-
</tabs>
55-
56-
You can add the following dependencies on a Pod library:
57-
* [From the CocoaPods repository](native-cocoapods-libraries.md#from-the-cocoapods-repository)
58-
* [On a locally stored library](native-cocoapods-libraries.md#on-a-locally-stored-library)
59-
* [From a custom Git repository](native-cocoapods-libraries.md#from-a-custom-git-repository)
60-
* [From a custom Podspec repository](native-cocoapods-libraries.md#from-a-custom-podspec-repository)
61-
* [With custom cinterop options](native-cocoapods-libraries.md#with-custom-cinterop-options)
62-
63-
3. Run **Reload All Gradle Projects** in IntelliJ IDEA (or **Sync Project with Gradle Files** in Android Studio)
64-
to re-import the project.
65-
66-
To use the dependency in your Kotlin code, import the package `cocoapods.<library-name>`. For the example above, it's:
67-
68-
```kotlin
69-
import cocoapods.SDWebImage.*
70-
```
71-
72-
### Without CocoaPods
73-
74-
If you don't want to use CocoaPods, you can use the cinterop tool to create Kotlin bindings for Objective-C or Swift
15+
You can use the cinterop tool to create Kotlin bindings for Objective-C or Swift
7516
declarations. This will allow you to call them from the Kotlin code.
7617

77-
The steps differ a bit for [libraries](#add-a-library-without-cocoapods)
78-
and [frameworks](#add-a-framework-without-cocoapods), but the idea remains the same.
18+
The steps differ a bit for [libraries](#add-a-library) and [frameworks](#add-a-framework),
19+
but the general workflow looks like this:
7920

8021
1. Download your dependency.
8122
2. Build it to get its binaries.
82-
3. Create a special `.def` file that describes this dependency to cinterop.
23+
3. Create a special `.def` [definition file](native-definition-file.md) that describes this dependency to cinterop.
8324
4. Adjust your build script to generate bindings during the build.
8425

85-
#### Add a library without CocoaPods
26+
#### Add a library
8627

8728
1. Download the library source code and place it somewhere where you can reference it from your project.
88-
8929
2. Build a library (library authors usually provide a guide on how to do this) and get a path to the binaries.
90-
9130
3. In your project, create a `.def` file, for example `DateTools.def`.
92-
9331
4. Add a first string to this file: `language = Objective-C`. If you want to use a pure C dependency, omit the language
9432
property.
95-
9633
5. Provide values for two mandatory properties:
34+
9735
* `headers` describes which headers will be processed by cinterop.
9836
* `package` sets the name of the package these declarations should be put into.
9937

10038
For example:
39+
10140
```none
10241
headers = DateTools.h
10342
package = DateTools
10443
```
10544
10645
6. Add information about interoperability with this library to the build script:
46+
10747
* Pass the path to the `.def` file. This path can be omitted if your `.def` file has the same name as cinterop and
10848
is placed in the `src/nativeInterop/cinterop/` directory.
10949
* Tell cinterop where to look for header files using the `includeDirs` option.
@@ -173,18 +113,15 @@ property in the `.def` file. For the example above, this will be:
173113
import DateTools.*
174114
```
175115

176-
#### Add a framework without CocoaPods
116+
#### Add a framework
177117

178118
1. Download the framework source code and place it somewhere that you can reference it from your project.
179-
180119
2. Build the framework (framework authors usually provide a guide on how to do this) and get a path to the binaries.
181-
182120
3. In your project, create a `.def` file, for example `MyFramework.def`.
183-
184121
4. Add the first string to this file: `language = Objective-C`. If you want to use a pure C dependency, omit the
185122
language property.
186-
187123
5. Provide values for these two mandatory properties:
124+
188125
* `modules` – the name of the framework that should be processed by the cinterop.
189126
* `package` – the name of the package these declarations should be put into.
190127

@@ -196,6 +133,7 @@ import DateTools.*
196133
```
197134
198135
6. Add information about interoperability with the framework to the build script:
136+
199137
* Pass the path to the .def file. This path can be omitted if your `.def` file has the same name as the cinterop and
200138
is placed in the `src/nativeInterop/cinterop/` directory.
201139
* Pass the framework name to the compiler and linker using the `-framework` option. Pass the path to the framework
@@ -266,10 +204,66 @@ import MyFramework.*
266204
Learn more about [Objective-C and Swift interop](native-objc-interop.md) and
267205
[configuring cinterop from Gradle](multiplatform-dsl-reference.md#cinterops).
268206

207+
### With CocoaPods
208+
209+
1. Perform [initial CocoaPods integration setup](native-cocoapods.md#set-up-an-environment-to-work-with-cocoapods).
210+
2. Add a dependency on a Pod library from the CocoaPods repository that you want to use by including the `pod()`
211+
function call in `build.gradle(.kts)` of your project.
212+
213+
<tabs group="build-script">
214+
<tab title="Kotlin" group-key="kotlin">
215+
216+
```kotlin
217+
kotlin {
218+
cocoapods {
219+
version = "2.0"
220+
//..
221+
pod("SDWebImage") {
222+
version = "5.20.0"
223+
}
224+
}
225+
}
226+
```
227+
228+
</tab>
229+
<tab title="Groovy" group-key="groovy">
230+
231+
```groovy
232+
kotlin {
233+
cocoapods {
234+
version = '2.0'
235+
//..
236+
pod('SDWebImage') {
237+
version = '5.20.0'
238+
}
239+
}
240+
}
241+
```
242+
243+
</tab>
244+
</tabs>
245+
246+
You can add the following dependencies on a Pod library:
247+
248+
* [From the CocoaPods repository](native-cocoapods-libraries.md#from-the-cocoapods-repository)
249+
* [On a locally stored library](native-cocoapods-libraries.md#on-a-locally-stored-library)
250+
* [From a custom Git repository](native-cocoapods-libraries.md#from-a-custom-git-repository)
251+
* [From a custom Podspec repository](native-cocoapods-libraries.md#from-a-custom-podspec-repository)
252+
* [With custom cinterop options](native-cocoapods-libraries.md#with-custom-cinterop-options)
253+
254+
3. Run **Reload All Gradle Projects** in IntelliJ IDEA (or **Sync Project with Gradle Files** in Android Studio)
255+
to re-import the project.
256+
257+
To use the dependency in your Kotlin code, import the package `cocoapods.<library-name>`. For the example above, it's:
258+
259+
```kotlin
260+
import cocoapods.SDWebImage.*
261+
```
262+
269263
## What's next?
270264

271265
Check out other resources on adding dependencies in multiplatform projects and learn more about:
272266

273-
* [Connecting platform-specific libraries](multiplatform-share-on-platforms.md#connect-platform-specific-libraries)
267+
* [Connecting platform libraries](native-platform-libs.md)
274268
* [Adding dependencies on multiplatform libraries or other multiplatform projects](multiplatform-add-dependencies.md)
275269
* [Adding Android dependencies](multiplatform-android-dependencies.md)

docs/topics/multiplatform/multiplatform-spm-local-integration.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The tutorial assumes that your project is using [direct integration](multiplatfo
4343
approach with the `embedAndSignAppleFrameworkForXcode` task in the project's build phase. If you're connecting a Kotlin
4444
framework through CocoaPods plugin or through Swift package with `binaryTarget`, migrate first.
4545

46-
### Migrate from SPM binaryTarget integration to local direct integration {initial-collapse-state="collapsed" collapsible="true"}
46+
### Migrate from SPM binaryTarget integration {initial-collapse-state="collapsed" collapsible="true"}
4747

4848
To migrate from the SPM integration with `binaryTarget`:
4949

@@ -52,7 +52,7 @@ To migrate from the SPM integration with `binaryTarget`:
5252
2. In every `Package.swift` file, remove both dependencies to the package with a Kotlin framework inside and target
5353
dependencies to the products.
5454

55-
### Migrate from CocoaPods plugin to direct integration {initial-collapse-state="collapsed" collapsible="true"}
55+
### Migrate from the CocoaPods plugin {initial-collapse-state="collapsed" collapsible="true"}
5656

5757
> If you have dependencies on other Pods in the `cocoapods {}` block, you have to resort to the CocoaPods integration approach.
5858
> Currently, it's impossible to both have dependencies on Pods and on the Kotlin framework in a multimodal SPM project.
@@ -81,7 +81,7 @@ To migrate from the CocoaPods plugin:
8181
To be able to use Kotlin code in a local Swift package, connect the Kotlin framework generated from the multiplatform
8282
project to your Xcode project:
8383

84-
1. In Xcode, go to **Product** | **Scheme** | **Edit scheme** or click the schemes icon in the top bar and select **Edit scheme**:
84+
1. In Xcode, go to **Product** | **Scheme** | **Edit scheme** or click the scheme icon in the top bar and select **Edit scheme**:
8585

8686
![Edit scheme](xcode-edit-schemes.png){width=700}
8787

@@ -103,7 +103,9 @@ project to your Xcode project:
103103

104104
![Filled run script action](xcode-filled-run-script-action.png){width=700}
105105

106-
5. You can now import the shared module into your local Swift package and use Kotlin code. For example, define the following function:
106+
5. You can now import the shared module into your local Swift package and use Kotlin code.
107+
108+
In Xcode, navigate to your local Swift package and define a function with a module import, for example:
107109

108110
```Swift
109111
import Shared

docs/topics/multiplatform/native-spm.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ To set up the publishing of an XCFramework:
112112
113113
`swift package compute-checksum Shared.xcframework.zip`
114114
115-
<anchor name="upload"/>
116-
117115
2. Upload the ZIP file to the file storage of your choice. The file should be accessible
118116
by a direct link. For example, here's how you can do it using releases in GitHub:
119117
@@ -295,8 +293,9 @@ For example, you have a `network` and a `database` module, which you combine in
295293
./gradlew :together:assembleTogetherReleaseXCFramework
296294
```
297295
298-
5. Follow steps 4–7 from [the previous section](#upload) for `together.xcframework`: archive, calculate the checksum,
299-
upload the archived XCFramework, create and push a `Package.swift` file.
296+
5. Follow the steps from the [previous section](#prepare-the-xcframework-and-the-swift-package-manifest) to prepare
297+
`together.xcframework`: archive it, calculate the checksum, upload the archived XCFramework to the file storage,
298+
create and push a `Package.swift` file.
300299
301300
Now, you can import the dependency into an Xcode project. After adding the `import together` directive,
302301
you should have classes from both the `network` and `database` modules available for import in Swift code.

0 commit comments

Comments
 (0)