All Rights Reserved. No part of this publication may be reproduced or distributed in any form or by any means or stored in a database or retrieval system, without the prior written permission of the publisher with the exception to the program listings which may be entered, stored and executed in a computer system, but they can not be reproduced by the means of publication.
The information contained in this book is true to correct and the best of authors & publishers knowledge. The author has made every effort to ensure the accuracy of these publications, but cannot be held responsible for any loss or damage arising from any information in this book.
All trademarks referred to in the book are acknowledged as properties of their respective owners.
Shop No. 5, Mahendra Chambers,
150 DN Rd. Next to Capital Cinema,
V.T. (C.S.T.) Station, MUMBAI-400 001
Published by Manish Jain for BPB Publications, 20 Ansari Road, Darya Ganj, New Delhi-110002 and Printed by him at Repro India Ltd, Mumbai
Dedicated to
All the fervent learners
who flourish to explore and learn for knowledge building
About the Author
Hardik Trivedi is a computer programmer and a self-taught Android application development. He started Android application development back in 2010. He is a Kotlin enthusiast and an active community speaker. He actively contributes to Stack overflow and also writes a blog. An Android application developer on his job, he has worked in numerous domains, for example, consumer internet, sports, banking, entertainment. He also mentors college students and professionals who want to develop their career in Android application development. As an active contributor in the community, he has delivered speeches for events hosted by GDG (Google Developer Group). He is already a co-author of Kotlin Blueprints A book that explains how Kotlin can be used everywhere.
In his personal life, he loves to travel, paint, and cook. In his retirement days, you may find him owning a restaurant and making lip-smacking food for his customers.
Acknowledgement
All innovation can not be done without unpredicted hurdles. To overcome this and continue with the research along with the knowledge, you must need the support of supreme powers. I thank the supreme power of God, who kept me motivated enough to work on this book in my spare time.
My family kept me courageous enough for months. I was stealing time from my family and used to write books on those hours. I acknowledge their sacrifice. My sincere thanks to them.
I would also like to take the opportunity to thank many other contributors in the community whose research helped me to extend my research work. A salute to all those developers.
Last but not least, I thank BPB publications, who were involved in this book. Without them, it would not have been possible.
Preface
This book aims to provide the knowledge around the fundamental concept of Kotlin languages, and its an application in Android application development. It covers basic to advanced concepts with practical examples. Each chapter in this book is a step by step journey towards the learning Kotlin and excel in various topics and concepts. It covers topics like data types, various functions, including lambdas and higher-order functions. It also covers advance topics like Generics, Collections, DSL, Coroutine, etc.
Most importantly, such concepts are explained with practical usage of it in Android application. You will get to know what is the best possible way to use these concepts while you develop an Android application. In this book, along with Kotlin, an attempt has been made where few Android-specific topics are also explained. For example, the application is using Architecture components, including ViewModel, LiveData, NavigationComponent, and also it uses Flow, which is a hot topic in Kotlin.
Readers with little previous knowledge of Android application development can easily follow this book. Most of the chapters are code-heavy and focuses on practical usage of Kotlins features. Each chapter has code on the GitHub. You can checkout this code and try it out. Or you can develop in parallel and cherry-pick things from the sample code base as and when you need it. Few chapters also follow the quiz at the end, and you can self assess yourself by going through that quiz. In total ten chapters, you will learn the following:
focuses on setting up the development environment. You configure the tools and environment which you will need to learn and develop Android application. Also, you learn some very basics of Android, e.g., Android Components and how to work with those components. You will become familiar with Kotlin code style in this chapter. In the end, a simple HelloWorld Android application in Kotlin will be developed.
takes on the voyage of learning Kotlin. You learn basics in this chapter. This chapter covers basics about data type and variables, function, string manipulation, control flow. It covers various types of functions including, inline and infix function. Everything is well explained by small yet practical examples. After functions, the chapter covers the concept of classes and properties. What differences Kotlin class as compare to Java class is well explained. It also has lots of tips and notes around IntelliJ IDEA. Error handling is also an attractive section in this chapter, which demonstrates how try-catch works in Kotlin. Scoping functions are very generously written and aim to give insight into it. And the most exciting part of this chapter is a quiz section. A total of 10 questions is designed, which will give a quick recap of the entire chapter in the end.
is all about diving deep into the advance topics of Kotlin. The type of system delegated properties, lambdas, collections, and generics. You think of a topic, and its covered one or the other way in this chapter. It also covers DSL and annotations. This chapter is once again full of code snippets, tips, diagrams. In collections, only the topics which are specific to Kotlin are covered. The basics of the collection are the same as Java, so we have focused on the delta part. Generics also explain covariance and contravariances topic. And one more time total of 10 questions are designed, which will give the quick recap of the entire chapter in the end.