[Tutorial] Your First Kotlin Multiplatform iOS & Android Apps, the Easy Way!
https://blog.kotlin-academy.com/

[Tutorial] Your First Kotlin Multiplatform iOS & Android Apps, the Easy Way!

Hi Kotlin, iOS, and Android lovers.

I am going to take you through the steps of creating a new Kotlin Multiplatform (KMP) project and create Android and iOS apps.

In this tutorial I am going to take the easy way path, using IntelliJ IDEA, promising you to have another tutorial using Android Studio.

As we are targeting the iOS, we must have a Mac machine with Xcode installed on it, or having a shared folder with a Mac to complete the tutorial.

1) Install IntelliJ IDEA Community EdItion, as it is free and support Android.

No alt text provided for this image

2) Install the JDK 8 (Java Development Kit), I advice you to download it from filehorse.com

No alt text provided for this image

3) Download the Android SDK (Software Development Kit)

No alt text provided for this image

4) Now we are ready to create our first KMP project.

No alt text provided for this image

5) Select Kotlin -> Mobile Android/iOS | Gradle, then click Next

No alt text provided for this image

6) For the Gradle JVM select the 1.8 java, this is the version that works with Android, as Android supports JDK 1.7 & 1.8, and that was the reason for downloading the JDK, then click Next.

No alt text provided for this image

7) Then change the Project Name and Location, and click Finish. I used KMP as my project name.

No alt text provided for this image

8) Every time you create a KMP or Android project might start with Error, don't panic, this is normal, the IDE just don't know where the Android SDK.

No alt text provided for this image

9) You can easily enter the SDK location, rebuild the project, and all is Good.

No alt text provided for this image

10) Now we had the KMP project created for us with environment for both the iOS and Android.

11) We will find to Apps folder if we are in the Project preview, app folder (that contains all our KMP code for business and magic), and iosApp folder (which contains the Xcode project created for us), and that was the creating that tutorial with the IntelliJ IDEA.

No alt text provided for this image

12) Lets navigate in the app folder, we will find under app->src folder androidMain, iosMain, and commonMain. Those are the main folders were all the magic happens, and business written.

No alt text provided for this image

13) Under the commonMain, we will find the Sample.kt file. This files contains the common business APIs and classes that is going to be called and consumed from both iOS and Android. I the class, variable, or method needs data from the native platform we must use (expect), that is how we till the native platform code the necessity to override or modify that variable. Remember you classes should be public to be able to see from iOS and Android classes.

In our example we can see that we have a hello() function that returns String when called with message "Hello from ${Platform.name}", and this an expect variable that should return "iOS" from iOS simulator and "Android" from Android emulator.

No alt text provided for this image

14) Now let open the SampleIos.kt file under the iosMain folder, we will find the (expect) val and object overridden by (actual), and passing the "iOS" string.

No alt text provided for this image

15) Same we will find that for Android the string was overridden to "Android" in the SampleAndroid.kt under the main folder. I still advice to put Android logic under the androidMain folder instead.

No alt text provided for this image

16) In the previous image you will find that from Android class we called the hello() function from the commonMain class, and inserted its text in the Android activity layout.

No alt text provided for this image

17) Now to run the application on Android emulator first we have to create AVD (Android Virtual Device), or connect to mobile. Then create Android configuration.

No alt text provided for this image

18) Then change the Module to app, and change the name to "AndroidApp" or as you like.

No alt text provided for this image

19) Now Run, test, and enjoy your work.

No alt text provided for this image

20) Voilà!! PERFECT!!

No alt text provided for this image

21) Now to try in Xcode and iOS we must first run the project Gradle wrapper for iOS. Remember!! This step should be done regularly every time we do change in iosMain or commonMain.

No alt text provided for this image

22) Now let's open the project with Xcode.

No alt text provided for this image
No alt text provided for this image

23) We can see that the iOS ViewController calls, the Proxy().proxyHello() method from the KMP commonMain and change the view label.text

No alt text provided for this image

24) Let's run and see.

No alt text provided for this image

25) Just AMAZING!! And we are done!

You can find the tutorial video with extra info and implementation in this video:

And the session for why to use Kotlin Multiplatform, and what is the difference with other market technologies in this video:

And previous session slides here:


Kareem Zock ???

Project Development Lead | Agile, Project Management

4 年

Hello, I came across your article, and it's very useful, However, I have few questions as I m trying to implement KMM into my existing Kotlin app. 1- Do we need to have Xcode installed? If yes, it is possible on windows? 2- I m trying to enable the IOS emulator after installing the KMM plugin but its not working, its like it doesn't exist

回复
Mohamed E.

Software Engineer (Golang, Kotlin, Ktor, Docker, K8s, GCP, Flutter, iOS, Android)

4 年
回复
Israel Ulelu

Senior Software Engineer | Technical Writer | Open Source Contributor

4 年

Ezichi Amarachi?What am I seeing??

回复
Farida Said

Technical Program Manager @ Thirdwayv Inc. | PMP Certification (in Progress) | Agile Nanodegree Certified

4 年

Karim Mousa? ????

回复
gamal sebaee

Senior Mobile Developer @ Magints | Android, Flutter, Mobile Development

4 年

要查看或添加评论,请登录

社区洞察

其他会员也浏览了