John P.

John P.

5 minute read

How to Target iOS and Android With Kotlin for Multi-Platform

How to Target iOS and Android With Kotlin for Multi-Platform

Creating a platform-specific app is no doubt an excellent idea. It gives every developer the ease to build projects that are specific to the device. And when you are to develop an app for a single platform, going native is the best decision. But how about an app that is to be deployed both on Android and iOS?

Would you still like to invest your time and effort in developing apps separately? Would it not be resource-draining?

kotlin programming with android

Of course, it is. While you might be deploying an app on two different platforms and evidently, they have different specifications, most of the process followed to create an iOS and Android app is the same. Also, the business logic incorporated to build the app remains the same.

You start with downloading files for the project, create programs to read from the file, process information and then display it. All of these are the same and the difference is simply in the platform.

So, why create two different applications, repeating the same process time and again? Ok, you don’t have to do this. You can simply create an app that functions well on both platforms.

But how?

This is where we introduce the idea of Kotlin Multi-Platform Project or in simple terms, MPP.

To give a brief, Kotlin Multi-Platform Project is the platform that allows developers to create a single project and compile it on different platforms. In case, you are wondering how to do that, this article will help you.

Here, we uncover the steps you need to follow to target iOS and Android apps using the Kotlin MPP.

How to use Kotlin Multi-Platform Project To Build iOS And Android Applications?

 

  1. Create a Project

 

To start with mobile development, you need to first set up a Kotlin Multi-Platform Project. Here, a single shared library would be used to create the project.

  • Create individual projects, one from Android Studio and other from Xcode.
  • Once done, keep them understand the same directory.

What we do here is create an Android project, and then under the root directory of the Android app, create an iOS project.

  1. Module For Shared Library

 

├── android-app

├── ios-app

└── mpp-library

Follow the above diagram to create an MPP library within the app. Next, you would need to create a mpp-library/build.gradle that will be the storehouse of Multi-Platform configurations. Once done, you next need to include the above-created library within the settings.gradle.

You can now run Gradle sync to ensure that the modifications done have been successfully executed. In case, there exists an error, it probably because you haven’t configured the library to state the SDK version. Write code to specify the same and then make sure you have the Android Manifest package.

The Gradle Sync would now run successfully.

  1. Specify Mobile Targets

 

This is an important step as we are to target both the Android and the iOS platform through the Kotlin Multi-Platform Project. Hence, it is needed to set up mobile targets. To do so, you need to edit the mpp-library/build.gradle with the following piece of code:

 

kotlin {
    targets {
        android()
        iosArm64()
        iosX64()
    }
}

As evident, the targets are Android and iOS devices.

Note: you would need to create the below mentioned three directories.

  • mpp-library/src/commonMain/kotlin/
  • mpp-library/src/androidMain/kotlin/
  • mpp-library/src/iosMain/kotlin/
  1. Configure Android Target

 

The next step involves the configuration of the Android target. To do this:

Shift AndroidManifest.xml to androidMain:

mpp-library/src/main/AndroidManifest.xml → mpp-library/src/androidMain/AndroidManifest.xml

By doing this, you have successfully configured the target but now, the main would fail to recognize Android Manifest.

In order to fix the error, you would need to once again edit mpp-library/build.Gradle:

android {
  //…
  sourceSets {
    main {
      setRoot(‘src/androidMain’)
    }
    release {
      setRoot(‘src/androidMainRelease’)
    }
    debug {
      setRoot(‘src/androidMainDebug’)
    }
    test {
      setRoot(‘src/androidUnitTest’)
    }
    testRelease {
      setRoot(‘src/androidUnitTestRelease’)
    }
    testDebug {
      setRoot(‘src/androidUnitTestDebug’)
    }
  }
}
  1. Coding

Now, we try to write code within the platform. A new file named mpp-library/src/commonMain/kotlin/HelloWorld.Kt is, created and fed with the contents given below :

object HelloWorld {
  fun print() {
    println(“hello common world”)
  }
}

Now, the fact that we are yet to configure Kotlin, the above doesn’t work. So, what do we do next?

Link Kotlin Library as mpp-library/build.gradle:

kotlin {

// …

sourceSets {

commonMain {

dependencies {

implementation “org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version”

}

}

}

}

And now the IDE is familiar with the platform and we can move ahead with writing code.

After we have done with the above we have two important steps

  1. Android realization and,
  2. iOS Realization

Android Realization

To start, you would first need to link the shared library with android-app.

Modify the android-app/build.gradle to add the following:

dependencies {

// …

implementation project(“:mpp-library”)

}

Next, you need to call the print function and further add the following to android-app/src/main/java/com/icerockdev/android_app/MainActivity.kt:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

// …

HelloWorld.print()

}

}

Make sure that you have upgraded the SDK version. If not, you can do if by adding:

android {

// …

minSdkVersion 21

// …

}

to android-app/build.gradle.

iOS Realization

 

The next and the final step is iOS app realization. In case you remember that we have set the targets as iosArm64Main and iosX64Main, you will realize that there would be two separate source codes. You either need to create copies of the same or deploy a generic one.

One way to negate duplication is by using symlinks in iosmain.  You can create symlinks mpp-library/src/iosArm64Main and mpp-library/src/iosX64Main as:

cd mpp-library/src

ln -s iosMain iosArm64Main

ln -s iosMain iosX64Main

Once done, you can now create iOS-specific codes and compile the framework. Make sure you have made the following changes in the mpp-library/build.gradle before compiling the framework.

kotlin {

targets {

// …

def configure = {

binaries {

framework(“MultiPlatformLibrary”)

}

}

iosArm64(“iosArm64”, configure)

iosX64(“iosX64”, configure)

}

}

Also, launch the project using the Xcode platform.

All you need to do is add the Kotlin Multi-Platform framework within the Platform and further, link (./../mpp-library/build/bin/iosX64/MultiPlatformLibraryDebugFramework) to the search path.

Final Word

That’s all about using the Kotlin Multi-Platform Project to target the iOS and the Android platform. If there exists a problem, you can contact an app development company to help you with the same.

Author Profile-

Nikhil Bansal is the CEO and Founder of Apptunix, a leading iOS App Development Company Company helping businesses in streamlining their processes with powerful and intuitive mobile apps. With extensive experience in iOS app development, he has established himself as a highly-focused Solution Architect and UX expert, the one who is always ready to make efforts in the direction where technology blends with lives.