Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Android Development with Kotlin
Android Development with Kotlin

Android Development with Kotlin: Enhance your skills for Android development using Kotlin

Arrow left icon
Profile Icon Marcin Moskala Profile Icon Igor Wojda
Arrow right icon
€36.99
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.6 (5 Ratings)
Paperback Aug 2017 440 pages 1st Edition
eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Marcin Moskala Profile Icon Igor Wojda
Arrow right icon
€36.99
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.6 (5 Ratings)
Paperback Aug 2017 440 pages 1st Edition
eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Android Development with Kotlin

Beginning Your Kotlin Adventure

Kotlin is great language that makes Android development easier, faster, and much more pleasant. In this chapter, we will discuss what Kotlin really is and look at many Kotlin examples that will help us build even better Android applications. Welcome to the amazing journey of Kotlin, that will change the way you think about writing code and solving common programming problems.

In this chapter, we will cover the following topics:

  • First steps with Kotlin
  • Practical Kotlin examples
  • Creating new Kotlin project in Android Studio
  • Migrating existing Java project to Kotlin
  • The Kotlin standard library (stdlib)
  • Why Kotlin is a good choice to learn

Say hello to Kotlin

Kotlin is a modern, statically typed, Android-compatible language that fixes many Java problems, such as null pointer exceptions or excessive code verbosity. Kotlin is a language inspired by Swift, Scala, Groovy, C#, and many other languages. Kotlin was designed by JetBrains professionals, based on analysis of both developers experiences, best usage guidelines (most important are clean code and effective Java), and data about this language's usage. Deep analysis of other programming languages has been done. Kotlin tries hard to not repeat the mistakes from other languages and take advantage of their most useful features. When working with Kotlin, we can really feel that this is a mature and well-designed language.

Kotlin takes application development to a whole new level by improving code quality and safety and boosting developer performance. Official Kotlin support for the Android platform was announced by Google in 2017, but the Kotlin language has been here for some time. It has a very active community and Kotlin adoption on the Android platform is already growing quickly. We can describe Kotlin as a safe, expressive, concise, versatile, and tool-friendly language that has great interoperability with Java and JavaScript. Let's discuss these features:

  • Safety: Kotlin offers safety features in terms of nullability and immutability. Kotlin is statically typed, so the type of every expression is known at compile time. The compiler can verify that whatever property or method we are trying to access of a particular class instance actually exists. This should be familiar from Java which is also statically typed, but unlike Java, Kotlin type system is much more strict (safe). We have to explicitly tell the compiler whether the given variable can store null values. This allows making the program fail at compile time instead of throwing a NullPointerException at runtime:

  • Easy debugging: Bugs can be detected much faster during the development phase, instead of crashing the application after it is released and thus damaging the user experience. Kotlin offers a convenient way to work with immutable data. For example, it can distinguish mutable (read-write) and immutable (read-only) collections by providing convenient interfaces (under the hood collections are still mutable).
  • Conciseness: Most of the Java verbosity was eliminated. We need less code to achieve common tasks and thus the amount of boilerplate code is greatly reduced, even comparing Kotlin to Java 8. As a result, the code is also easier to read and understand (expressive).
  • Interoperability: Kotlin is designed to seamlessly work side by side with Java (cross-language project). The existing ecosystem of Java libraries and frameworks works with Kotlin without any performance penalties. Many Java libraries even Kotlin-specific versions that allow more idiomatic usage with Kotlin. Kotlin classes can also be directly instantiated and transparently referenced from Java code without any special semantics and vice versa. This allows us to incorporate Kotlin into existing Android projects and use Kotlin easily together with Java (if we want to).
  • Versatility: We can target many platforms, including mobile applications (Android), server-side applications (backend), desktop applications, frontend code running in the browser, and even build systems (Gradle).

Any programming language is only as good as its tool support. Kotlin has outstanding support for modern IDEs such as Android Studio, IntelliJ Idea, and Eclipse. Common tasks such as code assistance or refactoring are handled properly. The Kotlin team works hard to make the Kotlin plugin better with every single release. Most of the bugs are quickly fixed and many of the features requested by the community are implemented.

