Book Image

How to Build Android Apps with Kotlin

By : Alex Forrester, Eran Boudjnah, Alexandru Dumbravan, Jomar Tigcal
Book Image

How to Build Android Apps with Kotlin

By: Alex Forrester, Eran Boudjnah, Alexandru Dumbravan, Jomar Tigcal

Overview of this book

Are you keen to get started building Android 11 apps, but don’t know where to start? How to Build Android Apps with Kotlin is a comprehensive guide that will help kick-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 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. Moving ahead, you'll get to grips with testing, learn how to keep your architecture clean, understand how to persist data, and gain 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.
Table of Contents (17 chapters)
Preface
12
12. Dependency Injection with Dagger and Koin

About the Book

Android has ruled the app market for the past decade, and developers are increasingly looking to start building their own Android apps. How to Build Android Apps with Kotlin starts with the building blocks of Android development, teaching you how to use Android Studio, the integrated development environment (IDE) for Android, with the programming language Kotlin for app development. Then, you'll learn how to create apps and run them on virtual devices through guided exercises. You'll cover the fundamentals of Android development, from structuring an app to building out the UI with Activities and Fragments and various navigation patterns. Progressing through the chapters, you'll delve into Android's RecyclerView to make the most of displaying lists of data and become comfortable with fetching data from a web service and handling images. You'll then learn about mapping, location services, and the permissions model before working with notifications and how to persist data. Moving on, you'll get to grips with testing, covering the full spectrum of the test pyramid. You'll also learn how AAC (Android Architecture Components) are used to cleanly structure your code and explore various architecture patterns and the benefits of dependency injection. The core libraries of RxJava and Coroutines are covered for asynchronous programming. The focus then returns to the UI, demonstrating how to add motion and transitions when users interact with your apps. Towards the end, you'll build an interesting app to retrieve and display popular movies from a movie database, and then see how to publish your apps on Google Play. By the end of this book, you'll have the skills and confidence needed to build fully-fledged Android apps using Kotlin.

ABOUT THE AUTHORS

Alex Forrester is an experienced software developer with more than 20 years of experience in mobile, web development, and content management systems. He has been working with Android for over 8 years, creating flagship apps for blue-chip companies across a broad range of industries at Sky, The Automobile Association, HSBC, The Discovery Channel, and O2. Alex lives in Hertfordshire with his wife and daughter. When he's not developing, he likes rugby and running in the Chiltern hills.

Eran Boudjnah is a developer with over 20 years of experience in developing desktop applications, websites, interactive attractions, and mobile applications. He has been working with Android for about 7 years, developing apps and leading mobile teams for a wide range of clients, from start-ups (JustEat) to large scale companies (Sky) and conglomerates. He is passionate about board games (with a modest collection of a few hundred games) and has a Transformers collection he's quite proud of. Eran lives in North London with Lea, his wife.

Alexandru Dumbravan started Android development in 2011 working for a digital agency. In 2016, he moved to London, working mainly in the FinTech sector. Over the course of his career, he has had the opportunity to analyze and integrate many different technologies on Android devices, from well-known applications like Facebook login, to lesser-known tech, like proprietary network protocols.

Jomar Tigcal is an Android developer with over 10 years of experience in mobile and software development. He has worked on various stages of app development for small startups and large companies. Jomar has also given talks and conducted training and workshops on Android. In his free time, he likes running and reading. He lives in Vancouver, Canada, with his wife Celine.

Audience

If you want to build your own Android apps using Kotlin but are unsure of how to begin, then this book is for you. A basic understanding of the Kotlin programming language will help you grasp the topics covered in this book more quickly.

About the Chapters

Chapter 1, Creating Your First App, shows how to use Android Studio to build your first Android app. Here you will create an Android Studio project and understand what it's made up of, and explore the tools necessary for building and deploying an app on a virtual device. You will also learn about the structure of an Android app.

Chapter 2, Building User Screen Flows, dives into the Android ecosystem and the building blocks of an Android application. Concepts such as activities and their lifecycle, intents, and tasks will be introduced, as well as restoring state and passing data between screens or activities.

Chapter 3, Developing the UI with Fragments, teaches you the fundamentals of using fragments for the user interface of an Android application. You will learn how to use fragments in multiple ways to build application layouts for phones and tablets, including using the Jetpack Navigation component.

Chapter 4, Building App Navigation, goes through the different types of navigation in an application. You will learn about navigation drawers with sliding layouts, bottom navigation, and tabbed navigation.

Chapter 5, Essential Libraries: Retrofit, Moshi, and Glide, gives you an insight into how to build apps that fetch data from a remote data source with the use of the Retrofit library and the Moshi library to convert data into Kotlin objects. You will also learn about the Glide library, which loads remote images into your app.

Chapter 6, RecyclerView, introduces the concept of building lists and displaying them with the help of the RecyclerView widget.

Chapter 7, Android Permissions and Google Maps, presents the concept of permissions and how to request them from the user in order for your app to execute specific tasks, as well as introducing you to the Maps API.

Chapter 8, Services, WorkManager, and Notifications, details the concept of background work in an Android app and how you can have your app execute certain tasks in a way that is invisible to the user, as well as covering how to show a notification of this work.

