iOSの依存関係の追加
Apple SDKの依存関係(FoundationやCore Bluetoothなど)は、Kotlin Multiplatformプロジェクトで事前構築済みのライブラリセットとして利用できます。追加の設定は必要ありません。
iOSエコシステムの他のライブラリやフレームワークをiOSソースセットで再利用することもできます。Kotlinは、Objective-Cの依存関係、およびSwiftの依存関係(それらのAPIが@objc
属性でObjective-Cにエクスポートされている場合)との相互運用をサポートしています。Pure Swiftの依存関係はまだサポートされていません。
Kotlin MultiplatformプロジェクトでiOSの依存関係を処理するには、cinteropツールを使用するか、CocoaPods dependency managerを使用します(pure Swift podsはサポートされていません)。
With cinterop
cinteropツールを使用すると、Objective-CまたはSwiftの宣言に対するKotlinバインディングを作成できます。これにより、Kotlinコードからそれらを呼び出すことができます。
手順はライブラリとフレームワークで少し異なりますが、一般的なワークフローは次のようになります。
- 依存関係をダウンロードします。
- それをビルドして、そのバイナリを取得します。
- cinteropにこの依存関係を記述する特別な
.def
定義ファイルを作成します。 - ビルド中にバインディングを生成するようにビルドスクリプトを調整します。
Add a library
-
ライブラリのソースコードをダウンロードし、プロジェクトから参照できる場所に配置します。
-
ライブラリをビルドし(ライブラリの作成者は通常、これを行う方法のガイドを提供しています)、バイナリへのパスを取得します。
-
プロジェクトで、
.def
ファイルを作成します。たとえば、DateTools.def
です。 -
このファイルに最初の文字列を追加します:
language = Objective-C
。Pure Cの依存関係を使用する場合は、languageプロパティを省略します。 -
2つの必須プロパティの値を指定します。
headers
は、cinteropによって処理されるヘッダーを記述します。package
は、これらの宣言を配置するパッケージの名前を設定します。
例えば:
headers = DateTools.h
package = DateTools -
このライブラリとの相互運用性に関する情報をビルドスクリプトに追加します。
.def
ファイルへのパスを渡します。.def
ファイルの名前がcinteropと同じで、src/nativeInterop/cinterop/
ディレクトリに配置されている場合、このパスは省略できます。includeDirs
オプションを使用して、cinteropにヘッダーファイルを検索する場所を指示します。- ライブラリバイナリへのリンクを構成します。
- Kotlin
- Groovy
kotlin {
iosArm64() {
compilations.getByName("main") {
val DateTools by cinterops.creating {
// Path to the .def file
definitionFile.set(project.file("src/nativeInterop/cinterop/DateTools.def"))
// Directories for header search (an analogue of the -I<path> compiler option)
includeDirs("include/this/directory", "path/to/another/directory")
}
val anotherInterop by cinterops.creating { /* ... */ }
}
binaries.all {
// Linker options required to link to the library.
linkerOpts("-L/path/to/library/binaries", "-lbinaryname")
}
}
}kotlin {
iosArm64 {
compilations.main {
cinterops {
DateTools {
// Path to the .def file
definitionFile = project.file("src/nativeInterop/cinterop/DateTools.def")
// Directories for header search (an analogue of the -I<path> compiler option)
includeDirs("include/this/directory", "path/to/another/directory")
}
anotherInterop { /* ... */ }
}
}
binaries.all {
// Linker options required to link to the library.
linkerOpts "-L/path/to/library/binaries", "-lbinaryname"
}
}
} -
プロジェクトをビルドします。
これで、Kotlinコードでこの依存関係を使用できます。これを行うには、.def
ファイルのpackage
プロパティで設定したパッケージをインポートします。上記の例では、これは次のようになります。
import DateTools.*
Add a framework
-
フレームワークのソースコードをダウンロードし、プロジェクトから参照できる場所に配置します。
-
フレームワークをビルドし(フレームワークの作成者は通常、これを行う方法のガイドを提供しています)、バイナリへのパスを取得します。
-
プロジェクトで、
.def
ファイルを作成します。たとえば、MyFramework.def
です。 -
このファイルに最初の文字列を追加します:
language = Objective-C
。Pure Cの依存関係を使用する場合は、languageプロパティを省略します。 -
次の2つの必須プロパティの値を指定します。
modules
– cinteropによって処理されるフレームワークの名前。package
– これらの宣言を配置するパッケージの名前。
例えば:
modules = MyFramework
package = MyFramework -
フレームワークとの相互運用性に関する情報をビルドスクリプトに追加します。
- .defファイルへのパスを渡します。このパスは、.defファイルの名前がcinteropと同じで、
src/nativeInterop/cinterop/
ディレクトリに配置されている場合は省略できます。 -framework
オプションを使用して、フレームワーク名をコンパイラーとリンカーに渡します。-F
オプションを使用して、フレームワークのソースとバイナリへのパスをコンパイラーとリンカーに渡します。
- Kotlin
- Groovy
kotlin {
iosArm64() {
compilations.getByName("main") {
val DateTools by cinterops.creating {
// Path to the .def file
definitionFile.set(project.file("src/nativeInterop/cinterop/DateTools.def"))
compilerOpts("-framework", "MyFramework", "-F/path/to/framework/")
}
val anotherInterop by cinterops.creating { /* ... */ }
}
binaries.all {
// Tell the linker where the framework is located.
linkerOpts("-framework", "MyFramework", "-F/path/to/framework/")
}
}
}kotlin {
iosArm64 {
compilations.main {
cinterops {
DateTools {
// Path to the .def file
definitionFile = project.file("src/nativeInterop/cinterop/MyFramework.def")
compilerOpts("-framework", "MyFramework", "-F/path/to/framework/")
}
anotherInterop { /* ... */ }
}
}
binaries.all {
// Tell the linker where the framework is located.
linkerOpts("-framework", "MyFramework", "-F/path/to/framework/")
}
}
} - .defファイルへのパスを渡します。このパスは、.defファイルの名前がcinteropと同じで、
-
プロジェクトをビルドします。
これで、Kotlinコードでこの依存関係を使用できます。これを行うには、packageプロパティの.defファイルで設定したパッケージをインポートします。上記の例では、これは次のようになります。
import MyFramework.*
Objective-C and Swift interopおよびGradleからのcinteropの設定の詳細をご覧ください。
With CocoaPods
-
初期CocoaPods統合のセットアップを実行します。
-
プロジェクトの
build.gradle(.kts)
にpod()
関数呼び出しを含めることにより、使用するCocoaPodsリポジトリからPodライブラリへの依存関係を追加します。- Kotlin
- Groovy
kotlin {
cocoapods {
version = "2.0"
//..
pod("SDWebImage") {
version = "5.20.0"
}
}
}kotlin {
cocoapods {
version = '2.0'
//..
pod('SDWebImage') {
version = '5.20.0'
}
}
}Podライブラリに次の依存関係を追加できます。
-
IntelliJ IDEAでReload All Gradle Projects(またはAndroid StudioでSync Project with Gradle Files)を実行して、プロジェクトを再インポートします。
Kotlinコードで依存関係を使用するには、パッケージcocoapods.<library-name>
をインポートします。上記の例では、次のようになります。
import cocoapods.SDWebImage.*
What's next?
マルチプラットフォームプロジェクトでの依存関係の追加に関するその他のリソースを確認し、詳細をご覧ください。