What Is Kotlin Multiplatform And How Does It Work? - KMP for Beginners

Philipp Lackner
16 Jun 202410:16

Summary

TLDRThis video introduces a new playlist focusing on Kotlin Multiplatform and Compose Multiplatform, emerging technologies for cross-platform development. The host explains the basics of sharing common logic and UI code across different platforms like Android, iOS, and the web, highlighting the efficiency and performance benefits. The video also delves into the technical workings of Kotlin Multiplatform, its limitations, and the necessity of using Kotlin libraries. A full course on building apps with these technologies is teased for those interested in deeper learning.

Takeaways

  • 🌐 The video introduces a new playlist focused on Kotlin Multiplatform (KMP) and Compose Multiplatform, highlighting their growing importance in cross-platform development.
  • 🛠️ KMP allows developers to share common logic and code across different platforms like desktop, mobile (iOS, Android), and web, reducing the need to write platform-specific code multiple times.
  • 🔍 Compose Multiplatform is an extension of KMP that enables sharing of UI code, leveraging Jetpack Compose for a consistent UI across platforms.
  • 🚀 The video promises to explain the basics of KMP and Compose Multiplatform, making it accessible for beginners and experienced developers alike.
  • 🛡️ Compose Multiplatform is currently in beta for iOS and alpha for web applications, indicating that it's still in development and not yet stable for these platforms.
  • 🔧 KMP works by compiling shared Kotlin code into platform-specific code, such as Java bytecode for Android and native binaries for iOS, to ensure optimal performance.
  • 🔄 The Kotlin compiler in KMP is capable of understanding and compiling to the preferred code format for each target platform, enabling direct use of native APIs.
  • 📚 KMP projects require the use of Kotlin libraries and cannot directly utilize Java libraries in the shared code, due to differences in platform support and JVM dependency.
  • 💡 For developers considering expanding their Android apps to other platforms, starting with Kotlin libraries can facilitate easier migration to KMP in the future.
  • 🍎 A limitation of KMP, especially for iOS and macOS app development, is the requirement of a Mac for building and running the apps, as these platforms are exclusive to that hardware.
  • 🔗 The video concludes by encouraging viewers to check out a full course on building an app in KMP if they're interested in a more in-depth learning experience.

Q & A

  • What is the main focus of the new playlist on the channel?

    -The new playlist is dedicated to exploring Kotlin Multiplatform and Compose Multiplatform, focusing on their capabilities and how they work in the context of cross-platform development.

  • Why is Kotlin Multiplatform (KMP) considered a viable option in the cross-platform space?

    -Kotlin Multiplatform is a trending technology by JetBrains that allows developers to share common logic between different platforms, reducing the need to write the same code multiple times and simplifying testing across platforms.

  • What does Compose Multiplatform offer in addition to Kotlin Multiplatform?

    -Compose Multiplatform is an add-on to Kotlin Multiplatform that allows developers to share UI code across platforms. It was originally available for Android but has been extended to support other platforms within KMP.

  • How does Kotlin Multiplatform address the issue of platform-specific code?

    -Kotlin Multiplatform allows developers to write shared code once and then compile it into platform-specific code, such as Java bytecode for Android or native binaries for iOS, ensuring that the app runs natively on each platform.

  • What is the significance of being able to call native APIs directly in Kotlin Multiplatform?

    -The ability to call native APIs directly in Kotlin Multiplatform provides more performance and flexibility compared to other cross-platform frameworks that require an intermediary layer to interpret the code for each platform.

  • What limitations does Kotlin Multiplatform have regarding the use of libraries?

    -Kotlin Multiplatform can only use Kotlin libraries within its shared code. Java libraries, which are interoperable with Kotlin on Android, cannot be used in the shared code due to differences in platform requirements and the lack of JVM on platforms like iOS.

  • Why is a Mac required to build iOS or macOS apps with Kotlin Multiplatform?

    -Building iOS or macOS apps with Kotlin Multiplatform requires a Mac because the applications can only run on a Mac, which is necessary for compiling and packaging the apps for these platforms.

  • What does the speaker suggest for developers currently working on native Android apps who might want to extend to other platforms in the future?

    -The speaker suggests that developers should start using pure Kotlin libraries from now on to facilitate an easier migration to Kotlin Multiplatform for extending their apps to other platforms in the future.

  • What does the speaker imply about the future of Kotlin Multiplatform and Compose Multiplatform?

    -The speaker is optimistic about the future of Kotlin Multiplatform and Compose Multiplatform, seeing them as exciting technologies with a bright future in cross-platform development.

  • How does the speaker plan to structure the playlist to cater to different levels of experience with Kotlin Multiplatform?

    -The playlist is structured to start from the absolute basics, making it accessible to those who have never worked with Kotlin Multiplatform before, while also providing value for those who have already started working with it.

  • What additional resources does the speaker offer for those interested in learning more about building a full app in Kotlin Multiplatform?

    -The speaker offers a full course on building a full app in Kotlin Multiplatform, which can be found through the link provided in the video description.

