iOS 종속성 추가
Kotlin 멀티플랫폼 프로젝트에서는 Apple SDK 종속성(예: Foundation 또는 Core Bluetooth)을 사전 빌드된 라이브러리 세트로 사용할 수 있습니다. 이러한 종속성은 추가 구성이 필요하지 않습니다.
또한 iOS 소스 세트에서 iOS 생태계의 다른 라이브러리 및 프레임워크를 재사용할 수 있습니다. Kotlin은 Objective-C 종속성 및 Swift 종속성과 상호 운용성을 지원하며, 이때 Swift 종속성의 API는 @objc
속성을 사용하여 Objective-C로 내보내져야 합니다. 순수 Swift 종속성은 아직 지원되지 않습니다.
Kotlin 멀티플랫폼 프로젝트에서 iOS 종속성을 처리하려면, cinterop 도구로 관리하거나 CocoaPods 종속성 관리자를 사용할 수 있습니다(순수 Swift Pod는 지원되지 않습니다).
cinterop 사용
cinterop 도구를 사용하여 Objective-C 또는 Swift 선언에 대한 Kotlin 바인딩을 생성할 수 있습니다. 이를 통해 Kotlin 코드에서 해당 선언을 호출할 수 있습니다.
단계는 라이브러리와 프레임워크에 따라 약간 다르지만, 일반적인 작업 흐름은 다음과 같습니다:
- 종속성을 다운로드합니다.
- 바이너리를 얻기 위해 빌드합니다.
- cinterop에 이 종속성을 설명하는 특수
.def
정의 파일을 생성합니다. - 빌드 중에 바인딩을 생성하도록 빌드 스크립트를 조정합니다.
라이브러리 추가
라이브러리 소스 코드를 다운로드하여 프로젝트에서 참조할 수 있는 위치에 배치합니다.
라이브러리를 빌드하고(라이브러리 작성자가 일반적으로 방법을 안내합니다) 바이너리 경로를 얻습니다.
프로젝트에 예를 들어
DateTools.def
와 같은.def
파일을 생성합니다.이 파일에 첫 번째 문자열
language = Objective-C
를 추가합니다. 순수 C 종속성을 사용하려면 language 속성을 생략합니다.두 가지 필수 속성에 값을 제공합니다:
headers
: cinterop에 의해 처리될 헤더를 설명합니다.package
: 이 선언이 포함될 패키지의 이름을 설정합니다.
예시:
```none
headers = DateTools.h
package = DateTools
```
이 라이브러리와의 상호 운용성에 대한 정보를 빌드 스크립트에 추가합니다:
.def
파일의 경로를 전달합니다..def
파일이 cinterop과 이름이 같고src/nativeInterop/cinterop/
디렉터리에 배치된 경우 이 경로는 생략할 수 있습니다.includeDirs
옵션을 사용하여 cinterop에게 헤더 파일을 찾을 위치를 알려줍니다.- 라이브러리 바이너리 링크를 구성합니다.
kotlinkotlin { 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") } } }
groovykotlin { 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.*
cinterop 도구와 libcurl 라이브러리를 사용하는 샘플 프로젝트를 참조하세요.
프레임워크 추가
프레임워크 소스 코드를 다운로드하여 프로젝트에서 참조할 수 있는 위치에 배치합니다.
프레임워크를 빌드하고(프레임워크 작성자가 일반적으로 방법을 안내합니다) 바이너리 경로를 얻습니다.
프로젝트에 예를 들어
MyFramework.def
와 같은.def
파일을 생성합니다.이 파일에 첫 번째 문자열
language = Objective-C
를 추가합니다. 순수 C 종속성을 사용하려면 language 속성을 생략합니다.두 가지 필수 속성에 값을 제공합니다:
modules
: cinterop에 의해 처리되어야 하는 프레임워크의 이름입니다.package
: 이 선언이 포함될 패키지의 이름입니다.
예시:
nonemodules = MyFramework package = MyFramework
프레임워크와의 상호 운용성에 대한 정보를 빌드 스크립트에 추가합니다:
- .def 파일의 경로를 전달합니다.
.def
파일이 cinterop과 이름이 같고src/nativeInterop/cinterop/
디렉터리에 배치된 경우 이 경로는 생략할 수 있습니다. -framework
옵션을 사용하여 컴파일러와 링커에 프레임워크 이름을 전달합니다.-F
옵션을 사용하여 프레임워크 소스 및 바이너리 경로를 컴파일러와 링커에 전달합니다.
kotlinkotlin { 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/") } } }
groovykotlin { 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 파일의 경로를 전달합니다.
프로젝트를 빌드합니다.
이제 Kotlin 코드에서 이 종속성을 사용할 수 있습니다. 이를 위해 .def
파일의 package 속성에 설정한 패키지를 임포트합니다. 위 예시의 경우 다음과 같습니다:
import MyFramework.*
Swift/Objective-C 상호 운용성 및 Gradle에서 cinterop 구성에 대해 자세히 알아보세요.
CocoaPods 사용
초기 CocoaPods 통합 설정을 수행합니다.
프로젝트의
build.gradle(.kts)
에pod()
함수 호출을 포함하여 사용하려는 CocoaPods 저장소의 Pod 라이브러리에 종속성을 추가합니다.kotlinkotlin { cocoapods { version = "2.0" //.. pod("SDWebImage") { version = "5.20.0" } } }
groovykotlin { cocoapods { version = '2.0' //.. pod('SDWebImage') { version = '5.20.0' } } }
Pod 라이브러리에 다음과 같은 종속성을 추가할 수 있습니다:
- IntelliJ IDEA에서 Build | Reload All Gradle Projects를 실행하거나 (Android Studio에서는 File | Sync Project with Gradle Files) 프로젝트를 다시 임포트합니다.
Kotlin 코드에서 종속성을 사용하려면 cocoapods.<library-name>
패키지를 임포트합니다. 위 예시의 경우 다음과 같습니다:
import cocoapods.SDWebImage.*
- Kotlin 프로젝트에 다양한 Pod 종속성이 설정된 샘플 프로젝트를 참조하세요.
- 여러 대상을 가진 Xcode 프로젝트가 Kotlin 라이브러리에 종속되는 샘플 프로젝트를 확인하세요.
다음 단계는?
멀티플랫폼 프로젝트에서 종속성 추가에 대한 다른 리소스를 확인하고 다음 사항에 대해 자세히 알아보세요: