How to Build Android Apps with Kotlin - Second Edition

Book description

Unleash the power of Android programming to build scalable and reliable apps using industry best practices Purchase of the print or Kindle book includes a free PDF eBook

Key Features

  • Build apps with Kotlin, Google’s preferred programming language for Android development
  • Unlock solutions to development challenges with guidance from experienced Android professionals
  • Improve your apps by adding valuable features that make use of advanced functionality

Book Description

Looking to kick-start your app development journey with Android 13, but don’t know where to start? How to Build Android Apps with Kotlin is a comprehensive guide that will help jump-start your Android development practice.

This book starts with the fundamentals of app development, enabling you to utilize Android Studio and Kotlin to get started with building Android projects. You'll learn how to create apps and run them on virtual devices through guided exercises. Progressing through the chapters, you'll delve into Android's RecyclerView to make the most of lists, images, and maps, and see how to fetch data from a web service.

You'll also get to grips with testing, learning how to keep your architecture clean, understanding how to persist data, and gaining basic knowledge of the dependency injection pattern. Finally, you'll see how to publish your apps on the Google Play store.

You'll work on realistic projects that are split up into bitesize exercises and activities, allowing you to challenge yourself in an enjoyable and attainable way. You'll build apps to create quizzes, read news articles, check weather reports, store recipes, retrieve movie information, and remind you where you parked your car.

By the end of this book, you'll have the skills and confidence to build your own creative Android applications using Kotlin.

What you will learn

  • Create maintainable and scalable apps using Kotlin
  • Understand the Android app development lifecycle
  • Simplify app development with Google architecture components
  • Use standard libraries for dependency injection and data parsing
  • Apply the repository pattern to retrieve data from outside sources
  • Build user interfaces using Jetpack Compose
  • Explore Android asynchronous programming with Coroutines and the Flow API
  • Publish your app on the Google Play store

Who this book is for

If you want to build Android applications using Kotlin but are unsure of how and where to begin, then this book is for you. To easily grasp the concepts in this book, a basic understanding of Kotlin, or experience in a similar programming language is a must.