Outlines

00:00

🚀 Introduction to Kotlin Multiplatform and Compose Multiplatform

The video introduces a new playlist focused on Kotlin Multiplatform (KMP) and Compose Multiplatform, two technologies gaining traction for cross-platform development. The host aims to cover the basics, making the content accessible to newcomers and experienced developers alike. The goal is to explain what KMP and Compose Multiplatform are, how they operate, and their current limitations. The host emphasizes the ability to share common logic across different platforms, reducing code duplication and the need for multiple tests.

05:02

🛠 Understanding Kotlin Multiplatform's Compilation Process

This paragraph delves into the technical workings of Kotlin Multiplatform, explaining how the shared Kotlin code is compiled for different platforms. It contrasts the Java bytecode used by Android with the native binaries required by iOS, illustrating how the Kotlin compiler adapts the code accordingly. The host highlights the advantages of direct API calls to native functionalities without an intermediate layer, as seen in other cross-platform frameworks like Flutter, and touches on the performance benefits and unique selling points of KMP.

10:06

🚧 Limitations and Considerations for Kotlin Multiplatform Development

The final paragraph addresses the limitations of Kotlin Multiplatform, such as the inability to use Java libraries within shared code due to the lack of JVM on non-Android platforms. It also mentions the necessity of a Mac for building iOS or macOS apps with KMP, as these can only be executed on a Mac. The host suggests starting with pure Kotlin libraries for easier migration to KMP in the future and concludes by expressing enthusiasm for the technology's potential and inviting viewers to explore a full course on building an app with KMP.

Mindmap

Keywords

💡Cotlin Multiplatform

Cotlin Multiplatform, abbreviated as KMP, is a technology by JetBrains that enables developers to write shared code in Kotlin that can run on multiple platforms such as desktop, mobile, web, and more. The video's theme revolves around this technology, as it allows for code reusability and reduces the need to write platform-specific code. For instance, the script mentions that with KMP, developers can share common logic between different platforms, which is a significant advantage in cross-platform development.

💡JetBrains

JetBrains is the company behind the Kotlin programming language and the Kotlin Multiplatform technology. The script refers to JetBrains as the creators of popular IDEs like Android Studio and IntelliJ IDEA, which are essential tools for developers. The mention of JetBrains establishes the credibility and origin of Kotlin Multiplatform.

💡Cross-platform

Cross-platform refers to the ability of an application or technology to work across different types of platforms and devices. The video discusses how Kotlin Multiplatform facilitates the development of cross-platform applications by sharing common logic and UI components across platforms like iOS, Android, and the web. The term is central to the video's message, emphasizing the efficiency and versatility of KMP.

💡Jetpack Compose

Jetpack Compose is Android's modern toolkit for building native UIs in a declarative way. The script introduces Compose Multiplatform as an extension of Kotlin Multiplatform that allows developers to share UI code across different platforms. The video discusses how Compose can be used in conjunction with KMP to create a consistent user interface across platforms, which is a significant aspect of modern app development.

💡Shared Code

