O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Kotlin Programming By Example

Book Description

Enhance your Kotlin programming skills by building 3 real-world applications

About This Book

  • Build three full-fledged, engaging applications from scratch and learn to deploy them
  • Enhance your app development and programming activities with Kotlin's powerful and intuitive tools and utilities.
  • Experience the gentle learning curve, expressiveness, and intuitiveness of Kotlin, as you develop your own applications

Who This Book Is For

This book is for those who are new to Kotlin or are familiar with the basics, having dabbled with Java until now. Basic programming knowledge is mandatory.

What You Will Learn

  • Learn the building blocks of the Kotlin programming language
  • Develop powerful RESTful microservices for Android applications
  • Create reactive Android applications efficiently
  • Implement an MVC architecture pattern and dependency management using Kotlin
  • Centralize, transform, and stash data with Logstash
  • Secure applications using Spring Security
  • Deploy Kotlin microservices to AWS and Android applications to the Play Store

In Detail

Kotlin greatly reduces the verbosity of source code. With Google having announced their support for Kotlin as a first-class language for writing Android apps, now's the time learn how to create apps from scratch with Kotlin

Kotlin Programming By Example takes you through the building blocks of Kotlin, such as functions and classes. You'll explore various features of Kotlin by building three applications of varying complexity. For a quick start to Android development, we look at building a classic game, Tetris, and elaborate on object-oriented programming in Kotlin. Our next application will be a messenger app, a level up in terms of complexity. Before moving onto the third app, we take a look at data persistent methods, helping us learn about the storage and retrieval of useful applications. Our final app is a place reviewer: a web application that will make use of the Google Maps API and Place Picker.

By the end of this book, you will have gained experience of of creating and deploying Android applications using Kotlin.

Style and Approach

Here we will build three exciting projects in Kotlin which will demonstrate how to effectively use Kotlin language constructs