Table of contents

  1. How to Build Android Apps with Kotlin
  2. Contributors
  3. About the authors
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Minimum hardware requirements
      2. Software requirements
      3. Installation and setup
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share your thoughts
    9. Download a free PDF copy of this book
  6. Part 1: Android Foundation
  7. Chapter 1: Creating Your First App
    1. Technical requirements
    2. Creating an Android project with Android Studio
      1. Exercise 1.01 – creating an Android Studio project for your app
    3. Setting up a virtual device and running your app
      1. Exercise 1.02 – setting up a virtual device and running your app on it
    4. The Android manifest
      1. Exercise 1.03 – configuring the Android manifest internet permission
    5. Using Gradle to build, configure, and manage app dependencies
      1. The project-level build.gradle file
      2. The app-level build.gradle file
      3. Exercise 1.04 – exploring how Material Design is used to theme an app
    6. Android application structure
      1. Exercise 1.05 – adding interactive UI elements to display a bespoke greeting to the user
      2. Accessing Views in layout files
      3. Further input validation
      4. Activity 1.01 – producing an app to create RGB colors
    7. Summary
  8. Chapter 2: Building User Screen Flows
    1. Technical requirements
    2. The Activity lifecycle
      1. Exercise 2.01 – logging the Activity Callbacks
    3. Saving and restoring the Activity state
      1. Exercise 2.02 – saving and restoring the state in layouts
      2. Exercise 2.03 – saving and restoring the state with Callbacks
    4. Activity interaction with Intents
      1. Exercise 2.04 – an introduction to Intents
      2. Exercise 2.05 – retrieving a result from an Activity
    5. Intents, Tasks, and Launch Modes
      1. Exercise 2.06 – setting the Launch Mode of an Activity
      2. Activity 2.01 – creating a login form
    6. Summary
  9. Chapter 3: Developing the UI with Fragments
    1. Technical requirements
    2. The fragment lifecycle
      1. onAttach
      2. onCreate
      3. onCreateView
      4. onViewCreated
      5. onActivityCreated
      6. onStart
      7. onResume
      8. onPause
      9. onStop
      10. onDestroyView
      11. onDestroy
      12. onDetach
      13. Exercise 3.01 – adding a basic fragment and the fragment lifecycle
      14. Exercise 3.02 – adding fragments statically to an activity
    3. Static fragments and dual-pane layouts
      1. Exercise 3.03 – dual-pane layouts with static fragments
    4. Dynamic fragments
      1. Exercise 3.04 – adding fragments dynamically to an activity
    5. Jetpack Navigation
      1. Exercise 3.05 – adding a Jetpack navigation graph
      2. Activity 3.01 – creating a quiz on the planets
    6. Summary
  10. Chapter 4: Building App Navigation
    1. Technical requirements
    2. Navigation overview
    3. Navigation drawer
      1. Exercise 4.01 – creating an App with a navigation drawer
    4. Bottom navigation
      1. Exercise 4.02 – adding bottom navigation to your app
    5. Tabbed navigation
      1. Exercise 4.03 – using tabs for app navigation
      2. Activity 4.01 – building primary and secondary app navigation
    6. Summary
  11. Part 2: Displaying Network Calls
  12. Chapter 5: Essential Libraries: Retrofit, Moshi, and Glide
    1. Technical requirements
    2. Introducing REST, API, JSON, and XML
    3. Fetching data from a network endpoint
      1. Exercise 5.01 – reading data from an API
    4. Parsing a JSON response
      1. Exercise 5.02 – extracting the image URL from the API response
    5. Loading images from a remote URL
      1. Exercise 5.03 – loading the image from the obtained URL
      2. Activity 5.01 – displaying the current weather
    6. Summary
  13. Chapter 6: Adding and Interacting with RecyclerView
    1. Technical requirements
    2. Adding RecyclerView to our layout
      1. Exercise 6.01 – adding an empty RecyclerView to your main activity
    3. Populating RecyclerView
      1. Exercise 6.02 – populating your RecyclerView
    4. Responding to clicks in RecyclerView
      1. Exercise 6.03 – responding to clicks
    5. Supporting different Item types
      1. Exercise 6.04 – adding titles to RecyclerView
    6. Swiping to remove Items
      1. Exercise 6.05 – adding swipe to delete functionality
    7. Adding items interactively
      1. Exercise 6.06 – implementing an Add A Cat button
      2. Activity 6.01 – managing a list of Items
    8. Summary
  14. Chapter 7: Android Permissions and Google Maps
    1. Technical requirements
    2. Requesting permission from the user
      1. Exercise 7.01 – requesting the location permission
    3. Showing a map of the user’s location
      1. Exercise 7.02 – obtaining the user’s current location
    4. Map clicks and custom markers
      1. Exercise 7.03 – adding a custom marker where the map was clicked
      2. Activity 7.01 – creating an app to find the location of a parked car
    5. Summary
  15. Chapter 8: Services, WorkManager, and Notifications
    1. Technical requirements
    2. Starting a background task using WorkManager
      1. Exercise 8.01 – executing background work with the WorkManager class
    3. Background operations noticeable to the user – using a Foreground Service
      1. Exercise 8.02 – tracking your SCA’s work with a Foreground Service
      2. Activity 8.01 – reminder to drink water
    4. Summary
  16. Chapter 9: Building User Interfaces Using Jetpack Compose
    1. Technical requirements
    2. What is Jetpack Compose?
      1. Exercise 9.01 – first Compose screen
    3. Handling user actions
      1. Exercise 9.02 – handling user inputs
    4. Theming in Compose
      1. Exercise 9.03 – applying themes
    5. Adding Compose to existing projects
      1. Activity 9.01 – first Compose app
    6. Summary
  17. Part 3: Testing and Code Structure
  18. Chapter 10: Unit Tests and Integration Tests with JUnit, Mockito, and Espresso
    1. Technical requirements
    2. Types of testing
    3. JUnit
    4. Android Studio testing tips
    5. Mockito
      1. Exercise 10.01 – testing the sum of numbers
    6. Integration tests
      1. Robolectric
      2. Espresso
      3. Exercise 10.02 – double integration
    7. UI tests
      1. Testing in Jetpack Compose
      2. Exercise 10.03 – random waiting times
    8. TDD
      1. Exercise 10.04 – using TDD to calculate the sum of numbers
      2. Activity 10.01 – developing with TDD
    9. Summary
  19. Chapter 11: Android Architecture Components
    1. Technical requirements
    2. Android components background
    3. ViewModel
      1. Exercise 11.01 – shared ViewModel
    4. Data streams
      1. LiveData
      2. Additional data streams
    5. Room
      1. Entities
      2. DAO
      3. Setting up the database
      4. Third-party frameworks
      5. Exercise 11.03 – making a little room
      6. Activity 11.01 – a shopping notes app
    6. Summary
  20. Chapter 12: Persisting Data
    1. Technical requirements
    2. Preferences and DataStore
      1. SharedPreferences
      2. Exercise 12.01 – wrapping SharedPreferences
      3. DataStore
      4. Exercise 12.02 – Preference DataStore
    3. Files
      1. Internal storage
      2. External storage
      3. FileProvider
      4. The Storage Access Framework (SAF)
      5. Asset files
      6. Exercise 12.03 – copying files
    4. Scoped storage
      1. Camera and media storage
      2. Exercise 12.04 – taking photos
      3. Activity 12.01 – dog downloader
    5. Summary
  21. Chapter 13: Dependency Injection with Dagger, Hilt, and Koin
    1. Technical requirements
    2. The necessity of dependency injection
    3. Manual DI
      1. Exercise 13.01 – manual injection
    4. Dagger 2
      1. Consumers
      2. Providers
      3. Connectors
      4. Qualifiers
      5. Scopes
      6. Subcomponents
      7. Exercise 13.02 – Dagger injection
    5. Hilt
      1. Exercise 13.03 – Hilt injection
    6. Koin
      1. Exercise 13.04 – Koin injection
      2. Activity 13.01 – injected repositories
    7. Summary
  22. Part 4: Polishing and Publishing an App
  23. Chapter 14: Coroutines and Flow
    1. Technical requirements
    2. Using Coroutines on Android
      1. Creating coroutines
      2. Adding coroutines to your project
      3. Exercise 14.01 – using coroutines in an Android app
    3. Transforming LiveData
      1. Exercise 14.02 – LiveData transformations
    4. Using Flow on Android
      1. Collecting Flows on Android
      2. Creating Flows with Flow Builders
      3. Using operators with Flows
      4. Exercise 14.03 – using Flow in an Android application
      5. Activity 14.01 – creating a TV Guide app
    5. Summary
  24. Chapter 15: Architecture Patterns
    1. Technical requirements
    2. Getting started with MVVM
      1. Binding data on Android with data binding
      2. Exercise 15.01– using data binding in an Android project
    3. Using Retrofit and Moshi
      1. Implementing the Repository pattern
      2. Exercise 15.02 – using Repository with Room in an Android project
    4. Using WorkManager
      1. Exercise 15.03 – adding WorkManager to an Android Project
      2. Activity 15.01 – revisiting the TV Guide app
    5. Summary
  25. Chapter 16: Animations and Transitions with CoordinatorLayout and MotionLayout
    1. Technical requirements
    2. Activity transitions
      1. Adding activity transitions through XML
      2. Adding activity transitions through code
      3. Starting an activity with an activity transition
      4. Exercise 16.01 – creating activity transitions in an app
      5. Adding a shared element transition
      6. Starting an activity with the shared element transition
      7. Exercise 16.02 – creating the shared element transition
    3. Animations with CoordinatorLayout
    4. Animations with MotionLayout
      1. Adding MotionLayout
      2. Creating animations with MotionLayout
      3. Exercise 16.03 – adding animations with MotionLayout
      4. The Motion Editor
      5. Debugging MotionLayout
      6. Modifying the MotionLayout path
      7. Exercise 16.04 – modifying the animation path with keyframes
      8. Activity 16.01 – Password Generator
    5. Summary
  26. Chapter 17: Launching Your App on Google Play
    1. Preparing your apps for release
      1. Versioning apps
      2. Creating a keystore
      3. Exercise 17.01 – creating a keystore in Android Studio
      4. Storing the keystore and passwords
      5. Signing your apps for release
      6. Exercise 17.02 – creating a signed APK
      7. Android app bundle
      8. Exercise 17.03 – creating a signed app bundle
      9. App signing by Google Play
    2. Creating a developer account
    3. Uploading an app to Google Play
      1. Creating a store listing
      2. Preparing the release
      3. Rolling out a release
    4. Managing app releases
      1. Release tracks
      2. Staged rollouts
      3. Managed publishing
      4. Activity 17.01 – publishing an app
    5. Summary
  27. Index
    1. Why subscribe?
  28. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share your thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: How to Build Android Apps with Kotlin - Second Edition
  • Author(s): Alex Forrester, Eran Boudjnah, Alexandru Dumbravan, Jomar Tigcal
  • Release date: May 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781837634934