Shared code is the common logic or functionality that can be written once and used across multiple platforms without the need for duplication. The script highlights the benefits of shared code in Kotlin Multiplatform, such as reducing the amount of code to write and test, which is a key advantage of using KMP for cross-platform development.

💡Native APIs

Native APIs are the application programming interfaces provided by the operating system that allow developers to access the full functionality of the platform. The video explains that Kotlin Multiplatform allows direct access to native APIs on each platform, unlike some other cross-platform frameworks that require a bridge layer. This feature is crucial for leveraging platform-specific features and enhancing app performance.

💡Compilation

Compilation is the process of translating code written in a high-level programming language into machine code that the computer can execute. The script describes how Kotlin Multiplatform compiles shared Kotlin code into platform-specific bytecode or native binaries, depending on the target platform. This process is fundamental to how KMP enables cross-platform functionality.

💡Targets

In the context of Kotlin Multiplatform, targets refer to the specific platforms that an application is intended to run on, such as iOS, Android, or the web. The script explains that developers must specify targets in a KMP project, and the compiler generates code suitable for each target's execution environment.

💡Interoperability

Interoperability is the ability of different systems or components to work together. The script mentions that Kotlin and Java are interoperable on Android because they both compile to the same JVM bytecode. However, using Java libraries in shared Kotlin code is not supported in Kotlin Multiplatform due to the lack of JVM on other platforms like iOS.

💡Gradle

Gradle is an open-source build automation system that is widely used in Android development and is also the build system behind Kotlin Multiplatform. The script refers to Gradle as the tool that packages the application into an executable file and manages the build process, which is essential for understanding the development workflow in KMP.

💡Limitations

Limitations refer to the constraints or restrictions of a technology or approach. The video discusses some limitations of Kotlin Multiplatform, such as the requirement of a Mac for building iOS or macOS apps and the inability to use Java libraries in shared code. These limitations are important for developers to consider when deciding whether to adopt KMP for their projects.

Highlights

Introduction of a new playlist dedicated to Kotlin Multiplatform and Compose Multiplatform, focusing on cross-platform development.

Kotlin Multiplatform (KMP) allows sharing common logic across different platforms without writing the same code multiple times.

JetBrains, the creators of IntelliJ IDEA and Android Studio, are behind Kotlin Multiplatform.

KMP enables testing shared code once, covering multiple platforms.

Compose Multiplatform is an add-on to KMP, allowing UI code sharing across platforms.

Jetpack Compose is Android's modern UI toolkit, now extended for use in KMP.

Compose Multiplatform is in beta for iOS and alpha for web applications but supports all platforms.

Explanation of how KMP works under the hood, including the concept of targets representing different platforms.

Targets in KMP specify platforms and their preferred code understanding, such as JVM for Android or native binaries for iOS.

KMP's compiler compiles shared Kotlin code into platform-specific code, like Java bytecode for Android or native binaries for iOS.

KMP allows direct calling of native APIs, unlike other cross-platform frameworks which require a bridge layer.

The performance advantage of KMP due to direct compilation to native code without a bridge layer.

Limitation of KMP: Only Kotlin libraries can be used in a KMP project, not Java libraries.

Recommendation to start using Kotlin libraries for easier migration to KMP in the future.

The necessity of a Mac for building iOS or macOS apps with KMP due to Apple's platform restrictions.

Enthusiasm for the future of Kotlin Multiplatform and Compose Multiplatform in cross-platform development.

Invitation to a full course on building an app in Kotlin Multiplatform for those interested in learning more.

Closing remarks and sign-off for the video.

Transcripts

play00:00

hey guys and welcome back to a new video

play00:01

and a new playlist on my channel because

play00:03

I decided to finally make a whole

play00:06

dedicated playlist on Copland

play00:07

multiplatform and compos multiplatform

play00:10

because those two technologies are more

play00:11

and more becoming a viable option in the

play00:14

crossplatform space and in this playlist

play00:15

I will really start from the absolute

play00:17

Basics so no matter if you've never

play00:19

worked with C multiplatform before no

play00:21

matter if you don't even know what that

