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 is not an introductory book for Kotlin, and it assumes basic familiarity with Kotlin. This book aims at helping developers solve issues they are stuck with while working with Kotlin.
What this book covers
Chapter 1, Installation and Working with Environment, walks you through starting with a Kotlin project. We will also introduce you to the Gradle build system and help you in setting up your development environment.
Chapter 2, Control Flow, includes recipes for control flow in Kotlin. Kotlin has brought a lot of power to old control flows, as you can now use them as an expression. Kotlin has also introduced a powerful "when", which is basically Java’s "switch" improvements.
Chapter 3, Classes and Objects, says that classes and objects are inevitable parts of object-oriented programming. This chapter will include the solutions and examples of real-world problems faced by developers and how Kotlin solves it. This chapter will also lay the foundation to the upcoming chapter OOPS programming with Kotlin.
Chapter 4, Functions, informs that functions are inevitable parts of object-oriented programming. This chapter will include the solutions and examples of real-world problems faced by developers and how Kotlin solves it.
Chapter 5, Object-Oriented Programming, builds upon the learning of Chapter 3, Classes and Objects, and will include recipes that help in OOP.
Chapter 6, Collections Framework, presents the recipes that will explore the full potential of Collection framework in Kotlin.
Chapter 7, Handling File Operations in Kotlin, covers recipes about basic I/O and File I/O.
Chapter 8, Anko Commons and Extension Function, contains recipes on how to use the Anko library of Kotlin for efficient and quick Android development.
Chapter 9, Anko Layouts, has recipes on how to use the Anko library of Kotlin for efficient and quick Android development.
Chapter 10, Databases and Dependency Injection, dives into recipes to work with SQLite databases in Android.
Chapter 11, Networking and Concurrency, discusses recipes that will help developers make network calls and fetch data over a network.
Chapter 12, Lambdas and Delegates, uncovers some of the best (and difficult) features of Kotlin, that is, Lambdas and Delegates. This contains recipes to help the developers get started with them.
Chapter 13, Testing, outlines concepts on writing tests in Kotlin while touching Unit tests, integration tests, instrumentation, and acceptance tests.
Chapter 14, Web Services with Kotlin, helps developers write web services using Kotlin language.
To get the most out of this book
This book assumes familiarity with Java and Android development. This is not an introductory book for learning Kotlin. Readers must have used Android studio because many of the recipes will be focused toward Android development.
Download the example code files
You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
- Log in or register at www.packtpub.com.
- Select the
SUPPORT
tab. - Click on
Code Downloads & Errata
. - Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
- WinRAR/7-Zip for Windows
- Zipeg/iZip/UnRarX for Mac
- 7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitLab at https://gitlab.com/users/aanandshekharroy/projects. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Conventions used
There are a number of text conventions used throughout this book.
CodeInText
: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "The corresponding sourceSets
property should be updated if not using the default convention."
A block of code is set as follows:
sourceSets { main.kotlin.srcDirs += 'src/main/myKotlin' main.java.srcDirs += 'src/main/myJava' }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
sourceSets {
main.java.srcDirs += 'src/main/kotlin/'
}
Any command-line input or output is written as follows:
$ kotlinc hello.kt -include-runtime -d hello.jar. $ java -jar hello.jar
Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "In the Select Deployment Target window, select your device, and click on
"OK
.
Note
Warnings or important notes appear like this.
Note
Tips and tricks appear like this.
Sections
In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).
To give clear instructions on how to complete a recipe, use these sections as follows:
Getting ready
This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.
How to do it…
This section contains the steps required to follow the recipe.
How it works…
This section usually consists of a detailed explanation of what happened in the previous section.
There's more…
This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.
See also
This section provides helpful links to other useful information for the recipe.
Get in touch
Feedback from our readers is always welcome.
General feedback: Email [email protected]
and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected]
.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected]
with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Reviews
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packtpub.com.