In this article, we will learn how to write code that can be shared by IOS and Android platforms.

Let’s start by creating a module named SharedCode that consists of all the common code. We can do that by adding a line include :SharedCode to settings.gradle file and syncing the Gradle. Make sure that the SharedCode directory is created and you can see it in the project explorer side. 

Project Structure

The SharedCode should follow the project structure where the common code is written in a common directory and platform-specific code is placed in respective directories. Hence, a second step would be to create the folders which look like the structure given below.

All the common code between multiple platforms should be written inside src/commonMain/kotlin. The code specific to android should be present inside src/androidMain/kotlin whereas the code for IOS should be inside src/iosMain/kotlin.

Components of Multiplatform Application

Before we jump into coding, let’s understand some of the components and protocols that should be specified in the Shared module to make it usable by cross-platforms. The following diagram represents the different elements involved in the library.

Target: Target is responsible for the building, compiling, testing, and packaging a complete piece of software for our platforms. Hence, multiplatform applications consist of multiple targets – in this case, it’s Android and IOS.

Compilation: Each target should have compilation information that defines how the source codes and tests are compiled.

Source sets: Kotlin codes are arranged into source sets. All the common code is present in the commonMain sourceset whereas the platform-specific codes are present in [platFormName]Main source set.

To provide information about all of these components of the SharedCode module, we need to create a file named build.gradle.kts inside the shared module. The following table summarizes the information that should be specified in this file.

Code TypeDescriptionSource code folderTargetArtifacts
commonCommon code used by android and iossrc/commonMain/kotlinKotlin metadata
androidAndroid app specific codesrc/androidMain/kotlinjvm.jar file/ .class file
iosios app specific codesrc/iosMain/kotliniosArm64 / iosX64Apple framework

The actual code of build.gradle.kts looks like the one shown below.

As you can see, we have set the targets and source sets in the Kotlin multiplatform extension block and packForXcode is the task specified for the integration with the Xcode. You can simply copy-paste the whole configuration set up into the build.gradle.kts and make sure the Gradle is synchronized. Click on the link to get the full material.

Alright, we are now ready to write a code in our SharedCode library. For simplicity, let’s just display “Application running on: Android” and “Application running on: ios 13” in our Android and IOS device to get the hang of a shared framework. Later on, we can modify it to perform the operation with the third party library.

First of all, let’s create a common.kt file inside src/commonMain/kotlin directory where we write the common operation that is performed by both the apps.

In line 2, you can see the function is declared with expect keyword. This is to tell the compiler that the code is actually implemented in the platform-specific directory but can be used in the common section. Both the targets must provide their definition for platformName(). 

Next, create a file actual.kt file inside src/androidMain/kotlin directory where we provide the implementation for the platformName. 

Similarly, we need to create a file actual.kt file inside src/iosMain/kotlin directory for the ios specific code:   

Now our shared library is ready to be used in our android and IOS application. To summarize, we first need to specify our targets, their dependencies, their compilation strategies, etc in build.gradle.kts file. Then we can write a code using expect and actual keyword which enables us to use the platform-specific code in the common section that makes the entire framework develop an easy task to do. In the next articles, we will discuss how we can use this framework in different targets.

Tags

One response

Leave a Reply

Your email address will not be published. Required fields are marked *