play00:23

is at this point or if you already got

play00:24

your hands dirty on it this playlist

play00:26

will be structured so that everybody can

play00:28

learn something in this video I first of

play00:30

all want to talk about what coton

play00:31

multiplatform actually is what composed

play00:33

multiplatform is how both these

play00:35

Technologies work under the hood so I

play00:37

will explain all the magic behind the

play00:39

scenes and at the end of the video I

play00:40

will talk about some limitations the

play00:42

technology itself has so what is cotland

play00:45

multiplatform or k Fe as an abbreviation

play00:48

normally when we build software that

play00:50

each specific platform we develop for so

play00:52

desktop mobile web all those platforms

play00:55

have their very own native way of

play00:57

building software for it and that just

play00:58

leads to Windows applications not

play01:00

running on mechos that leads to iOS apps

play01:03

not running on Android and Android apps

play01:05

not running in your browser yet very

play01:07

often we working on the same application

play01:09

for multiple platforms so especially

play01:11

when you're building a mobile app then

play01:12

it should be a no-brainer to bring out

play01:14

the same apps for both IOS and Android

play01:17

however normally that would require us

play01:18

to build two separate apps even though

play01:20

the actual behavior of the app is very

play01:22

similar so if we have

play01:25

desktop we might have IOS as a platform

play01:29

we might have and

play01:31

Android and we might have web that if we

play01:34

build the same application that should

play01:36

run on all those platforms then there

play01:38

will always be a common part I am

play01:42

drawing very ugly and there is actually

play01:44

a common part between those platforms

play01:46

and in reality the common part is much

play01:47

bigger than I can illustrate here so why

play01:50

isn't there a way that lets us share

play01:52

this common logic so the logic that is

play01:54

the same between all those platforms and

play01:56

let us only write that logic multiple

play01:58

times that actually diff per platform

play02:01

and there is it's called cotlin

play02:02

multiplatform surprise surprise so coton

play02:05

multiplatform or KP is just a very

play02:07

trending technology by jet brains so by

play02:10

the same guys who are behind your

play02:11

favorite idees so Android Studio intell

play02:13

J and so on and KP allows us to do

play02:17

exactly that it allows us to share the

play02:19

common logic between all those different

play02:21

platforms I have on the screen here by

play02:23

just specifying that only once in

play02:25

cotland and that not only has the

play02:26

advantage that you need to write much

play02:28

much less code so you need to write a

play02:30

shared code snippit only once instead of

play02:32

four times in this example but it also

play02:33

has the advantage that you only need to

play02:35

test your shared code once so you need

play02:37

one single test case and it will cover

play02:39

possibly four platforms so that's really

play02:42

what cotton multiplatform is it really

play02:43

allows us to share cotton code between

play02:46

different targets so between different

play02:47

platforms but what is now compos

play02:49

multiplatform because that's also a

play02:50

common buzzword you hear in combination

play02:52

with coton multiplatform and you can see

play02:54

composed multiplatform as kind of an

play02:57

add-on to carton multiplatform that also

play02:59

lets as sheer UI code so compose itself

play03:02

or jetpack compose itself is really just

play03:04

Android's Modern Way of building uis by

play03:06

just describing how the UI looks like on

play03:08

cotland and originally that was only

play03:10

available for Android but nowadays we

play03:12

also have the option to use jetpack

play03:13

compos for cotland multiplatform and

play03:15

that means if our app shares the same UI

play03:18

components across platform so if we have

play03:20

the same type of button on web then we

play03:22

have on Android then we have on iOS then

play03:24

we have on desktop then we just need to

play03:25

Define that button that maybe

play03:27

specialized style button in our own kind

play03:29

of branding and theming we only need to

play03:31

Define that once in our shared coton

play03:33

code and we can still use it on every

play03:35

single platform as of now composed

play03:37

multiplatform is still in beta for iOS

play03:39

so it's not officially stable yet and I

play03:41

think it's also in Alpha for web

play03:43

applications but technically it already

play03:45

support all these platforms here because

play03:47

I definitely see the future of cotton

