Kotlin Native for Android and iOS Applications

Ahmed Omar
4 min readMar 11, 2020

--

Android and iOS

The most challenging is that it was long since I wrote an iOS application, and I wanted to add something different by using the latest UI native languages enhancements in the 2 frameworks (Jetpack Compose & SwiftUI), as these are the latest updates and not covered in any official codelabs and documentation, yet.

To make the audience interested with the top, specialty that most of them are expert in iOS Swift, I have to make them feel the pain from real life experience managing Mobile Android and iOS teams to create 2 native apps for the same project or client.

But as I mentioned they don’t utilize Jetpack Compose and SwiftUI.

I am going to make some changes on it and updates to look like that, https://github.com/joreilly/PeopleInSpace.

Note: We’ll be using IntelliJ IDEA 2019.2, Android Studio 3.4, Kotlin 1.3.50, Xcode 10.3, macOS 10.14, Gradle 5.5.1

. Creating an Android Project

We need Android Studio for the tutorial. We can download and install it from the https://developer.android.com/studio/. Let’s open the IDE and check that we see the newest Kotlin version, namely 1.3.50 or newer under the Kotlin section Languages & Frameworks | Kotlin in the Settings (or Preferences) dialog of Android Studio.

Our first step is to create a new Android project via the Start a new Android project item on the Android Studio home screen. We then proceed to select the Empty Activity option and click Next. It’s important to pick the Kotlin language in the wizard. Let’s use the com.jetbrains.handson.mpp.mobile package for the tutorial. Now we can press the Finish button and create our new Android project.

At this point, we should be able to compile and run the Android application. Let’s check that it works!

The step-002 branch of the github.com/kotlin-hands-on/mpp-ios-android repository contains a possible solution for the tasks that we have done above. We can also download the archive from GitHub directly or check out the repository and select the branch.

ANDROID

. Shared Kotlin Code for iOS

et’s run the packForXcode Gradle task of the SharedCode project. We can do it either from the Gradle tab in AndroidStudio or by running the ./gradlew :SharedCode:packForXcode command from console. This task is designed to help to simplify the setup of our iOS Framework in the Xcode project model.

We need several binaries from the framework to use it with Xcode:

  • iOS arm64 debug --- the binary to run the iOS device in debug mode
  • iOS arm64 release --- the binary to include into a release version of an app
  • iOS x64 debug --- the binary for iOS simulator, which uses the desktop mac CPU

The easiest way to configure Xcode to use a custom-built framework is to place the framework under the same folder for all configurations and targets. Then add a custom step to the Xcode project build to update or build the framework before the actual Xcode build is started. Xcode sets several environment variables for custom steps; we can use these variables in the Gradle script to select the requested target platform and the configuration. For more details, please refer to the packForXcode task sources in the SharedCode/build.gradle.kts file.

Let’s now switch back to Android Studio and execute the build target of the SharedCode project from the Gradle tool window. The task looks for environment variables set by the Xcode build and copies the right variant of the framework to the SharedCode/build/xcode-frameworks folder. We can then include the framework from that folder into the build

IOS

. Reference

1- Play.kotlinlang.org

2- hands-on

3- Kotlin Multiplatform Project for Android and iOS: Getting Started

4- Shady Yehia Selim, MSc,MBA

--

--

Ahmed Omar

Software Engineer || ( 📱 Android Engineer ) 📝 Writer….. 💻 Open Source Contributor .