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
Arrow up icon
GO TO TOP
How to Build Android Apps with Kotlin

You're reading from   How to Build Android Apps with Kotlin A practical guide to developing, testing, and publishing your first Android apps

Arrow left icon
Product type Paperback
Published in May 2023
Publisher Packt
ISBN-13 9781837634934
Length 704 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (4):
Arrow left icon
Alex Forrester Alex Forrester
Author Profile Icon Alex Forrester
Alex Forrester
Jomar Tigcal Jomar Tigcal
Author Profile Icon Jomar Tigcal
Jomar Tigcal
Eran Boudjnah Eran Boudjnah
Author Profile Icon Eran Boudjnah
Eran Boudjnah
Alexandru Dumbravan Alexandru Dumbravan
Author Profile Icon Alexandru Dumbravan
Alexandru Dumbravan
Arrow right icon
View More author details
Toc

Table of Contents (24) Chapters Close

Preface 1. Part 1: Android Foundation
2. Chapter 1: Creating Your First App FREE CHAPTER 3. Chapter 2: Building User Screen Flows 4. Chapter 3: Developing the UI with Fragments 5. Chapter 4: Building App Navigation 6. Part 2: Displaying Network Calls
7. Chapter 5: Essential Libraries: Retrofit, Moshi, and Glide 8. Chapter 6: Adding and Interacting with RecyclerView 9. Chapter 7: Android Permissions and Google Maps 10. Chapter 8: Services, WorkManager, and Notifications 11. Chapter 9: Building User Interfaces Using Jetpack Compose 12. Part 3: Testing and Code Structure
13. Chapter 10: Unit Tests and Integration Tests with JUnit, Mockito, and Espresso 14. Chapter 11: Android Architecture Components 15. Chapter 12: Persisting Data 16. Chapter 13: Dependency Injection with Dagger, Hilt, and Koin 17. Part 4: Polishing and Publishing an App
18. Chapter 14: Coroutines and Flow 19. Chapter 15: Architecture Patterns 20. Chapter 16: Animations and Transitions with CoordinatorLayout and MotionLayout 21. Chapter 17: Launching Your App on Google Play 22. Index 23. Other Books You May Enjoy

Setting up a virtual device and running your app

As a part of installing Android Studio, you downloaded and installed the latest Android software development kit (SDK) components. These included a base emulator, which you will configure to create a virtual device to run Android apps on. An emulator mimics the hardware and software features and configuration of a real device. The benefit is that you can make changes and quickly see them on your desktop while developing your app. Although virtual devices do not have all the features of a real device, the feedback cycle is often quicker than going through the steps of connecting a real device.

Also, although you should ensure your app runs as expected on different devices, you can standardize it by targeting a specific device by downloading a device profile, even if you don’t have a real device if this is a requirement of your project.

The screen you will have seen (or something similar) when installing Android Studio is as follows:

Figure 1.4 – SDK components

Figure 1.4 – SDK components

Let’s take a look at the SDK components that are installed and how the virtual device fits in:

  • Android Emulator: This is the base emulator, which we will configure to create virtual devices of different Android makes and models.
  • Android SDK Build-Tools: Android Studio uses the build tools to build your app. This process involves compiling, linking, and packaging your app to prepare it for installation on a device.
  • Android SDK Platform: This is the version of the Android platform that you will use to develop your app. The platform refers to the API level.
  • Android SDK Platform-Tools: These are tools you can use, ordinarily, from the command line, to interact with and debug your app.
  • Android SDK Tools: In contrast to the platform tools, these are tools that you use predominantly from within Android Studio in order to accomplish certain tasks, such as the virtual device for running apps and the SDK manager to download and install platforms and other components of the SDK.
  • Intel x86 Emulator Accelerator (HAXM installer): If your OS provides it, this is a feature at the hardware level of your computer you will be prompted to enable, which allows your emulator to run more quickly.
  • SDK Patch Applier v4: As newer versions of Android Studio become available, this enables patches to be applied to update the version you are running.