Downloading the example code for this book You can download the example code files for all Packt books you have purchased 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.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Kotlin Programming By Example
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. The Fundamentals
    1. Getting started with Kotlin
      1. JDK installation
        1. Installation on Windows
        2. Installation on macOS
        3. Installation on Linux
      2. Compiling Kotlin programs
        1. Working with the command-line compiler
        2. Installing the command-line compiler on macOS
          1. Homebrew
          2. MacPorts
        3. Installing the command-line compiler on Linux
          1. SDKMAN!
        4. Installing the command-line compiler on Windows
      3. Running your first Kotlin program
        1. Writing scripts with Kotlin
        2. Using the REPL
      4. Working with an IDE
        1. Installing IntelliJ IDEA
        2. Setting up a Kotlin project with IntelliJ
    2. The fundamentals of the Kotlin programming language
      1. Kotlin basics
        1. Variables
          1. Variable scope
          2. Local variables
        2. Operands and operators
        3. Types
          1. Int
          2. Float
          3. Double
          4. Boolean
          5. String
          6. Char 
          7. Array
        4. Functions
          1. Declaring functions
          2. Invoking functions
          3. Return values
          4. The function naming convention
        5. Comments
          1. Single-line comments
          2. Multiline comments
          3. Doc comments
        6. Controlling program flow
          1. Conditional expressions
          2. The if expression
          3. The when expression
          4. The Elvis operator
        7. Loops
          1. The for loops
          2. The while loops
          3. The break and continue keywords
          4. The do…while loops
        8. Packages
          1. The import keyword
      2. Object-oriented programming concepts
        1. Introduction
        2. Working with classes
        3. Creating objects
        4. Companion objects
        5. Properties
    3. Advantages of Kotlin
    4. Developing Android applications with Kotlin
      1. Setting up Android Studio
      2. Building your first Android application
        1. Creating a user interface
        2. Running the application
    5. Fundamentals of the web
      1. What is the web?
      2. Hypertext Transfer Protocol
      3. Clients and servers
      4. HTTP requests and responses
      5. HTTP methods
    6. Summary
  8. Building an Android Application – Tetris
    1. Android – an overview
      1. Application components
        1. Activities
        2. Intents
        3. Intent filters
        4. Fragments
        5. Services
        6. Loaders
        7. Content providers
    2. Understanding Tetris
    3. Creating the user interface
      1. Implementing the layouts
        1. ConstraintLayout
          1. Margins
          2. Chains
          3. Dimension constraints
        2. Defining dimension resources
        3. Views
        4. View groups
        5. Defining string resources
        6. Handling input events
          1. Event listeners
        7. Working with SharedPreferences
        8. Implementing the game activity layout
    4. The app manifest
      1. Structure of the app manifest file
        1. <action>
        2. <activity>
        3. <application>
        4. <category>
        5. <intent-filter>
        6. <manifest>
    5. Summary
  9. Implementing Tetris Logic and Functionality
    1. Implementing the Tetris gameplay
      1. Modeling a tetromino
        1. Characteristics of a block
        2. Behaviors of a block
        3. Modeling block shape
          1. Private access modifier (private)
          2. Public access modifier (public)
          3. Protected access modifier (protected)
          4. Internal visibility modifier (internal)
      2. Creating the application model
      3. Creating TetrisView
        1. Implementing ViewHandler
        2. Implementing Dimension
        3. Implementing TetrisView
      4. Finishing up GameActivity
    2. Introduction to Model-View-Presenter (MVP)
      1. What is MVP?
        1. Model
        2. View  
        3. Presenter
      2. Varying implementations of MVP
    3. Summary
  10. Designing and Implementing the Messenger Backend with Spring Boot 2.0
    1. Designing the messenger API
      1. Application programming interfaces
      2. REST
      3. Designing the messenger API system
        1. Incremental development
        2. Spring Boot
        3. What the messenger system does
          1. Use cases
          2. System behavior
          3. Identifying data
    2. Implementing the messenger backend
      1. PostgreSQL 
        1. Installing PostgreSQL
          1. Windows installation
          2. macOS installation
          3. Linux installation
      2. Creating a new Spring Boot application
      3. Getting familiar with Spring Boot
        1. Maven
        2. Creating models
        3. Creating repositories
        4. Services and service implementations
      4. Restricting API access
        1. Spring Security
        2. JSON Web Tokens
        3. Configuring web security
        4. Accessing server resources via RESTful endpoints
    3. Deploying the messenger API to AWS
      1. Setting up PostgreSQL on AWS
      2. Deploying the messenger API to Amazon Elastic Beanstalk
    4. Summary
  11. Building the Messenger Android App – Part I
    1. Developing the Messenger app
      1. Including project dependencies
      2. Developing the Login UI
        1. Creating the login view
        2. Creating the Messenger API service and data repositories
          1. Storing data locally with SharedPreferences
          2. Creating value objects
        3. Retrieving remote data
          1. Communicating with a remote server
          2. Communicating with servers using OkHttp
          3. Sending requests to a server with OkHttp
          4. Modeling request data
          5. Creating the Messenger API service
          6. Implementing data repositories
        4. Creating the login interactor
        5. Creating the login presenter
        6. Finishing the LoginView
      3. Developing the signup UI
        1. Creating the signup interactor
        2. Creating the signup presenter
        3. Creating the signup view
    2. Summary
  12. Building the Messenger Android App – Part II
    1. Creating the Main UI
      1. Creating the MainView
      2. Creating the MainInteractor
      3. Creating the MainPresenter
      4. Wrapping up the MainView
      5. Creating the MainActivity menu
    2. Creating the Chat UI
      1. Creating the chat layout
      2. Preparing chat UI models
      3. Creating the ChatInteractor and ChatPresenter
    3. Creating the application's settings activity
    4. Android application testing
    5. Performing background operations
      1. AsyncTask
      2. IntentService
    6. Summary
  13. Storing Information in a Database
    1. Working with internal storage
      1. Writing files to internal storage
      2. Reading files from internal storage
      3. A sample application using internal storage
      4. Saving cached files
    2. Working with external storage
      1. Getting external storage permission
      2. Asserting media availability
      3. Storing sharable files
      4. Caching files with external storage
    3. Network storage
    4. Working with a SQLite database
    5. Working with content providers
    6. Summary
  14. Securing and Deploying an Android App
    1. Securing an Android application
      1. Data storage
        1. Using internal storage
        2. Using external storage
        3. Using content providers
      2. Networking security considerations
        1. IP networking
        2. Telephony networking
      3. Input validation
      4. Working with user credentials
      5. Code obfuscation
      6. Securing broadcast receivers
      7. Dynamically loading code
      8. Securing services
    2. Launching and publishing your Android application
      1. Understanding the Android developer program policies
      2. Preparing your Android developer account
      3. Localization planning
      4. Planning for simultaneous release
      5. Testing against the quality guidelines
      6. Building a release-ready application package (APK)
      7. Planning your application's Play Store listing
      8. Uploading your application package to the alpha or beta channel
      9. Device compatibility definition
      10. Pre-launch report assessment
      11. Pricing and application distribution setup
      12. Distribution option selection
      13. In-app products and subscriptions setup
      14. Determining your application's content rating
      15. Publishing your application
        1. Signing your application for release
      16. Releasing your Android app
    3. Summary
  15. Creating the Place Reviewer Backend with Spring
    1. The MVC design pattern
      1. The model
      2. The view
      3. The controller
    2. Designing and implementing the Place Reviewer backend
      1. Use case identification
      2. Identifying data
      3. Setting up the database
      4. Implementing the backend
      5. Connecting the backend to Postgres
      6. Creating models
      7. Creating data repositories
      8. Place Reviewer business logic implementation
      9. Securing the Place Reviewer backend
      10. Serving web content with Spring MVC
    3. Managing Spring application logs with ELK
      1. Generating logs with Spring
      2. Installing Elasticsearch
      3. Installing Kibana
      4. Installing Logstash
      5. Configuring Kibana
    4. Summary
  16. Implementing the Place Reviewer Frontend
    1. Creating views with Thymeleaf
      1. Implementing the user registration view
      2. Implementing the login view
      3. Setting up the Place Reviewer app with the Google Places API web service
        1. Getting an API key
        2. Including Google Places API in your web application
      4. Implementing the home view
      5. Implementing the review creation web page
    2. Spring application testing
      1. Adding necessary testing dependencies to the project
      2. Creating a configuration class
      3. Configuring a test class to use custom configuration
      4. Writing your first test
    3. Summary
    4. What next?
  17. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think