Programming Android with Kotlin
by Pierre-Olivier Laurence and Amanda Hinchman-Dominguez , with G. Blake Meike and Mike Dunn
Copyright 2022 Pierre-Olivier Laurence and Amanda Hinchman-Dominguez. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Acquisition Editor: Suzanne McQuade
- Development Editor: Jeff Bleiel
- Production Editor: Beth Kelly
- Copyeditor: Piper Editorial Consulting, LLC
- Proofreader: Audrey Doyle
- Indexer: WordCo Indexing Services, Inc.
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Kate Dullea
- December 2021: First Edition
Revision History for the First Edition
- 2021-12-03: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492063001 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Programming Android with Kotlin, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the authors, and do not represent the publishers views. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-492-06300-1
[LSI]
Preface
JetBrains created Kotlin for two reasons: there was no language that filled all the gaps in Android developmentusing (legacy) Java libraries, and a new language would allow Android development to set trends, rather than justfollow them.
In February 2015, Kotlin 1.0 was officially announced. Kotlin is concise, safe, pragmatic, and focused on interoperability with Java code. It can be used everywhere Java is used today: for server-side development, Android apps, desktop or portable clients, IoT device programming, and much, much more. Kotlin gained popularity among Android developers quite rapidly, and Googles decision to adopt Kotlin as the official language of Android development resulted in skyrocketing interest in the language. According to the Android Developers website, more than 60% of professional Android developers currently use Kotlin.
The learning curve in Android is rather steep: admittedly, its hard to learn and harder to master. Part of theAndroid developer upbringing, for many, is to be exposed over time to unintended interactions between the Android operating system and the application. This book intends to bring those kinds of exposures to readers in depth and up close by examining such problems in Android. Well talk not only about Kotlin and Java, but also about the concurrency problems that arise when using Android and how Kotlin is able to solve them.
We will sometimes compare Kotlin to Java when we believe doing so provides better insight (especially since most readers are expected to have a Java background). We can demonstrate, with working examples, how to bridge that gap, and how the underlying concepts of most Kotlin operations are more similar to the Java equivalent than not. The tasks will be organized by topic to provide software engineers with a structured decomposition of that mass of information, and they will show how to make an application robust and maintainable.
Additionally, users familiar with Javaincluding Android developerswill find their learning curve dramatically flatten when we present each of the common tasks inboth Java and Kotlin. Where appropriate, well discuss the difference and the pitfalls of one or both,but we hope to provide bite-size and easily digestible examples of a task that will just work,and enable the reader to consume and adapt to the modern paradigm, as well as become aware of thesignificance of the updated code immediately and instinctively.
While Kotlin is fully interoperable with Java, other Java application development (server-side programming,desktop clients, middleware, etc.) has not caught on to the extent that Android has. This is largely due to themaintainer of Android (Google) strongly encouraging its users to make the change. Users are regularly migratingto Kotlin, but even more still fall back to Java for mission-critical work. Our hope is that this book willserve as the lifeline an Android developer needs to feel safe in committing to the advantages and simplicitythat Kotlin represents.
Who Should Read This Book
Any of the over six million Android engineers. We believe that virtually every Android engineer could benefit from this book. While a small percentage will be fluent in Kotlin, even they will likely learn something from the information well present. But realistically, were targeting the very large majority who havent made the transition to Kotlin. This book is also for those who have dipped a toe in but not gained the same level of familiarity with Kotlin that they may have accrued in Java-centric Android development:
Scenario 1
A reader is proficient in Java, heard of this new Kotlin language, and wants to try it out. So they read some online tutorial and start using it and it works great. Soon they realize that this isnt just a new syntax.The idioms arent the same (e.g., functional programming, coroutines) and a whole new way of developing is now possible. But they lack guidelines, structure. For them, this book is a perfect fit.
Scenario 2
A reader is part of a small team of Java developers. They have discussions about whether they should start including Kotlin in their project. Even if Kotlin is said to be 100% interoperable with Java, some colleagues argue that introducing another language will add complexity to the project. Others suggest it might limit the number of colleagues who will be able to work on the project because of the need to master two languages. The reader could use this book to convince their colleagues, if they can show that the benefits will outweigh the costs.
Scenario 3
An experienced Android developer may have played around with Kotlin or written a feature in it, but still falls back to the home base of Java when things need to get done. This was the scenario we found ourselves inwhen realizing the book were pitching now would have made our lives much easier. This is also the state we see most commonly around usmany Android devs have touched Kotlin, and many feel like they understand enough to write it when necessary, but they are either unaware, or simply unconvinced, of the significance of data classes, immutable properties, and structured concurrency. We think this book will turn a curious person into a committed evangelist.
Why We Wrote This Book
There are plenty of books that show how Android works, how Kotlin works, or how concurrency works. Kotlin is becoming wildly popular with Android development for its easy adoption and cleaner syntax, but Kotlin offers Android much more than that: it offers new ways to solve concurrency problems in Android. We wrote this book to provide a unique and specific intersectionality of these topics in great depth. Both Android and Kotlin are rapidly changing, separately and together. Trying to keep up with all the changes can be difficult.