Android application development becomes much more efficient and pleasant with Kotlin. Kotlin is compatible with JDK 6, so applications created in Kotlin run safely even on old Android devices that precede Android 4.

Kotlin aims to bring you the best of both worlds by combining concepts and elements from both procedural and functional programming. It follows many guidelines as described in the book, Effective Java, 2nd Edition, by Joshua Bloch, which is considered a must-read book for every Java developer.

On top of that, Kotlin is open sourced, so we can check out the project and be actively involved in any aspect of the Kotlin project such as Kotlin plugins, compilers, documentation, or the Kotlin language itself.

Awesome Kotlin examples

Kotlin is really easy to learn for Android developers because the syntax is similar to Java and Kotlin often feels like a natural Java evolution. At the beginning, a developer usually writes Kotlin code by having in mind habits from Java, but after a while, it is very easy to move to more idiomatic Kotlin solutions. Let's look at some cool Kotlin features and see where Kotlin may provide benefits by solving common programming tasks in an easier, more concise, and more flexible way. We have tried to keep examples simple and self-explanatory, but they utilize content from various parts of this book, so it's fine if they are not fully understood at this point. The goal of this section is to focus on the possibilities and present what can be achieved by using Kotlin. This section does not necessarily need to fully describe how to achieve it. Let's start with a variable declaration:

    var name = "Igor" // Inferred type is String 
    name = "Marcin" 

Notice that Kotlin does not require semicolons. You can still use them, but they are optional. We also don't need to specify a variable type because it's inferred from the context. Each time, the compiler can figure out the type from the context; we don't have to explicitly specify it. Kotlin is a strongly typed language, so each variable has an adequate type:

    var name = "Igor" 
    name = 2 // Error, because name type is String 

The variable has an inferred String type, so assigning a different value (integer) will result in a compilation error. Now, let's see how Kotlin improves the way to add multiple strings using string templates:

    val name = "Marcin" 
    println("My name is $name") // Prints: My name is Marcin 

We need no more joining strings using the + character. In Kotlin, we can easily incorporate single variable or even whole expressions, into string literals:

    val name = "Igor" 
        println("My name is ${name.toUpperCase()}") 
// Prints: My name is IGOR

In Java, any variable can store null values. In Kotlin, strict null safety forces us to explicitly mark each variables, that can store nullable values:

    var a: String = "abc"
a = null // compilation error

var b: String? = "abc"
b = null // It is correct

By adding a question mark to a data type (string versus string?), we say that the variable can be nullable (can store null references). If we don't mark the variable as nullable, we will not be able to assign a nullable reference to it. Kotlin also allows us to deal with nullable variables in proper ways. We can use the safe call operator to safely call methods on potentially nullable variables:

    savedInstanceState?.doSomething 

The method doSomething will be invoked only if savedInstanceState has a non-null value, otherwise the method call will be ignored. This is Kotlin's safe way to avoid null pointer exceptions that are so common in Java.

Kotlin also has several new data types. Let's look at the Range data type that allows us to define end inclusive ranges:

    for (i in 1..10) { 
        print(i) 
    } // 12345678910 

Kotlin introduces the Pair data type that, combined with infix notation, allows us to hold a common pair of values:

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

We can deconstruct it into separate variables using destructive declarations:

    val (country, city) = capitol 
    println(country) // Prints: England 
    println(city) // Prints: London 

We can even iterate through a list of pairs:

    val capitols = listOf("England" to "London", "Poland" to "Warsaw") 
    for ((country, city) in capitols) { 
        println("Capitol of $country is $city") 
    } 
 
    // Prints: 
    // Capitol of England is London 
    // Capitol of Poland is Warsaw 

Alternatively, we can use the forEach function:

    val capitols = listOf("England" to "London", "Poland" to "Warsaw") 
    capitols.forEach { (country, city) -> 
        println("Capitol of $country is $city") 
    } 

Note that Kotlin distinguishes between mutable and immutable collections by providing a set of interfaces and helper methods (List versus MutableList, Set versus Set versus MutableSet, Map versus MutableMap, and so on):

    val list = listOf(1, 2, 3, 4, 5, 6) // Inferred type is List 
    val mutableList = mutableListOf(1, 2, 3, 4, 5, 6) 
// Inferred type is MutableList

Immutable collection means that the collection state can't change after initialization (we can't add/remove items). Mutable collection (quite obviously) means that the state can change.

With lambda expressions, we can use the Android framework build in a very concise way:

    view.setOnClickListener { 
        println("Click") 
    } 

The Kotlin standard library (stdlib) contains many functions that allow us to perform operations on collections in a simple and concise way. We can easily perform stream processing on lists:

    val text = capitols.map { (country, _) -> country.toUpperCase() } 
                       .onEach { println(it) } 
                       .filter { it.startsWith("P") } 
                       .joinToString (prefix = "Countries prefix P:")
    // Prints: ENGLAND POLAND
println(text) // Prints: Countries prefix P: POLAND
.joinToString (prefix = "Countries prefix P:")

Notice that we don't have to pass parameters to a lambda. We can also define our own lambdas that will allow us to write code in a completely new way. This lambda will allow us to run a particular piece of code only in Android Marshmallow or newer:

    inline fun supportsMarshmallow(code: () -> Unit) { 
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
        code() 
    } 
 
    //usage 
    supportsMarshmallow { 
        println("This code will only run on Android Nougat and newer") 
    } 

We can make asynchronous requests easily and display responses on the main thread using the doAsync function:

    doAsync { 
        var result = runLongTask()  // runs on background thread 
 
        uiThread { 
            toast(result)           // run on main thread 
        } 
    } 

Smart casts allow us to write code without performing redundant casting:

    if (x is String) { 
        print(x.length) // x is automatically casted to String 
    } 
 
    x.length //error, x is not casted to a String outside if block 
 
    if (x !is String) 
        return 
 
    x.length // x is automatically casted to String 

The Kotlin compiler knows that the variable x is of the type String after performing a check, so it will automatically cast it to the String type, allowing it to call all methods and access all properties of the String class without any explicit casts.

Sometimes, we have a simple function that returns the value of a single expression. In this case, we can use a function with an expression body to shorten the syntax:

    fun sum(a: Int, b: Int) = a + b 
    println (sum(2 + 4)) // Prints: 6 

Using default argument syntax, we can define the default value for each function argument and call it in various ways:

    fun printMessage(product: String, amount: Int = 0, 
name: String = "Anonymous") { println("$name has $amount $product") } printMessage("oranges") // Prints: Anonymous has 0 oranges printMessage("oranges", 10) // Prints: Anonymous has 10 oranges printMessage("oranges", 10, "Johny")
// Prints: Johny has 10 oranges

The only limitation is that we need to supply all arguments without default values. We can also use named argument syntax to specify function arguments:

    printMessage("oranges", name = "Bill") 

This also increases readability when invoking the function with multiple parameters in the function call.

The data classes give a very easy way to define and operate on classes from the data model. To define a proper data class, we will use the data modifier before the class name:

    data class Ball(var size:Int, val color:String) 
 
    val ball = Ball(12, "Red") 
    println(ball) // Prints: Ball(size=12, color=Red) 

Notice that we have a really nice, human readable string representation of the class instance and we do not need the new keyword to instantiate the class. We can also easily create a custom copy of the class:

    val ball = Ball(12, "Red") 
    println(ball) // prints: Ball(size=12, color=Red) 
    val smallBall = ball.copy(size = 3) 
    println(smallBall) // prints: Ball(size=3, color=Red) 
    smallBall.size++ 
    println(smallBall) // prints: Ball(size=4, color=Red) 
    println(ball) // prints: Ball(size=12, color=Red) 

The preceding constructs make working with immutable objects very easy and convenient.

One of the best features in Kotlin are extensions. They allow us to add new behavior (a method or property) to an existing class without changing its implementation. Sometimes when you work with a library or framework, you would like to have an extra method or property for a certain class. Extensions are a great way to add those missing members. Extensions reduce code verbosity and remove the need to use utility functions known from Java (for example, the StringUtils class). We can easily define extensions for custom classes, third-party libraries, or even Android framework classes. First of all, ImageView does not have the ability to load images from a network, so we can add the loadImage extension method to load images using the Picasso library (an image loading library for Android):

    fun ImageView.loadUrl(url: String) { 
        Picasso.with(context).load(url).into(this) 
    } 
 
    //usage 
    imageView.loadUrl("www.test.com\\image1.png") 

We can also add a simple method displaying toasts to the Activity class:

    fun Context.toast(text:String) { 
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show() 
    } 
 
    //usage (inside Activity class)
toast("Hello")

There are many places where usage of extensions will make our code simpler and more concise. Using Kotlin, we can fully take advantage of lambdas to simplify Kotlin code even more.

Interfaces in Kotlin can have default implementations as long as they don't hold any state:

    interface BasicData { 
        val email:String 
        val name:String 
        get() = email.substringBefore("@") 
    } 

In Android, there are many applications where we want to delay object initialization until it is needed (used). To solve this problem, we can use delegates:

    val retrofit by lazy { 
        Retrofit.Builder() 
            .baseUrl("https://www.github.com") 
            .addConverterFactory(MoshiConverterFactory.create()) 
            .build() 
    } 

Retrofit (a popular Android networking framework) property initialization will be delayed until the value is accessed for the first time. Lazy initialization may result in faster Android application startup times, since loading is deferred to when the variable is accessed. This is a great way to initialize multiple objects inside a class, especially when not all of them are always needed (for certain class usage scenarios, we may need only specific objects) or when not every one of them is needed instantly after class creation.

All the presented examples are only a glimpse of what can be accomplished with Kotlin. We will learn how to utilize the power of Kotlin throughout this book.

Dealing with Kotlin code

There are multiple ways of managing and running Kotlin code. We will mainly focus on Android Studio and Kotlin Playground.

Kotlin Playground

