Android Development with Kotlin

Book description

Learn how to make Android development much faster using a variety of Kotlin features, from basics to advanced, to write better quality code.

About This Book

  • 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

Who This Book Is 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

In Detail

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.

Publisher resources

Download Example Code

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Beginning Your Kotlin Adventure
    1. Say hello to Kotlin
    2. Awesome Kotlin examples
    3. Dealing with Kotlin code
      1. Kotlin Playground
      2. Android Studio
        1. Configuring Kotlin for the project
        2. Using Kotlin in a new Android project
        3. Java to Kotlin converter (J2K)
        4. Alternative ways to run Kotlin code
    4. Kotlin under the hood
      1. The Kotlin standard library
    5. More reasons to use Kotlin
    6. Summary
  3. Laying a Foundation
    1. Variables
    2. Type inference
    3. Strict null safety
      1. Safe call
      2. Elvis operator
      3. Not-null assertion
      4. The let function
    4. Nullability and Java
    5. Casts
      1. Safe/unsafe cast operator
      2. Smart casts
        1. Type smart casts
        2. Non-nullable smart cast
    6. Primitive data types
      1. Numbers
      2. Char
      3. Arrays
      4. The Boolean type
    7. Composite data types
      1. Strings
        1. String templates
      2. Ranges
      3. Collections
    8. Statements versus expressions
    9. Control flow
      1. The if statement
      2. The when expression
      3. Loops
        1. The for loop
        2. The while loop
        3. Other iterations
        4. Break and continue
    10. Exceptions
      1. The try... catch block
    11. Compile-time constants
    12. Delegates
    13. Summary
  4. Playing with Functions
    1. Basic function declaration and usage
      1. Parameters
      2. Returning functions
    2. Vararg parameter
    3. Single-expression functions
    4. Tail-recursive functions
    5. Different ways of calling a function
      1. Default argument values
      2. Named arguments syntax
    6. Top-level functions
    7. Top-level functions under the hood
    8. Local functions
    9. Nothing return type
    10. Summary
  5. Classes and Objects
    1. Classes
      1. Class declaration
    2. Properties
      1. Read-write versus read-only
      2. Property access syntax between Kotlin and Java
        1. Increment and decrement operators
      3. Custom getters/setters
        1. The getter versus property default value
      4. Late-initialized properties
      5. Annotating properties
      6. Inline properties
    3. Constructors
      1. Properties versus constructor parameters
      2. Constructors with default arguments
        1. Patterns
    4. Inheritance
      1. The JvmOverloads annotation
    5. Interfaces
    6. Data classes
      1. The equals and hashCode methods
      2. The toString method
      3. The copy method
      4. Destructing declarations
    7. Operator overloading
    8. Object declaration
    9. Object expression
    10. Companion objects
      1. Companion object instantiation
    11. Enum classes
    12. Infix calls for named methods
    13. Visibility modifiers
      1. The internal modifier and Java bytecode
    14. Sealed classes
    15. Nested classes
    16. Import aliases
    17. Summary
  6. Functions as First-Class Citizens
    1. Function types
      1. What is function type under the hood?
    2. Anonymous functions
    3. Lambda expressions
      1. The implicit name of a single parameter
    4. Higher-order functions
      1. Providing operations to functions
      2. Observer (Listener) pattern
      3. A callback after a threaded operation
    5. Combination of named arguments and lambda expressions
    6. The last lambda in an argument convention
      1. Named code surrounding
      2. Processing data structures using LINQ style
    7. Java SAM support in Kotlin
    8. Named Kotlin function types
      1. Named parameters in function types
      2. Type alias
    9. Underscore for unused variables
    10. Destructuring in lambda expressions
    11. Inline functions
      1. The noinline modifier
      2. Non-local returns
      3. Labeled returns in lambda expressions
      4. Crossinline modifier
      5. Inline properties
    12. Function references
    13. Summary
  7. Generics Are Your Friends
    1. Generics
      1. The need for generics
        1. Type parameters versus type arguments
    2. Generic constraints
      1. Nullability
    3. Variance
      1. Variance modifiers
      2. Use-site variance versus declaration-site variance
      3. Collection variance
      4. Variance producer/consumer limitation
      5. Invariant constructor
    4. Type erasure
      1. Reified type parameters
        1. The startActivity method
    5. Star-projections
    6. Type parameter naming convention
    7. Summary
  8. Extension Functions and Properties
    1. Extension functions
      1. Extension functions under the hood
        1. No method overriding
        2. Access to receiver elements
        3. Extensions are resolved statically
      2. Companion object extensions
      3. Operator overloading using extension functions
      4. Where should top-level extension functions be used?
    2. Extension properties
      1. Where should extension properties be used?
    3. Member extension functions and properties
      1. Types of receivers
      2. Member extension functions and properties under the hood
    4. Generic extension functions
      1. Collection processing
        1. The Kotlin collection type hierarchy
        2. The map, filter, and flatMap functions
        3. The forEach and onEach functions
        4. The withIndex and indexed variants
        5. The sum, count, min, max, and sorted functions
        6. Other stream processing functions
        7. Examples of stream collection processing
        8. Sequence
    5. Function literals with a receiver
      1. Kotlin standard library functions
        1. The let function
        2. Using the apply function for initialization
        3. The also function
        4. The run and with functions
        5. The to function
      2. Domain-specific language
        1. Anko
    6. Summary
  9. Delegates
    1. Class delegation
      1. The Delegation pattern
      2. Decorator pattern
    2. Property delegation
      1. What are delegated properties?
      2. Predefined delegates
        1. The lazy function
        2. The notNull function
        3. The observable delegate
        4. The vetoable delegate
        5. Property delegation to the Map type
      3. Custom delegates
        1. View binging
        2. Preference binding
        3. Providing a delegate
    3. Summary
  10. Making Your Marvel Gallery Application
    1. Marvel Gallery
      1. How to use this chapter
      2. Making an empty project
      3. Character gallery
        1. View implementation
        2. Network definition
        3. Business logic implementation
        4. Putting it all together
      4. Character search
      5. Character profile display
    2. Summary

Product information

  • Title: Android Development with Kotlin
  • Author(s): Marcin Moskala, Igor Wojda
  • Release date: August 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781787123687