Kotlin Programming Cookbook
Explore more than 100 recipes that show how to build
robust mobile and web applications with Kotlin, Spring Boot,
and Android
Aanand Shekhar Roy
Rashi Karanpuria
BIRMINGHAM - MUMBAI
Kotlin Programming Cookbook
Copyright 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Merint Mathew
Acquisition Editor: Isha Raval
Content Development Editor: Onkar Wani
Technical Editor: Sachin Sunilkumar
Copy Editor: Shaila Kusanale
Project Coordinator: Devanshi Doshi
Proofreader: Safis Editing
Indexer: Rekha Nair
Graphics: Jason Monteiro
Production Coordinator: Nilesh Mohite
First published: January 2018
Production reference: 1230118
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78847-214-2
www.packtpub.com
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
PacktPub.com
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com , you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors
About the authors
Aanand Shekhar Roy is a freelance Android developer. His mobile engineering career includes working for various startups and companies, such as Netherlands-based ConceptOffice and U.S.-based startups Consciously.Life and NextFan-FantasyIndyCar.
He is also a mentor for the Associate Android Developer Fast Track program by Google on Udacity and mentors a team of 20 software developers. He is also a writer at AndroidPub.
Acknowledgement is probably that part of the book you least have interest in. Simply because every time you open it, it's either thanking someone else who you don't know. Not this time, this book is dedicated to only learners like you, simply because it wouldn't have come to life without you wanting to read it.
Rashi Karanpuria is a professional Android developer. She started her career by working on a wallet app for a New Zealand based Fintech startup. She has developed projects in various domains such as IOT, AdTech, Business, Social, and live streaming apps. Over the years, she has acquired a deep understanding of the user experience needed to develop a successful mobile application. She combines her design skills with scalable and maintainable code using best practices to create quality products.
I would like to thank each & every member of the developer community all across the world. This is the only community that I know of to give ideas and resources so freely without expecting anything in return. Did you know Kotlin is open source? This giving nature of our developer community is why I am who I am today. Thanks.
About the reviewer
Mitchell Wong Ho was born in Johannesburg, South Africa, where he completed his national diploma in electrical engineering. Mitchell's software development career started on embedded systems and then moved to Microsoft desktop/server applications.
Mitchell has been programming in Java since 2000 on J2ME, JEE, desktop, and Android applications, and has more recently been advocating Kotlin for Android.
Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Preface
Kotlin Cookbook will be a go-to guide to problems new Kotlin developers get stuck with. Along with that, Kotlin Cookbook will also help developers learn handy tricks and concepts that they may need while coding. This book will also help developers uncover the full potential of an amazing programming language, that is, Kotlin.
The book starts with an overview of Kotlin and moves on to some great simple concepts and features that Kotlin offers. From there, it will move on to OOP fundamentals and creating simple Android applications. Next will be the recipes for more complicated concepts such as networking, database, architectures, file io, and testing. It will also cover some great features of Anko that really eases out some complicated concepts in Android development, making it faster and more fun. Last will be some miscellaneous but extremely useful recipes that developers might need from time to time.
Who this book is for
This book is targeted at Kotlin beginners who know Android and Java Development and who have a good knowledge level and understanding of the Android development cycle. The readers are familiar with the concepts of Android development and understand the needs of testing their code. They want to learn efficient Kotlin techniques in order to make the existing Android development process more efficient and fun. This