![]() ![]() The folder structure should look like the following. Open the starter project in Android Studio. ![]() Now that you’ve covered the theory, what now? Keep reading to find out. Then, write the platform-specific implementations for the class, method or function in all the other modules using the actual keyword in the signature.Įach platform can share a common interface while also possessing its own implementation. It isn’t possible to use platform-specific code inside the common module, but Kotlin has a mechanism it uses to achieve this result: expect/ actual.įirst, 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. Sometimes you need to call methods that are specific to a platform. Then the platform specifies the interface that will be used in each platform separately. To do this, there can be a common interface acting as common code. Not all code can be common for example, calling native libraries for key-value storage requires writing different code for iOS and Android. Note: For more on this topic, you can read about understanding the KMM project structure on the Kotlin website. You’ll go through the steps of creating this framework later. However, as mentioned earlier, for iOS it’s different, as the code is compiled to native code. If you think it’s easy to understand how Android uses common code, you’re right. On iOS, you can import the shared module as a framework that you can access from Swift code. On Android, it does this by using a shared module with Gradle. KMM allows shared code in Android and iOS. You’ll focus on the mobile aspect, or Kotlin Multiplatform Mobile (KMM). It also supports JavaScript, Windows and other native targets. Kotlin Multiplatform supports more than just Android and iOS. Since you have both Android and iOS apps, you can save some headache by sharing some code for fetching the data and saving your preferences.īut before you get into the implementation, you’ll learn some theory first. It’s also great if you can save the users’ favorite grains locally. Of course, you’ll want to update the list from time to time, and it’s better if it can be done remotely. Once you click the Grains button, you’ll see the screen below. Looking at the code, you’ll find the starter project provides the interface and some logic for the MultiGrain app: A simple app that navigates a list of grains and their descriptions, it saves your favorite grains and has platform specific code in the UI.īuild and run the starter code on Android Studio and you’ll see the screen below. If you are new to Android development, check out our Android Tutorial for Beginners series first. Note: This tutorial assumes you are familiar with the basics of Android and Android Studio. Use a recent version of Kotlin - 1.4.20 or above. Open the starter project in Android Studio 4.1 or later. Ready to get started? Keep reading! Getting Startedĭownload the starter project by clicking the Download Materials button at the top or bottom of the tutorial. Integrate KMM into an existing project.In this tutorial, you’ll learn how to build and update an app for Android and iOS while only having to write the business logic once in Kotlin. Thanks to Kotlin/JVM, Kotlin/JS and Kotlin/Native, you can compile/transpile a single project for many platforms. Historically, it’s known as Kotlin Multiplatform Project, or MPP. These similarities are why Kotlin Multiplatform Mobile (KMM) exists. While these two platforms are different, the business logic behind your app is probably similar: download files, read from and write to a database, send messages to a remote host, and retrieve and display fancy kitten pictures. Dario Coletto wrote the original.Īre you a bit tired of writing two versions of similar code for Android and iOS? Update note: JB Lorenzo updated this tutorial for the alpha version of Kotlin Multiplatform. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |