What Is Kotlin Multiplatform And How Does It Work? - KMP for Beginners
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
🚀 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.
🛠 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.
🚧 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
💡JetBrains
💡Cross-platform
💡Jetpack Compose
💡Shared Code
💡Native APIs
💡Compilation
💡Targets
💡Interoperability
💡Gradle
💡Limitations
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
hey guys and welcome back to a new video
and a new playlist on my channel because
I decided to finally make a whole
dedicated playlist on Copland
multiplatform and compos multiplatform
because those two technologies are more
and more becoming a viable option in the
crossplatform space and in this playlist
I will really start from the absolute
Basics so no matter if you've never
worked with C multiplatform before no
matter if you don't even know what that
is at this point or if you already got
your hands dirty on it this playlist
will be structured so that everybody can
learn something in this video I first of
all want to talk about what coton
multiplatform actually is what composed
multiplatform is how both these
Technologies work under the hood so I
will explain all the magic behind the
scenes and at the end of the video I
will talk about some limitations the
technology itself has so what is cotland
multiplatform or k Fe as an abbreviation
normally when we build software that
each specific platform we develop for so
desktop mobile web all those platforms
have their very own native way of
building software for it and that just
leads to Windows applications not
running on mechos that leads to iOS apps
not running on Android and Android apps
not running in your browser yet very
often we working on the same application
for multiple platforms so especially
when you're building a mobile app then
it should be a no-brainer to bring out
the same apps for both IOS and Android
however normally that would require us
to build two separate apps even though
the actual behavior of the app is very
similar so if we have
desktop we might have IOS as a platform
we might have and
Android and we might have web that if we
build the same application that should
run on all those platforms then there
will always be a common part I am
drawing very ugly and there is actually
a common part between those platforms
and in reality the common part is much
bigger than I can illustrate here so why
isn't there a way that lets us share
this common logic so the logic that is
the same between all those platforms and
let us only write that logic multiple
times that actually diff per platform
and there is it's called cotlin
multiplatform surprise surprise so coton
multiplatform or KP is just a very
trending technology by jet brains so by
the same guys who are behind your
favorite idees so Android Studio intell
J and so on and KP allows us to do
exactly that it allows us to share the
common logic between all those different
platforms I have on the screen here by
just specifying that only once in
cotland and that not only has the
advantage that you need to write much
much less code so you need to write a
shared code snippit only once instead of
four times in this example but it also
has the advantage that you only need to
test your shared code once so you need
one single test case and it will cover
possibly four platforms so that's really
what cotton multiplatform is it really
allows us to share cotton code between
different targets so between different
platforms but what is now compos
multiplatform because that's also a
common buzzword you hear in combination
with coton multiplatform and you can see
composed multiplatform as kind of an
add-on to carton multiplatform that also
lets as sheer UI code so compose itself
or jetpack compose itself is really just
Android's Modern Way of building uis by
just describing how the UI looks like on
cotland and originally that was only
available for Android but nowadays we
also have the option to use jetpack
compos for cotland multiplatform and
that means if our app shares the same UI
components across platform so if we have
the same type of button on web then we
have on Android then we have on iOS then
we have on desktop then we just need to
Define that button that maybe
specialized style button in our own kind
of branding and theming we only need to
Define that once in our shared coton
code and we can still use it on every
single platform as of now composed
multiplatform is still in beta for iOS
so it's not officially stable yet and I
think it's also in Alpha for web
applications but technically it already
support all these platforms here because
I definitely see the future of cotton
multiplatform use compos multiplatform
as well I will definitely also use
compos multiplatform in this playlist
here so let's talk about how KMP
actually works under the hood what is
really the magic behind this tool that
allows us to share cotlin code for so
many different platforms that actually
don't even use cotlin natively so
generally when we have a KP project then
we have to specify so-called Targets
target is nothing else than a platform
we want our app to run on so to keep
things simple let's assume we have an
iOS Target and we have an Android Target
if you get deeper into carton
multiplatform you will also notice that
specific targets also represent specific
types of CPU architectures cu the CPU
architecture has a lot of impact on what
kind of code and what kind of binaries
it can understand but on a higher level
we can just say okay we have Android and
iOS in this little example but this
could also be extended with desktop so
with Mac OS Windows Linux and of course
web and when we have those targets here
then every single target has its own
kind of let's call it preferred way of
reading code or preferred way of
understanding code so Android for
example runs on the jbm the Java virtual
machine so that means it wants Java bite
code from us which it will then feed
into the Java virtual machine and that
one will further process the Java bite
code however on iOS we don't have such a
thing like the jvm that's not compatible
with iOS iOS actually works with Native
binaries Native binaries are in the end
just files with a bunch of zeros and
ones which are pre-compiled it doeses
not work with a jvm it just has its own
kind of CPU architecture which has its
own format these CPU instructions have
to be in if we now have our sheared
cotton code which is really just one
piece of code and we still want that to
run on specific multiple different
targets on Android and on iOS the
cartton compiler in this case has to
somehow figure out how to compile that
carton code into code that natively runs
on Android and on iOS if we would then
try to actually create an executable app
out of our KMP project then what the
compiler will do is it will take a look
okay here I actually want to build that
Cent multiplatform project for an
Android Target oh Android actually
supports the jvm so it will take our
shared content code and compile that to
Java B code and that is something that
runs very well on Android that is also
what normal native Android apps compile
to so no problem there but when the
compiler will then noce okay actually
our developers also want our app to run
on iOS which does not work with the jvm
but with Native binaries then the cotton
compiler will actually take a look at
the Cotton code and compile that into
those specific native binaries iOS wants
to understand here we compile that to
Native binaries you know why that is so
cool this way of compiling a coton
multiplatform project means that our app
is still running n ly on every single
platform we make it run on so that means
that from our KMP code from the sheared
cotton code we can still call the native
apis directly from Android and from IOS
so a native API could for example be hey
we want to show a notification on our
device that is something that of course
Works quite differently on iOS than on
Android because these are just different
operating systems and Apple has
different ideas how showing notification
works on iOS than Google has about
Android but both Google and apple
actually provide their own kind of API
their own kinds of sets of functionality
that let us developers show a
notification in our code and with KMP we
can use those specific apis directly
that Apple or Google provide with other
crossplatform Technologies like flutter
for example that's not possible while
flutter also compiles to native code
which runs natively on each platform
with flutter there is actually a bridge
layer necessary so some kind of layer in
between which interprets the code from
flutter and translates that into
something specific platform needs to
understand so from flutter we can't call
these system apis directly in our
flutter code like it's possible with
Carton multiplatform and by just saving
this step in a KP project and directly
compiling to native code we of course
have more performant apps after all and
this is really unique to coton
multiplatform because there is no other
crossplatform framework that allows us
to actually call these native system
apis directly in the crossplatform
framework itself so I hope that gave you
a good understanding of how these
Technologies actually work under the
hood the last thing I I want to talk
about about are some limitations of
cotland multiplatform and that is that
you can only use cotland libraries
inside of a cotland multiplatform
project so while on Android we can use
libraries that were written in cotland
we can use libraries that were written
in Java because those two Java and
cotland are interoperable so in the end
they compile to the same jvm bite code
that Android understands but using Java
libraries in the cotton multiplatform
shared code is not possible while that
would still work for our Android Target
for our Android platform it wouldn't for
iOS for example because iOS doesn't have
a thing like the jvm and Java libraries
just very often depend on specific
behavior or functionality from the jvm
and also the build system behind KMP is
usually Gradle so also what we use for
Android so just the thing that really
packages together a working app that
controls all those little tasks like
compiling our app packaging it together
to an executable file and so on and in
order for a library to properly support
Cartland multiplatform it just also
needs to include the correct
configuration for every single platform
it should run on and this by default
Java libraries just don't do so what
that also means is is if you might be
working on a pure native Android app at
the moment and you may be plan to also
make that work for iOS or potentially
more Platforms in future then it's very
smart to already start pure cotton
libraries from now on because then the
migration will be very easy later on and
one last limitation of carton
multiplatform but that actually a
limitation of carton multiplatform but
rather of Apple if you want to build iOS
apps or Macos apps with coton
multiplatform you need actually a Mac
because those types of applications
simply only run on a Mac if you don't
have a Mac you can still use carton
multiplatform you can still use composed
multiplatform make that work for Android
make that work for desktop and also make
that work for iOS but you won't be able
to run that IOS app but other than that
I think it's a very very exciting
technology I see a very bright future in
coton multiplatform especially in
composed multiplatform and I think we
will have a lot of fun here in this
playlist if you actually want to learn
how you can build a full app in Cartland
multiplatform then check the link down
below because I already have a full
course about it other than that thanks
so much for watching I will see you back
in the next video have an amazing rest
of your week bye-bye
[Music]
[Music]
Browse More Related Video
KMP vs. Flutter - Who Will Win The Cross-Platform Battle?
Kim Chouard – Zero to Music Hero: Building Cross-Platform Games in Months With React Native & Skia
Chegou a Vez do Kotlin em 2024? (Análise da Linguagem ao Mercado)
Flutter Basic Training - 12 Minute Bootcamp
WebAssembly: A new development paradigm for the web
Expo in 100 Seconds
5.0 / 5 (0 votes)