Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Create Hello World App with KMM 📱- Android & IOS

 

 
Aman Bansal
Software Engineer @goibibo
Published: October 06, 2020
Tweet
Share
 

 

In mobile development, the Android and iOS versions of an application often have a lot in common . The business logic behind the apps is probably pretty much the same. Download files, read from and write to a database, get and send data to a remote host, parsing the remote data, etc. So why not we write the business logic only once and share it between different platforms?

With this idea, JetBrains came up with Kotlin Multiplatform Project.

 

➡️ What is Kotlin Multiplatform Mobile?

Kotlin Multiplatform Mobile (KMM) is an SDK for cross-platform mobile development provided by JetBrains. Leveraging the multiplatform capabilities of Kotlin, you can compile/transpile a single project for multiple platforms.

 

Image for post

With KMM, you get this flexibility and retain the benefits of native programming. Use a single codebase for the business logic of iOS and Android apps and write platform-specific code only where it’s necessary, to implement a native UI or when working with platform-specific APIs.

KMM can seamlessly be integrated with your mobile project. Shared code, written in Kotlin, is compiled to JVM bytecode with Kotlin/JVM and to native binaries with Kotlin/Native, so you can use your KMM business-logic modules just like any other regular mobile library.

While writing this blog, KMM is in Alpha and you can begin sharing business rationale in your versatile applications with it immediately.

KMM is still quite new to the mobile development world, JetBrains also developed KMM plugin for Android Studio to seamlessly setup your KMM project. Plugin permits you to compose, run, test, and debug shared code in the Android Studio.

 

➡️ Steps of build your HELLO WORLD KMM app

  1. Install Kotlin Multiplatform Mobile plugin in Android studio. Open your Android Studio -> Click Configure -> Select Plugins

 

Image for post

 

2. Select Marketplace in the plugin section and search for “KMM”, Install it and restart your Android studio

 

Image for post

 

3. In the Android studio first screen Select “ Start a new Android Studio project”.

 

Image for post

 

4. In the “ Select a project Template” screen, scroll down and select “KMM Application”

 

Image for post

 

5. Now set your project a name, minimum SDK, directory and package name.

 

Image for post

 

Now, you will have to wait while your project is set up for the first time. It may take some time to download and set up the required components.

 

➡️ Run Your Application

Select your platform you want to run from the toolbar, select the Emulator/Simulator and click Run

 

Image for post

 

 

Image for post

 

 

Image for post

 

 

To run IOS app you need to have Xcode and Simulater installed.

 

If you want to run your ios application on another simulated device, you can add a new run configuration.

  1. In the list of run configurations, click Edit Configurations.
 

Image for post

 

2. Click the + button above the list of configurations and select iOS Application.

 

Image for post

 

3. Name your configuration.

4. Select a simulated device in the Execution target list, and then click OK.

 

Image for post

 

5. Click Run to run your application on the new simulated device.

 

➡️ Dive Into The Code

 

Image for post

 

Android Devs? Looks Familiar? 😎
IOS Devs? Looks Alien? 👽

 

➡️ Modules

  • Shared module — a Kotlin module where your common business logic code resides for both Android and iOS applications. Builds into an Android library and an iOS framework. Uses Gradle as a build system.
  • androidApp module — a Kotlin module that builds into the Android application. Uses Gradle as a build system.
  • iosApp module — an Xcode project that builds into the iOS application.

 

Project: build.gradle.kts

 

 

➡️ Shared module

The shared module contains the code that is common for Android and iOS applications. However, to implement the same logic on Android and iOS, you sometimes need to write two platform-specific versions of it like Bluetooth, Wifi. To handle such cases, Kotlin offers the expect/actual mechanism. The source code of the shared module is organized in three source sets accordingly:

 

Image for post

 

  • commonMain stores the code that works on both platforms, including the expect declarations
  • androidMain stores Android-specific parts, including actualimplementations
  • iosMain stores iOS-specific parts, including actual implementations

