Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Simplifying Application Development with Kotlin Multiplatform Mobile

You're reading from  Simplifying Application Development with Kotlin Multiplatform Mobile

Product type Book
Published in Mar 2022
Publisher Packt
ISBN-13 9781801812580
Pages 184 pages
Edition 1st Edition
Languages
Author (1):
Róbert Nagy Róbert Nagy
Profile icon Róbert Nagy

Table of Contents (15) Chapters

Preface Section 1 - Getting Started with Multiplatform Mobile Development Using Kotlin
Chapter 1: The Battle Between Native, Cross-Platform, and Multiplatform Chapter 2: Exploring the Three Compilers of Kotlin Multiplatform Chapter 3: Introducing Kotlin for Swift Developers Section 2 - Code Sharing between Android and iOS
Chapter 4: Introducing the KMM Learning Project Chapter 5: Writing Shared Code Chapter 6: Writing the Android Consumer App Chapter 7: Writing an iOS Consumer App Section 3 - Supercharging Yourself for the Next Steps
Chapter 8: Exploring Tips and Best Practices Chapter 9: Integrating KMM into Existing Android and iOS Apps Chapter 10: Summary and Your Next Steps Other Books You May Enjoy

Chapter 3: Introducing Kotlin for Swift Developers

Before we turn things more practical and start developing our Kotlin Multiplatform Mobile (KMM) apps, I'd like to make sure everyone has the necessary knowledge to follow the steps. This chapter was designed for iOS and Swift developers, especially for those who don't have a comprehensive knowledge of Kotlin and Gradle. Most of the concepts in Swift can be found in Kotlin as well, and in this chapter, we're going to see how Swift's concepts translate to Kotlin. By the end of this chapter, you should be ready for KMM development, by understanding the core Kotlin concepts and other KMM prerequisites. We will be learning about the following topics:

  • Introducing Gradle
  • Exploring Kotlin's core features
  • Understanding Kotlin coroutines

Technical requirements

Introducing Gradle

When writing a KMM application, you'll be using Gradle to build your shared code. For this reason, it's paramount that you at least know the basics, in order to start developing KMM apps.

Gradle is an open source build automation tool and dependency manager. It is similar to CocoaPods on iOS, while covering a broader purpose than pure dependency management, and it is the build tool on which Kotlin Multiplatform (KMP) is also based.

Gradle provides its own domain-specific language (DSL) for writing build scripts, and this DSL is available both in Groovy and Kotlin: build.gradle is a build script written in Groovy, while build.gradle.kts is written in Kotlin.

We will not have an in-depth description of Gradle as it is a huge topic, and without a doubt, many of us as Android developers use it as someone uses a lightbulb: without extensive knowledge on how it works, it still proves to be useful. If you want to gain a more in-depth view of Gradle, I...

Exploring Kotlin's core features

Kotlin is an object-oriented programming (OOP) language with many functional programming features. In this section, we'll go over its main features so that you have a basic understanding of how to express yourself in Kotlin later on. Throughout this chapter, we'll be comparing Kotlin to Swift in terms of these core concepts.

Null safety

Having null references in code proved to be an underestimated factor of error proneness in older languages, such as Java and Objective-C (Obj-C), thus it has probably earned its billion-dollar mistake tag (https://en.wikipedia.org/wiki/Tony_Hoare#Apologies_and_retractions).

The purpose and solution are pretty much similar in Kotlin and Swift, with slight syntax and naming differences. They both aim to provide a type system that eliminates the danger of null references. Swift introduced optional types, which can be found as nullables in Kotlin.

Let's see some code in action, in order to...

Understanding Kotlin coroutines

Asynchronous programming is at the heart of mobile development. In order to write efficient applications, leveraging the async capabilities of the framework and language you are using makes all the difference.

Coroutines are my absolute favorite language feature of Kotlin because of their expressiveness and how easy it is to express your asynchronous development needs in a concise way.

In this section, we will be covering the basic concepts of coroutines and compare them to Swift's async/await and Combine patterns.

Suspend functions

Coroutines are basically suspendable tasks that can suspend and resume execution, and they are not bound to any particular thread.

When you're writing asynchronous code, you generally need to think about the following two things:

  • Which task needs asynchronous attention and has to be suspendable
  • How you combine asynchronous tasks with the rest of your code

Let's see how you...

Summary

In this chapter, we've gone through basic Kotlin and Gradle concepts that are essential in order for you to start sharing code between Android and iOS (and other platforms, potentially) while comparing these concepts to ones that can be found in Swift.

I hope that now you have a proper base knowledge of these tools and you're ready to move forward to more practical things—developing a KMM application.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Simplifying Application Development with Kotlin Multiplatform Mobile
Published in: Mar 2022 Publisher: Packt ISBN-13: 9781801812580
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}