play03:49

multiplatform use compos multiplatform

play03:51

as well I will definitely also use

play03:53

compos multiplatform in this playlist

play03:55

here so let's talk about how KMP

play03:57

actually works under the hood what is

play03:59

really the magic behind this tool that

play04:00

allows us to share cotlin code for so

play04:02

many different platforms that actually

play04:04

don't even use cotlin natively so

play04:05

generally when we have a KP project then

play04:08

we have to specify so-called Targets

play04:10

target is nothing else than a platform

play04:12

we want our app to run on so to keep

play04:14

things simple let's assume we have an

play04:17

iOS Target and we have an Android Target

play04:21

if you get deeper into carton

play04:22

multiplatform you will also notice that

play04:24

specific targets also represent specific

play04:26

types of CPU architectures cu the CPU

play04:29

architecture has a lot of impact on what

play04:31

kind of code and what kind of binaries

play04:33

it can understand but on a higher level

play04:34

we can just say okay we have Android and

play04:36

iOS in this little example but this

play04:37

could also be extended with desktop so

play04:39

with Mac OS Windows Linux and of course

play04:42

web and when we have those targets here

play04:44

then every single target has its own

play04:46

kind of let's call it preferred way of

play04:48

reading code or preferred way of

play04:50

understanding code so Android for

play04:51

example runs on the jbm the Java virtual

play04:54

machine so that means it wants Java bite

play04:56

code from us which it will then feed

play04:58

into the Java virtual machine and that

play04:59

one will further process the Java bite

play05:01

code however on iOS we don't have such a

play05:04

thing like the jvm that's not compatible

play05:06

with iOS iOS actually works with Native

play05:09

binaries Native binaries are in the end

play05:11

just files with a bunch of zeros and

play05:13

ones which are pre-compiled it doeses

play05:15

not work with a jvm it just has its own

play05:17

kind of CPU architecture which has its

play05:19

own format these CPU instructions have

play05:21

to be in if we now have our sheared

play05:23

cotton code which is really just one

play05:24

piece of code and we still want that to

play05:26

run on specific multiple different

play05:28

targets on Android and on iOS the

play05:30

cartton compiler in this case has to

play05:31

somehow figure out how to compile that

play05:33

carton code into code that natively runs

play05:36

on Android and on iOS if we would then

play05:38

try to actually create an executable app

play05:40

out of our KMP project then what the

play05:42

compiler will do is it will take a look

play05:43

okay here I actually want to build that

play05:47

Cent multiplatform project for an

play05:48

Android Target oh Android actually

play05:50

supports the jvm so it will take our

play05:52

shared content code and compile that to

play05:55

Java B code and that is something that

play05:58

runs very well on Android that is also

play06:00

what normal native Android apps compile

play06:02

to so no problem there but when the

play06:04

compiler will then noce okay actually

play06:05

our developers also want our app to run

play06:07

on iOS which does not work with the jvm

play06:09

but with Native binaries then the cotton

play06:11

compiler will actually take a look at

play06:12

the Cotton code and compile that into

play06:14

those specific native binaries iOS wants

play06:17

to understand here we compile that to

play06:20

Native binaries you know why that is so

play06:23

cool this way of compiling a coton

play06:25

multiplatform project means that our app

play06:28

is still running n ly on every single

play06:30

platform we make it run on so that means

play06:33

that from our KMP code from the sheared

play06:35

cotton code we can still call the native

play06:38

apis directly from Android and from IOS

play06:40

so a native API could for example be hey

play06:43

we want to show a notification on our

play06:45

device that is something that of course

play06:46

Works quite differently on iOS than on

play06:48

Android because these are just different

play06:50

operating systems and Apple has

play06:52

different ideas how showing notification

play06:53

works on iOS than Google has about

play06:55

Android but both Google and apple

play06:57

actually provide their own kind of API

play06:59

their own kinds of sets of functionality

play07:01

that let us developers show a

play07:03

notification in our code and with KMP we

play07:05

can use those specific apis directly

play07:07

that Apple or Google provide with other

play07:09