With this knowledge, let’s start with the next exercise of this chapter.

Exercise 1.02 – setting up a virtual device and running your app on it

We set up an Android Studio project to create our app in Exercise 1.01, Creating an Android Studio project for your app, and we are now going to run it on a virtual device. You can also run your app on a real device, but you will use a virtual device in this exercise. This process is a continuous cycle while working on your app. Once you have implemented a feature, you can verify its look and behavior as you require.

For this exercise, you will create a single virtual device, but you should ensure you run your app on multiple devices to verify that its look and behavior are consistent. Perform the following steps:

  1. In the toolbar in Android Studio, you will see two drop-down boxes next to each other with app and No devices pre-selected:
Figure 1.5 – The Android Studio toolbar

Figure 1.5 – The Android Studio toolbar

app is the configuration of the app that we will run. As we haven’t set up a virtual device yet, it says No devices.

  1. In order to create a virtual device, click on Device Manager, as shown in Figure 1.5, to open the virtual devices window/screen. The option to do this can also be accessed from the Tools menu:
Figure 1.6 – Device Manager in the Tools menu

Figure 1.6 – Device Manager in the Tools menu

  1. Click the button or toolbar option to open the Device Manager window and click the Create device button, as shown in Figure 1.7:
Figure 1.7 – The Device Manager window

Figure 1.7 – The Device Manager window

You will then be presented with a screen, as shown in Figure 1.8:

Figure 1.8 – Device definition creation

Figure 1.8 – Device definition creation

  1. We are going to choose the Pixel 6 device. The real (non-virtual device) Pixel range of devices is developed by Google and has access to the most up-to-date versions of the Android platform. Once selected, click the Next button:
Figure 1.9 – System Image

Figure 1.9 – System Image

The Tirimasu name displayed here is the initial code/release name for Android 13. Select the latest system image available. The Target column might also show (Google Play) or (Google APIs) in the name. Google APIs mean that the system image comes pre-installed with Google Play Services.

This is a rich feature set of Google APIs and Google apps that your app can use and interact with. On first running the app, you will see apps such as Maps and Chrome instead of a plain emulator image. A Google Play system image means that, in addition to the Google APIs, the Google Play app will also be installed.

  1. You should develop your app with the latest version of the Android platform to benefit from the latest features. On first creating a virtual device, you will have to download the system image. If a Download link is displayed next to Release Name, click on it, and wait for the download to complete. Select the Next button to see the virtual device you have set up:
Figure 1.10 – Virtual device configuration

Figure 1.10 – Virtual device configuration

  1. Click Finish, and your virtual device will be created. You will then see your device highlighted:
Figure 1.11 – Virtual devices listed

Figure 1.11 – Virtual devices listed

  1. Press the play arrow button under the Actions column to run the virtual device:
Figure 1.12 – Virtual device launched

Figure 1.12 – Virtual device launched

You will then see the virtual device running within Android Studio in the Emulator tool window. Now that you’ve created the virtual device and it’s running, you can go back into Android Studio to run your app.

  1. The virtual device you have set up and started will be selected. Press the green triangle/play button to launch your app:
Figure 1.13 – App launch configuration

Figure 1.13 – App launch configuration

This will load the app into the emulator as shown in Figure 1.14.

Figure 1.14 – The app running on a virtual device

Figure 1.14 – The app running on a virtual device

In this exercise, you have gone through the steps to create a virtual device and run the app you created on it. The Android Virtual Device Manager, which you have used to do this, enables you to create the device (or range of devices) you would like to target your app for. Running your app on the virtual device allows a quick feedback cycle to verify how a new feature development behaves and that it displays the way you expect it to.

Next, you will explore the AndroidManifest.xml file of your project, which contains the information and configuration of your app.

You have been reading a chapter from
How to Build Android Apps with Kotlin - Second Edition
Published in: May 2023
Publisher: Packt
ISBN-13: 9781837634934
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image