Setup Kotlin Native Project with CocoaPods

Yuya Horita
4 min readSep 9, 2019

--

Introduction:

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. The dependencies of your projects are specified in simple Podfile . The following is an example for iOS Xcode project.

// in Podfile
pod 'RxSwift', '5.0.1'

Frameworks build by Kotlin Native are no exception. An experimental integration with CocoaPods started with 1.3.30.

This article shows an example of using CocoaPods with Kotlin Native, with version 1.3.50.

Configure:

1. Create sharedNative/src/commonMain/kotlin/common.kt

common.kt is very simple. If you have trouble, see this.

2. Setup Project Gradle

build.gradle

If project’s kotlin version is not set to 1.3.50 , change it. And, add the following line to setting.gradle.

include ':sharedNative'

3. Setup Module Gradle

Add a new sharedNative/build.gradle .

sharedNative/build.gradle

Both of presets.iosX64 / iosArm64 are defined here. This enables cocoapods-plugin to switch the build target(simulator/device) in build phase. iosArm32 is not included this time.

kotlin.cocoapods { .. } block is a key of this article. summary and homepage are defined in this block. These become spec’s information. How about version? It is not inside block. In fact, the project’s version is used for podspec’s version.

Refer this cocoapods-gradle-plugin implementation. Also, would be helpful for searching any other parameters like, authors, license or etc.

Unfortunately, spec name and module_name property are not accessible now. Same as version, project’s name property is used for spec’s name and it is used for module_name. (2019/9/9)

This is the podspec task implementation will be executed later. specName is defined by project’s name and is private & immutable. 😱

With above gradle setting, run ./gradlew :sharedNative:podspec , then sharedNative/sharedNative.podspec will be generated.

sharedNative/sharedNative.podspec

The file contains the following information.

  • basic information: name, version, homepage, summary and etc.
  • pod_target_xcconfig: Define and set KOTLIN_TARGET variable determines environment. Simulator, Device or Mac to .xcconfig file.
  • script_phases: Added to Xcode Build Phase, run the script, builds Kotlin Native framework. Some parameters are passed automatically, like Xcode environment CONFIGURATION (debug or release), user-defined KOTLIN_TARGET (defined above) and etc.

4. Setup Xcode project.

Open Xcode (here, version 10.2.1), create a new Xcode project in ios directory.

Run the following command to generate Podfile in ios directory.

pod init

If pod command is not available, see this official page.

The directory tree would be like the following,

Open Podfile,

ios/Podfile

add pod line, sharedNative is corresponds to module name, and path to the module, i.e. ../sharedNative .

Then, run pod install . If succeed, Sample.xcworkspace will be generated. Open this and in ViewController.swift ,

ViewController.swift

Hello World!!

Without any configuration of xcode, could execute Kotlin code. The magic is inside Pods project.

Sample.xcworkspace

Pods project has sharedNative Target. Open Build Phases of it.

sharedNative Build Phases

This has a script of building sharedNative module generated by above podspec.

sharedNative.xcconfig

Also, sharedNative.xcconfig is inside Pods project. In this file, KOTLIN_TARGET environment variable for switching target among device, simulator and mac is automatically defined.

These make kotlin native project configuring much easier.

Reference:

--

--

Yuya Horita

Master of Nuclear Physics, CyberAgent, Inc. FRESH LIVE. M3. Software Engineer. Twitter: https://twitter.com/horita_yuya ,GitHub: https://github.com/horita-yuya