Chapter 9, Unit Tests and Integration Tests with JUnit, Mockito, and Espresso, teaches you about the different types of tests for an Android application, what frameworks are used for each type of test, and the concept of test-driven development.

Chapter 10, Android Architecture Components, gives an insight into components from the Android Jetpack libraries, such as LiveData and ViewModel, which help you structure your code, and Room, which allows you to persist data on a device in a database.

Chapter 11, Persisting Data, shows you the various ways to store data on a device, from SharedPreferences to files. The Repository concept will also be introduced, giving you an idea of how to structure your app in different layers.

Chapter 12, Dependency Injection with Dagger and Koin, explains the concept of dependency injection and the benefits it provides to an application. Frameworks such as Dagger and Koin are introduced to help you manage your dependencies.

Chapter 13, RxJava and Coroutines, introduces you to doing background operations and data manipulations with RxJava and Coroutines. You'll also learn about manipulating and displaying data using RxJava operators and LiveData transformation.

Chapter 14, Architecture Patterns, explains the architecture patterns you can use to structure your Android projects to separate them into different components with distinct functionality. These make it easier for you to develop, test, and maintain your code.

Chapter 15, Animations and Transitions with CoordinatorLayout and MotionLayout, discusses how to enhance your apps with animations and transitions with CoordinatorLayout and MotionLayout.

Chapter 16, Launching Your App on Google Play, concludes this book by showing you how to publish your apps on Google Play: from preparing a release, to creating a Google Play Developer account, to finally launching your app.

Conventions

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows:

"You can find it in the main project window under MyApplication | app | src | main."

A block of code is set as follows:

<resources>
    <string name="app_name">My Application</string>
</resources>

In some cases, important lines of code are highlighted. These cases are presented as follows:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My Application</string>
    <string name="first_name_text">First name:</string>
    <string name="last_name_text">Last name:</string>
</resources>

Words that you see on the screen, for example, in menus or dialog boxes, also appear in the text like this: "Click Finish and your virtual device will be created."

New terms and important words are shown like this: "It is the official Integrated Development Environment (IDE) for Android development, built on JetBrains' IntelliJ IDEA software and developed by the Android Studio team at Google."

Before You Begin

Each great journey begins with a humble step. Before we can do awesome things in Android, we need to be prepared with a productive environment. In this section, we will see how to do that.

Minimum Hardware Requirements

For an optimal learning experience, we recommend the following hardware configuration:

  • Processor: Intel Core i5 or equivalent or higher
  • Memory: 4 GB RAM minimum; 8 GB RAM recommended
  • Storage: 4 GB available space

Software Requirements

You'll also need the following software installed in advance:

  • OS: Windows 7 SP1 64-bit, Windows 8.1 64-bit or Windows 10 64-bit, macOS, or Linux
  • Android Studio 4.1 or higher

Installation and Setup

Before you start this book, you will need to install Android Studio 4.1 (or higher), which is the main tool that you will be using throughout the chapters. You can download Android Studio from https://developer.android.com/studio.

On macOS, launch the DMG file and drag and drop Android Studio into the Applications folder. Once this is done, open Android Studio. On Windows, launch the EXE file. If you're using Linux, unpack the ZIP file into your preferred location. Open your Terminal and navigate to the android-studio/bin/ directory and execute studio.sh. If you see an Import Settings dialog pop-up, select Do not import settings and click the OK button (this usually occurs when there is a previous installation of Android Studio):

Figure 0.1: The import settings dialog

Figure 0.1: The import settings dialog

Next, the Data Sharing dialog will pop up; click the Don't send button to disable sending anonymous usage data to Google:

Figure 0.2: The Data Sharing dialog

Figure 0.2: The Data Sharing dialog

In the Welcome dialog, click the Next button to start the setup:

Figure 0.3: The Welcome dialog

Figure 0.3: The Welcome dialog

In the Install Type dialog, select Standard to install the recommended settings. Then, click the Next button:

Figure 0.4: The Install Type dialog

Figure 0.4: The Install Type dialog

In the Select UI Theme dialog, choose your preferred IDE theme—either Light or Dracula (dark theme) - then click the Next button:

Figure 0.5: The Select UI Theme dialog

Figure 0.5: The Select UI Theme dialog

In the Verify Settings dialog, review your settings and then click the Finish button. The setup wizard downloads and installs additional components, including the Android SDK:

Figure 0.6: The Verify Settings dialog

Figure 0.6: The Verify Settings dialog

Once the download finishes, you can click the Finish button. You are now ready to create your Android project.

Installing the Code Bundle

You can download the code files and activity solutions from GitHub at https://github.com/PacktPublishing/How-to-Build-Android-Apps-with-Kotlin. Refer to these code files for the complete code bundle.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have any questions about this book, please mention the book title in the subject of your message and email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you could report this to us. Please visit www.packtpub.com/support/errata and complete the form.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you could provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Please leave a review

Let us know what you think by leaving a detailed, impartial review on Amazon. We appreciate all feedback – it helps us continue to make great products and help aspiring developers build their skills. Please spare a few minutes to give your thoughts – it makes a big difference to us.