Book Image

Android Development with Kotlin

By : Igor Wojda, Marcin Moskala
Book Image

Android Development with Kotlin

By: Igor Wojda, Marcin Moskala

Overview of this book

Nowadays, improved application development does not just mean building better performing applications. It has become crucial to find improved ways of writing code. Kotlin is a language that helps developers build amazing Android applications easily and effectively. This book discusses Kotlin features in context of Android development. It demonstrates how common examples that are typical for Android development, can be simplified using Kotlin. It also shows all the benefits, improvements and new possibilities provided by this language. The book is divided in three modules that show the power of Kotlin and teach you how to use it properly. Each module present features in different levels of advancement. The first module covers Kotlin basics. This module will lay a firm foundation for the rest of the chapters so you are able to read and understand most of the Kotlin code. The next module dives deeper into the building blocks of Kotlin, such as functions, classes, and function types. You will learn how Kotlin brings many improvements to the table by improving common Java concepts and decreasing code verbosity. The last module presents features that are not present in Java. You will learn how certain tasks can be achieved in simpler ways thanks to Kotlin. Through the book, you will learn how to use Kotlin for Android development. You will get to know and understand most important Kotlin features, and how they can be used. You will be ready to start your own adventure with Android development with Kotlin.
Table of Contents (16 chapters)
Title Page
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface
9
Making Your Marvel Gallery Application

Preface

Nowadays, the Android application development process is quite extensive. Over the last few years, we have seen how various tools have evolved to make our lives easier. However, one core element of the Android application development process hasn't changed much over time, Java. The Android platform adapts to newer versions of Java, but to be able to use them, we need to wait for a very long time until new Android devices reach proper market propagation. Also, developing applications in Java comes with its own set of challenges since Java is an old language with many design issues that can't be simply resolved due to backward compatibility constraints.

Kotlin, on the other hand, is a new but stable language that can run on all Android devices and solve many issues that Java cannot. It brings lots of proven programming concepts to the Android development table. It is a great language that makes a developer's life much easier and allows them to produce more secure, expressive, and concise code.

This book is an easy-to-follow, practical guide that will help you to speed up and improve the Android development process using Kotlin. We will present many shortcuts and improvements over Java and new ways of solving common problems. By the end of this book, you will be familiar with Kotlin features and tools, and you will be able to develop an Android application entirely in Kotlin.

What this book covers

Chapter 1, Beginning Your Kotlin Adventure, discusses the Kotlin language, its features, and reasons to use it. We'll introduce the reader to the Kotlin platform and show how Kotlin fits into the  Android development process.

Chapter 2, Laying a Foundation, is largely devoted to the building blocks of Kotlin. It presents various constructs, data types, and features that make Kotlin an enjoyable language to work with.

Chapter 3, Playing with Functions, explains various ways to define and call a function. We will also discuss function modifiers and look at possible locations where function can be defined.

Chapter 4, Classes and Objects, discusses Kotlin features related to object-oriented programming. You will learn about different types of class. We will also see features that improve readability: property operator overloading and infix calls.

Chapter 5, Functions as First-Class Citizens, covers Kotlin support for functional programming and functions as first-class citizens. We will take a closer look at lambdas, higher order functions, and function types.

Chapter 6, Generics Are Your Friends, explores the subjects of generic classes, interfaces, and functions. We will take a closer look at the Kotlin generic type system.

Chapter 7, Extension Functions and Properties, demonstrates how to add new behavior to an existing class without using inheritance. We will also discuss simpler ways to deal with collections and stream processing.

Chapter 8, Delegates, shows how Kotlin simplifies class delegation due to built-in language support. We will see how to use it both by using built-in property delegates and by defining custom ones.

Chapter 9, Making Your Marvel Gallery Application, utilizes most of the features discussed in the book and uses them to build a fully functional Android application in Kotlin.

What you need for this book

To test and use the code presented in this book, you need only Android Studio installed. Chapter 1, Beginning Your Kotlin Adventure, explains how a new project can be started and how the examples presented here can be checked. It also describes how most of the code presented here can be tested without any programs installed.

Who this book is for

To use this book, you should be familiar with two areas:

  • You need to know Java and object-oriented programming concepts, including objects, classes, constructors, interfaces, methods, getters, setters, and generic types. So if this area does not ring a bell, it will be difficult to fully understand the rest of this book. Start instead with an introductory Java book and return to this book afterward.
  • Though not mandatory, understanding the Android platform is desirable because it will help you to understand the presented examples in more detail, and you'll have a deeper understanding of the problems that are solved by Kotlin. If you are an Android developer with 6-12 months of experience, or you have created few Android applications, you'll be fine. On the other hand, if you feel comfortable with OOP concepts but your knowledge of the Android platform is limited, you will probably still be OK for most of the book.

Being open-minded and eager to learn new technologies will be very helpful. If something makes you curious or catches your attention, feel free to test it and play with it while you are reading this book

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Let's look at the range data type, which allows us to define end-inclusive ranges."

A block of code is set as follows:

    val capitol = "England" to "London"
    println(capitol.first) // Prints: England
    println(capitol.second) // Prints: London

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    ext.kotlin_version = '1.1.3'
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
    }

Any command-line input or output is written as follows:

sdk install kotlin

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Set name, package, and location for the new project. Remember to tick the Include Kotlin support option."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you. You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

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 GitHub at https://github.com/PacktPublishing/Android-Development-with-Kotlin. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

 

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.