crossplatform Technologies like flutter

play07:11

for example that's not possible while

play07:13

flutter also compiles to native code

play07:15

which runs natively on each platform

play07:17

with flutter there is actually a bridge

play07:19

layer necessary so some kind of layer in

play07:22

between which interprets the code from

play07:24

flutter and translates that into

play07:27

something specific platform needs to

play07:29

understand so from flutter we can't call

play07:30

these system apis directly in our

play07:32

flutter code like it's possible with

play07:34

Carton multiplatform and by just saving

play07:36

this step in a KP project and directly

play07:40

compiling to native code we of course

play07:41

have more performant apps after all and

play07:44

this is really unique to coton

play07:45

multiplatform because there is no other

play07:47

crossplatform framework that allows us

play07:49

to actually call these native system

play07:51

apis directly in the crossplatform

play07:53

framework itself so I hope that gave you

play07:55

a good understanding of how these

play07:56

Technologies actually work under the

play07:58

hood the last thing I I want to talk

play07:59

about about are some limitations of

play08:00

cotland multiplatform and that is that

play08:02

you can only use cotland libraries

play08:04

inside of a cotland multiplatform

play08:06

project so while on Android we can use

play08:08

libraries that were written in cotland

play08:09

we can use libraries that were written

play08:11

in Java because those two Java and

play08:13

cotland are interoperable so in the end

play08:15

they compile to the same jvm bite code

play08:17

that Android understands but using Java

play08:19

libraries in the cotton multiplatform

play08:21

shared code is not possible while that

play08:23

would still work for our Android Target

play08:25

for our Android platform it wouldn't for

play08:26

iOS for example because iOS doesn't have

play08:28

a thing like the jvm and Java libraries

play08:30

just very often depend on specific

play08:32

behavior or functionality from the jvm

play08:34

and also the build system behind KMP is

play08:37

usually Gradle so also what we use for

play08:39

Android so just the thing that really

play08:40

packages together a working app that

play08:42

controls all those little tasks like

play08:44

compiling our app packaging it together

play08:46

to an executable file and so on and in

play08:48

order for a library to properly support

play08:50

Cartland multiplatform it just also

play08:52

needs to include the correct

play08:53

configuration for every single platform

play08:55

it should run on and this by default

play08:57

Java libraries just don't do so what

play08:58

that also means is is if you might be

play09:00

working on a pure native Android app at

play09:02

the moment and you may be plan to also

play09:04

make that work for iOS or potentially

play09:06

more Platforms in future then it's very

play09:08

smart to already start pure cotton

play09:10

libraries from now on because then the

play09:12

migration will be very easy later on and

play09:14

one last limitation of carton

play09:15

multiplatform but that actually a

play09:17

limitation of carton multiplatform but

play09:18

rather of Apple if you want to build iOS

play09:21

apps or Macos apps with coton

play09:22

multiplatform you need actually a Mac

play09:24

because those types of applications

play09:26

simply only run on a Mac if you don't

play09:28

have a Mac you can still use carton

play09:30

multiplatform you can still use composed

play09:31

multiplatform make that work for Android

play09:33

make that work for desktop and also make

play09:36

that work for iOS but you won't be able

play09:37

to run that IOS app but other than that

play09:39

I think it's a very very exciting

play09:41

technology I see a very bright future in

play09:43

coton multiplatform especially in

play09:44

composed multiplatform and I think we

play09:46

will have a lot of fun here in this

play09:48

playlist if you actually want to learn

play09:50

how you can build a full app in Cartland

play09:51

multiplatform then check the link down

play09:53

below because I already have a full

play09:55

course about it other than that thanks

play09:56

so much for watching I will see you back

play09:57

in the next video have an amazing rest

play09:59

of your week bye-bye

play10:06

[Music]

play10:12

[Music]

Rate This

5.0 / 5 (0 votes)

Related Tags
Kotlin MultiplatformCross-PlatformApp DevelopmentShared CodeMobile AppsWeb DevelopmentJetpack ComposeNative APIsPlatform TargetsCode Compilation