The fastest way to try Kotlin code without the need to install any software is Kotlin Playground (https://try.kotlinlang.org). We can run Kotlin code there using JavaScript or JVM Kotlin implementations and easily switch between different Kotlin versions. All the code examples from the book that does not require Android framework dependencies and can be executed using Kotlin Playground.

The main function is the entry point of every Kotlin application. This function is called when any application starts, so we must place code from the book examples in the body of this method. We can place code directly or just place a call to another function containing more Kotlin code:

    fun main(args: Array<String>) { 
        println("Hello, world!") 
    }
Android applications have multiple entry points. The main function is called implicitly by the Android framework, so we can't use it to run Kotlin code on the Android platform.

Android Studio

All Android Studio's existing tools work with Kotlin code. We can easily use debugging, lint checks, have proper code assistance, refactoring, and more. Most of the things work the same way as for Java, so the biggest noticeable change is the Kotlin language syntax. All we need to do is to configure Kotlin in the project.

Android applications have multiple entry points (different intents can start different components in the application) and require Android framework dependencies. To run the book examples, we need to extend the Activity class and place code there.

Configuring Kotlin for the project

Starting from Android Studio 3.0, full tooling support for Kotlin was added. Installation of the Kotlin plugin is not required and Kotlin is integrated even deeper into the Android development process.

To use Kotlin with Android Studio 2.x, we must manually install the Kotlin plugin. To install it, we need to go to Android Studio | File | Settings | Plugins | Install JetBrains plugin... | Kotlin and press the Install button:

To be able to use Kotlin, we need to configure Kotlin in our project. For existing Java projects, we need to run the Configure Kotlin in project action (the shortcut in Windows is Ctrl+Shift+A, and in macOS, it is command + shift + A) or use the corresponding Tools | Kotlin | Configure Kotlin in Project menu item:

Then, select Android with Gradle:

Finally, we need to select the required modules and the proper Kotlin version:

The preceding configuration scenario also applies to all existing Android projects that were initially created in Java. Starting from Android Studio 3.0, we can also check the Include Kotlin support checkbox while creating a new project:

In both scenarios, the Configure Kotlin in project command updates the root build.gradle file and the build.gradle files corresponding to the module(s) by adding Kotlin dependencies. It also adds the Kotlin plugin to the Android module. At the time of writing this book release version of Android Studio 3 is not yet available, but we can review the build script from the pre-release version:

//build.gradle file in project root folder 
buildscript { 
    ext.kotlin_version = '1.1' 
 
    repositories { 
        google() 
        jcenter() 
    } 
    dependencies { 
       classpath 'com.android.tools.build:gradle:3.0.0-alpha9' 
       classpath "org.jetbrains.kotlin:kotlin-gradle-
plugin:$kotlin_version" } }
... //build.gradle file in the selected modules apply plugin: 'com.android.application' apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
... dependencies { ...
implementation 'com.android.support.constraint:constraint-
layout:1.0.2'
} ...
Prior to the Android plugin for Gradle 3.x (delivered with Android Studio 3.0), compile dependency configuration was used instead of implementation.

To update the Kotlin version (let us say in the future), we need to change the value of the kotlin_version variable in the build.gradle file (project root folder). Changes in Gradle files mean that the project must be synchronized, so Gradle can update its configuration and download all the required dependencies:

Using Kotlin in a new Android project

For new Kotlin projects created in Android Studio 3.x, the main activity will be already defined in Kotlin, so that we can start writing Kotlin code right away:

Adding a new Kotlin file is similar to adding a Java file. Simply right-click on a package and select new | Kotlin File/Class:

The reason why the IDE says Kotlin File/Class and not simply Kotlin class, which is analogous to Java class, is that we can have more members defined inside a single file. We will discuss this in more detail in Chapter 2, Laying a Foundation.

Notice that Kotlin source files can be located inside the java source folder. We can create a new source folder for Kotlin, but it is not required:

Running and debugging a project is exactly the same as in Java and does not require any additional steps besides configuring Kotlin in the project:

Starting from Android Studio 3.0, various Android templates will also allow us to select a language. This is the new Configure Activity wizard:

Java to Kotlin converter (J2K)

Migration of existing Java projects is also quite easy, because we can use Java and Kotlin side by side in the same project. There are also ways to convert existing Java code into Kotlin code by using the Java to Kotlin converter (J2K).

The first way is to convert whole Java files into Kotlin files using the convert Java File to Kotlin command (the keyboard shortcut in Windows is Alt + Shift + Ctrl + K and in macOS is option + shift + command + K), and this works very well. The second way is to paste Java code into an existing Kotlin file and the code will also be converted (a dialog window will appear with a conversion proposition). This may be very helpful when learning Kotlin.

If we don't know how to write a particular piece of code in Kotlin, we can write it in Java, then simply copy to the clipboard and paste it into the Kotlin file. Converted code will not be the most idiomatic version of Kotlin, but it will work. The IDE will display various intentions to convert the code even more and improve its quality. Before conversion, we need to make sure that the Java code is valid, because conversion tools are very sensitive and the process will fail even if a single semicolon is missing. The J2K converter combined with Java interoperability allows Kotlin be introduced gradually into the existing project (for example, to convert a single class at a time).

Alternative ways to run Kotlin code

Android Studio offers an alternative way of running Kotlin code without the need to run the Android application. This is useful when you want to quickly test some Kotlin code separately from the long Android compilation and deployment process.

The way to run Kotlin code is to use the Kotlin Read Eval Print Loop (REPL). REPL is a simple language shell that reads single user input, evaluates it, and prints the result:

REPL looks like the command-line, but it will provide us with all the required code hints and will give us access to various structures defined inside the project (classes, interfaces, top-level functions, and so on):

The biggest advantage of REPL is its speed. We can test Kotlin code really quickly.

Kotlin under the hood

We will focus mainly on Android, but keep in mind that Kotlin can be compiled to multiple platforms. Kotlin code can be compiled to Java bytecode and then to Dalvik bytecode. Here is the simplified version of the Kotlin build process for the Android platform:

  • A file with a .java extension contains Java code
  • A file with a .kt extension contains Kotlin code
  • A file with a .class extension contains Java bytecode
  • A file with a .dex extension contains Dalvik bytecode
  • A file with a .apk extension contains the AndroidManifest file, resources, and .dex file

For pure Kotlin projects, only the Kotlin compiler will be used, but Kotlin also supports cross-language projects, where we can use Kotlin together with Java in the same Android project. In such cases, both compilers are used to compile the Android application and the result will be merged at the class level.

The Kotlin standard library

The Kotlin standard library (stdlib) is a very small library that is distributed together with Kotlin. It is required to run applications written in Kotlin and it is added automatically to our application during the build process.

In Kotlin 1.1, kotlin-runtime was required to run applications written in Kotlin. In fact, in Kotlin 1.1 there were two artifacts (kotlin-runtime and kotlin-stdlib) that shared a lot of Kotlin packages. To reduce the amount of confusion, both the artifacts will be merged into a single artifact (kotlin-stdlib) in the upcoming 1.2 version of Kotlin. Starting from Kotlin 1.2, kotlin-stdlib is required to run applications written in Kotlin.

The Kotlin standard library provides essential elements required for everyday work with Kotlin. These include:

  • Data types such as arrays, collections, lists, ranges, and so on
  • Extensions
  • Higher-order functions
  • Various utilities for working with strings and char sequences
  • Extensions for JDK classes, making it convenient to work with files, IO, and threading

More reasons to use Kotlin

Kotlin has strong commercial support from JetBrains, a company that delivers very popular IDEs for many popular programming languages (Android Studio is based on JetBrains IntelliJ IDEA). JetBrains wanted to improve the quality of their code and team performance, so they needed a language that would solve all the Java issues and provide seamless interoperability with Java. None of the other JVM languages meet those requirements, so JetBrains finally decided to create their own language and started the Kotlin project. Nowadays, Kotlin is used in their flagship products. Some use Kotlin together with Java while others are pure Kotlin products.

Kotlin is quite a mature language. In fact, its development started many years before Google announced official Android support (the first commit dates back to 2010-11-08):

The initial name of the language was Jet. At some point, the JetBrains team decided to rename it to Kotlin. The name comes from Kotlin Island near St. Petersburg and is analogous to Java, which was named after an Indonesian island.

After the version 1.0 release in 2016, more and more companies started to support the Kotlin project. Gradle added support for Kotlin into building scripts; Square, the biggest creator of Android libraries posted that they strongly support Kotlin; and finally, Google announced official Kotlin support for the Android platform. This means that every tool that will be released by the Android team will be compatible not only with Java but also with Kotlin. Google and JetBrains have begun a partnership to create a nonprofit foundation for Kotlin, responsible for future language maintenance and development. All of this will greatly increase the number of companies that use Kotlin in their projects.

Kotlin is also similar to Apple's Swift programming language. In fact, such is the resemblance that some articles focus on differences, not similarities. Learning Kotlin will be very helpful for developers eager to develop applications for Android and iOS. There are also plans to port Kotlin to iOS (Kotlin/Native), so maybe we don't have to learn Swift after all. Full stack development is also possible in Kotlin, so we can develop server-side applications and frontend clients that share the same data model as mobile clients.

Summary

We've discussed how the Kotlin language fits into Android development and how we can incorporate Kotlin into new and existing projects. We have seen useful examples where Kotlin simplified the code and made it much safer. There are still many interesting things to discover.

In the next chapter, we will learn about Kotlin building blocks and lay a foundation to develop Android applications using Kotlin.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • • Leverage specific features of Kotlin to ease Android application development
  • • Write code based on both object oriented and functional programming to build robust applications
  • • Filled with various practical examples so you can easily apply your knowledge to real world scenarios
  • • Identify the improved way of dealing with common Java patterns

Description

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.

Who is this book for?

This book is for developers who have a basic understanding of Java language and have 6-12 months of experience with Android development and developers who feel comfortable with OOP concepts.

What you will learn

  • ? Run a Kotlin application and understand the integration with Android Studio
  • ? Incorporate Kotlin into new/existing Android Java based project
  • ? Learn about Kotlin type system to deal with null safety and immutability
  • ? Define various types of classes and deal with properties
  • ? Define collections and transform them in functional way
  • ? Define extensions, new behaviours to existing libraries and Android framework classes
  • ? Use generic type variance modifiers to define subtyping relationship between generic types
  • ? Build a sample application
Estimated delivery fee Deliver to Portugal

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 30, 2017
Length: 440 pages
Edition : 1st
Language : English
ISBN-13 : 9781787123687
Category :
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Portugal

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Publication date : Aug 30, 2017
Length: 440 pages
Edition : 1st
Language : English
ISBN-13 : 9781787123687
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 110.97
Programming Kotlin
€36.99
Mastering Android Development with Kotlin
€36.99
Android Development with Kotlin
€36.99
Total 110.97 Stars icon
Banner background image

Table of Contents

9 Chapters
Beginning Your Kotlin Adventure Chevron down icon Chevron up icon
Laying a Foundation Chevron down icon Chevron up icon
Playing with Functions Chevron down icon Chevron up icon
Classes and Objects Chevron down icon Chevron up icon
Functions as First-Class Citizens Chevron down icon Chevron up icon
Generics Are Your Friends Chevron down icon Chevron up icon
Extension Functions and Properties Chevron down icon Chevron up icon
Delegates Chevron down icon Chevron up icon
Making Your Marvel Gallery Application Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.6
(5 Ratings)
5 star 20%
4 star 0%
3 star 20%
2 star 40%
1 star 20%
Mikolaj Leszczynski Sep 21, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excellent book. This is for Android developers who would like to learn how to apply Kotlin paradigms to typical Android development patterns. Makes for a very practical approach to teaching Kotlin.
Amazon Verified review Amazon
Amazon Customer Oct 07, 2017
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Good explanation of Kotlin and advantages over Java. I expected more Android references given the book title so that aspect is a little underwhelming since I am primarily concerned with learning Kotlin for Android development. Though, the last chapter is dedicated to building a gallery app (which I've yet to reach).English is not great, and the grammatical errors don't help, but it is still readable overall. Definitely seems like a number of the issues should have been caught prior to publishing.Overall, so far, I am pleased with the content. My rating may change from a 3 star once I finish reading the entire book.
Amazon Verified review Amazon
Bob Mann Sep 30, 2017
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
I have made it halfway through this book and only because I worked through a much better book before I read this one.Android Studio Development Essentials by Neil Smyth is an example of a GOOD Android Studio Tutorial.Android Development with Kotlin by Marcin Moskala is an example of a BAD Android Studio Tutorial.When I see a code example " println(Text) " I know the author is not using Android Studio.When I see Github listing just code snippets instead of working examples I know the author is not using Android Studio.When I see Github code organized in Chapters and no Chapter numbers in the downloaded book I know the editor did not read all of the content.I can work around the broken english in this book and I really want to learn Kotlin on Android Studio, I guess the only way I will is when Neil Smyth writes a Kotlin version of his Android Studio Development Essentials.
Amazon Verified review Amazon
Ollie C Sep 25, 2017
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
The breadth of coverage and touching on Android are valuable and the authors clearly have a great deal of knowledge about Kotlin but the book is severely let down by typos, poor grammar, code sample errors (one defines a class called Snail but instantiates one called Sneil!), duplicated text, images that are unreadable on a Kindle and poor English which is distracting (it's instead of its, sentences missing definite/indefinite articles, missing words, capital letters in the middle of sentences, etc). It's not an exaggeration to say most pages have errors on them, making it incredibly difficult to read and very poor value for money. Even one of the book's chapter *titles* has a glaring typo in it ("View binging" instead of "View binding"!). It's clear this book was not even looked at by a technical book editor. Packt told me they are now editing the book, but why didn't they do this before publishing it? Come on! Although the book does contain some Android examples there are very few. Kotlin in Action is a significantly higher quality book, in which I saw not a single copy error. It also contains Android examples and so I heartily recommend that as an alternative.
Amazon Verified review Amazon
Xolix Sep 14, 2019
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
This is probably the worst book for anyone starting out with Android development and Kotlin. The book is essentially a long list of loosely related details that might come up when working in Kotlin. Maybe there could have been a place for it as reference, but then of course it is already outdated. The one and only project - if one wants to call it that - starts on page 338 and assumes that one has read through the encyclopedic style of some hundred mini-topics. At that pint one then gets to type in reams of xml as the books doesn’t get into layout or other Android Studio related issues and any explanations from this point on are extremely brief or non-existing. Just thoughtless and horrible.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact [email protected] with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at [email protected] using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on [email protected] with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on [email protected] within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on [email protected] who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on [email protected] within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela