Create an iOS KMM Library CocoaPods Artifact

March 17, 2023
 
Build your KMM library into CocoaPods for use with a Local Native iOS Application

In this article we will be building a Kotlin Multiplatform Mobile (KMM) library that will provide the functionality of a temperature converter and an HTTP Client that could be shared with iOS in the form of CocoaPods.

In previous articles, I have provided simple logic functionality in Kotlin, for both iOS and Android, which is fairly arbitrary to the discussion of deployment and resolution of a private CocoaPod artifact, but are good examples of a simple logic and an http client that demonstrates the inclusion of dependencies within your KMM shared library:

In this article we will reuse that functionality, but will focus on the iOS code to build a private CocoaPods

You can download the code for the library:

git clone https://CodeGorilla@bitbucket.org/CodeGorilla/cg-lib-kmm-artifactory.git
As of this article I am using the following:
  • Android Studio Electric Eel
  • Java 19
  • Gradle 7.6

Create Kotlin Multiplatform Library

Open Android Development Studio and create a new project. Select the Kotlin Multiplatform Library option.


Enter the name of your library and select your save location and Android SDK.


Enter the name of your shared module and select iOS framework distribution: CocoaPods dependency manager.


Troubleshooting

At this point you have a bare bones KMM library. If you have any errors in your build, please refer to my previous article KMM: Building a Simple Application for Android and iOS which fixes some gradle or JDK issues you may encounter.

Adding KTor Dependency

Once you have confirmed that the default template code runs successfully, we can now begin to update our dependencies to include KTor. Update the build.gradle.kts file to add artifacts of KTor in several places within the sourceSets section.

First we will add a variable to hold the version of KTor we which to use. This will ensure all of the components are of the same version and makes it easier for future updates. We will then add the ktor-client-core dependency to commonMain.

sourceSets {
val ktor_version = "2.2.4"
val commonMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:$ktor_version")
}
}
}

The next dependency will be the engine that will be used for Android (ktor-client-okhttp) only. Update the existing androidMain to add the dependency.

val androidMain by getting {
dependencies {
implementation("io.ktor:ktor-client-okhttp:$ktor_version")
}
}

And finally, add the engine that will be used for iOS (ktor-client-ios) only.

val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
dependencies {
implementation("io.ktor:ktor-client-ios:$ktor_version")
}
}
After you have saved the build.gradle.kts, make sure to do a Gradle sync to get your updates.

Adding Functionality

Once you have confirmed that the default template code runs successfully, we can now begin to update our library to contain our custom logic.

For this example, we are going to add 2 new classes:

  • TemperatureConverter class which will contain 2 functions that will simply convert Celsius to Fahrenheit and vice versa.
  • GorillaHTTPClient class which is a simple HTTP GET RESTful web service which will respond with the user agent information of the client calling the service, with some additional versioning information.

Temperature Converter

Right click on commonMain/kotlin/com.code_gorilla.kmmlibrary and select New Kotlin Class/File. Add a new Class named TemperatureConverter.

Enter the following code into your new TemperatureConverter.kt file:

package com.code_gorilla.kmmlibrary

class TemperatureConverter {
fun celsiusToFahrenheit(celsius: Double): Double {
return celsius * 1.8 + 32.0
}

fun fahrenheitToCelsius(fahrenheit: Double): Double {
return (fahrenheit - 32.0) / 1.8
}
}

KTor HTTP Client

The simple GET api we will be using in this example:https://www.code-gorilla.com/gorillaClientInfo should look similar to the following:


We are going to add a new GorillaHTTPClient class which will utilize the KTor as an HTTP Client. The GorillaHTTPClient class will simply call a RESTful web service and return the results as a string.

Right click on commonMain/kotlin/com.code_gorilla.kmmhttpclient and select New Kotlin Class/File. Add a new Class named GorillaHTTPClient.

Enter the following code into your new GorillaHTTPClient.kt file:

package com.code_gorilla.kmmhttpclient

import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.statement.*

class GorillaHTTPClient {
private val httpClient = HttpClient()

@Throws(Exception::class)
suspend fun getClientInfo(): String {
var retVal = "FAILURE"
val response: HttpResponse = httpClient.get("https://www.code-gorilla.com/gorillaClientInfo")
if (response.status.value in 200..299) {
retVal = response.bodyAsText()
}
return retVal
}
}

Here are some notable comments about the code:

  • The function is a suspend function to support the asynchronous HTTP request
  • @Throws(Exception::class)

Build the iOS Library CocoaPods

At this point, we have written some basic Kotlin functions within a self-contained library. We are now ready to build the library for use within other projects. In this case we will be building the library for both Android and iOS.

Clean Up

Let's start by cleaning our build folders to ensure we are starting with a clean slate by running the gradle clean. Open your terminal/command prompt, navigate to the root folder of your project and execute the following command:

gradle clean

Build

In your terminal/command prompt window, execute the following gradle command:

gradle build

When you run the build, you will be creating the Android Archive (AAR) files in the following location: /source/CodeGorillaArtifact/build/outputs/aar. The AAR files can now be used as a dependency within other Android applications.

iOS CocoaPods

Our objective in this article is to create CocoaPods. So we have one additional step to accomplish this goal.

In your terminal/command prompt window, execute the following gradle command:

gradle podPublishXCFramework

Upon successful completion, you should now see your CocoaPods files in the following location: /source/CodeGorillaArtifact/build/cocoapods/publish



SUCCESS! We now have our library prepared for deployment!

References



 
Return to articles