Each source set has its own dependencies. Kotlin standard library is added automatically to all source sets, you don’t need to declare it in the build script.

 

build.gradle.kts

 

This build.gradle.kts has the configurations of Android as well as IOS for shared module

 

 

AndroidApp: build.gradle.kts

 

 


➡️ Using the Expect/Actual Keywords

It’s common for a multiplatform app to have platform-specific code. For example, you may want to know if your app is running on an Android or iOS device and get the exact model of your device. To achieve this you need to use the expect/actual keywords.

First, declare a class, method or function in the common module using the expect keyword and leave the body empty as you often do when creating interfaces or abstract classes. Then, write the platform-specific implementations for the class, method or function in all of the other modules using the actual keyword in the signature.

 

Image for post

Note: If you use expect in a package, you also need to use the corresponding actual command in a package with the same name.

 

Else you’ll this error

 

Image for post

 


➡️ Expect/Actual Usage

commonMain

 

expect class Platform() {
    val platform: String
}

 

androidMain

 

actual class Platform actual constructor() {
    actual val platform: String = "Android ${android.os.Build.VERSION.SDK_INT}"
}

 

iosMain

 

import platform.UIKit.UIDevice


actual class Platform {
    actual val platform: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion
}

 

MainActivity.kt (Android)

 

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val tv: TextView = findViewById(R.id.text_view)
        tv.text = "Hello World, ${Platform().platform}!"
    }
}

 

ContentView.swift (iOS)

 

struct ContentView: View {
    var body: some View {
        Text("Hello World, "+ Platform().platform)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

 

CONGRATULATIONS!! You have made your first KMM app.

 


➡️ Here are some KMM apps

 

JetBrains/kotlinconf-app

This is the official KotlinConf App! We hope you enjoy(ed) the conference and sessions. This repository contains the…

github.com

 

saket/press

Press is a wysiwyg writer for crafting notes inspired by Bear. It uses markdown for styling and formatting text with a…

github.com

 

jarroyoesp/KotlinMultiPlatform

This example shows how to create a simple Android/iOS/JVM/JS project sharing some Kotlin code. This app saves on a…

github.com

 

➡️ Check Out Available Libraries

 

AAkira/Kotlin-Multiplatform-Libraries

Kotlin Multiplatform Libraries. Welcome PR if you find or create new Kotlin Multiplatform Library. …

github.com

 


Feel free to connect with me on Twitter


Hope You’ve learned something new today..

 


 

 

Tags: Kmm, Android, Kotlin, iOS, Swift

 

View original article at: 


 

Originally published: September 21, 2020

Android News
Evolution of Android Update SystemEvolution of Android Update System
Evolution of Android Update SystemEvolution of Android Update System

By Ivan Kuten

So, how can you update Android on mobile devices? While developing software for Smart TVs and Android-based set-top boxes, we’ve narrowed it down to four ways, discarding some very exotic options:

By ProAndroidDev -
Android News
Happy Railway
Happy Railway

By Hadi Lashkari Ghouchani

This post is on the tail of Railway Oriented Programming in Kotlin by Antony Harfield. So you need to read it first and continue here. As it’s obvious I really liked it and tried it out. It needs every process have a result like

By ProAndroidDev -
Android News
Unit Tests and Concurrency
Unit Tests and Concurrency

By Stojan Anastasov

Once Retrofit added RxJava support, RxJava became my go-to concurrency framework for writing Android apps. One of the great things about RxJava is the excellent testing support. It includes TestObserver, TestScheduler, RxJavaPlugins so you can switch your schedulers in tests.

By ProAndroidDev -
Android News
When Compat libraries will not save you
When Compat libraries will not save you

By Danny Preussler

And why you should avoid using the “NewApi” suppression! The idea of “Compat” libraries was probably one of the key aspects of Android dominating the mobile space. Other than with iOS, Android users often could not update their operating system after a new version launch, simply as their phones won’t allow them to, the Android problem of fragmentation.

 

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One