Skip to main content

Diving into Kotlin Multiplatform: A Developer's First Look

 

Hey fellow coders!

Let's talk about a problem we've all faced. You're building an app for both Android and iOS. You write your networking layer, your data models, and all your business logic in Kotlin for Android. Then, you switch over to Xcode and... write the exact same networking layer, the exact same data models, and the exact same business logic in Swift.

It's tedious. It's a maintenance nightmare. And it feels... wrong.

What if you could write all that core logic just once?

Enter Kotlin Multiplatform (KMP).

So, what exactly IS KMP?

In the simplest terms, KMP is a technology from JetBrains that lets you share code between different platforms—like Android, iOS, Web, and Desktop.

But here's the most important part to understand right away: KMP is for sharing logic, not the UI.

Think of it like this: your app has a "brain" (the business logic, data handling, and network calls) and a "face" (the user interface). KMP lets you build a single, shared "brain" that can be plugged into different native "faces."

  • For Android, you'll use your shared brain with a native Android UI (built with Views or Jetpack Compose).
  • For iOS, you'll use that same shared brain with a native iOS UI (built with UIKit or SwiftUI).

This means your app gets to have a truly native look and feel on each platform, which users love, without you having to duplicate all the hard work that happens behind the scenes.

Why Bother? The Awesome Benefits.

Okay, so you can share code. But what does that actually mean for you as a developer?

  1. Write Once, Use Everywhere: This is the big one. Your networking, database access, data validation, and algorithms are written once in Kotlin and work everywhere. Less code means fewer bugs.
  2. Consistency is King: Ever had a bug on Android but not iOS because the logic was implemented slightly differently? With KMP, that's gone. The business logic is identical across all platforms, so you get consistent behavior.
  3. Lean on Your Kotlin Skills: If you're already an Android developer, you don't need to learn a whole new language like Swift or Dart. You can keep working in the language you already know and love.
  4. Faster Development: When you need to update a feature or fix a bug in your core logic, you do it in one place. The fix is automatically available for all platforms. It's a huge time-saver.

A Peek Under the Hood: How It Works

KMP organizes your code into "source sets." You have a commonMain folder for your shared code, and then platform-specific folders like androidMain and iosMain.

But how does the shared code know how to do something platform-specific, like getting the device name? That's where the magic of expect and actual comes in.

In your commonMain, you expect a function to exist:

kotlin
// In commonMain/kotlin/Platform.kt
expect fun getPlatformName(): String

This is like making a promise. You're telling the compiler, "Trust me, a function called getPlatformName() will be available on each platform."

Then, in each platform's specific code, you provide the actual implementation:

kotlin
// In androidMain/kotlin/Platform.kt
actual fun getPlatformName(): String {
return "Android"
}

// In iosMain/kotlin/Platform.kt
actual fun getPlatformName(): String {
return "iOS"
}

The compiler ties these together, and from your shared code, you can just call getPlatformName() and it will work!

What Can I Share? (The Good Stuff)

You can share a surprising amount! Here are some common candidates:

  • Data models and classes (e.g., User, Product)
  • Business logic (e.g., a function to calculate a shopping cart total)
  • Network calls and API definitions (using a library like Ktor)
  • Database access (using a library like SQLDelight)
  • Utility functions and constants

Ready to Dive In?

Kotlin Multiplatform isn't a silver bullet for every project, but it's an incredibly powerful tool for reducing code duplication and improving consistency. It lets you build robust apps faster by focusing your effort where it matters most.

If you're comfortable with Kotlin, you're already most of the way there. The official documentation has improved by leaps and bounds, and tools like the KMP Wizard can get you a starter project up and running in minutes.

So next time you're about to duplicate that networking layer, give KMP a look. You might be surprised just how much you can share.

Happy coding!

Comments

Popular posts from this blog

ProgressDialog example using handler android

final ProgressDialog dialog = ProgressDialog . show ( this , "Title" , "Message" , true ); final Handler handler = new Handler () { public void handleMessage ( Message msg ) { dialog . dismiss (); } } ; Thread t = new Thread () { public void run () { // write process code here. handler . sendEmptyMessage ( 0 ); } } ; t . start ();

How to set image button click effect ?

I have one simple solution: using that, you can set the default press effect to the button. // Create button and add event Button addImageView = (Button) bottomView.findViewById(R.id. addImage ); addImageView.setOnClickListener( new OnClickListener () { @Override public void onClick(View v) { // click event } }); //Using this code set touch listener addImageView.setOnTouchListener( touchEffect ); //touch listner OnTouchListener touchEffect = new OnTouchListener() {         public boolean onTouch(View v, MotionEvent event) {             switch (event.getAction()) {                 case MotionEvent. ACTION_DOWN : {                     v.getBackground().setColorFilter(0xe0f47521,PorterDuff.Mode. SRC_ATOP );                     v....

Store array of String or int or list in SharedPreferences android

 SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences( this );         JSONArray arr = new JSONArray ();         arr.put( "Viren" );         arr.put( "Android" );         prefs.edit().putString( "key" , arr.toString());         prefs.edit().commit();         try {             arr = new JSONArray (prefs.getString( "key" , "{}" ));             System. out .println(arr.getString(1));          } catch ( JSONException e) {             e.printStackTrace();         }