© Ted Hagos 2020
T. HagosLearn Android Studio 4https://doi.org/10.1007/978-1-4842-5937-5_3

3. Project Basics

Ted Hagos1 
(1)
Manila, National Capital Region, Philippines
 
What the chapter covers:
  • Create a simple project in Android Studio

  • Create an emulator (an AVD or Android Virtual Device)

  • Run a test project in the emulator

You can build many things in Android Studio, a business app, an ebook, a casual game (even triple AAA titles, why not?), and so on, but before you can do all that, you need to know the basics of creating, building, and testing an app in Android Studio. This chapter is all about that.

Create a Project

Launch Android Studio. Click “Start a new Android Studio Project,” as shown in Figure 3-1. You have to be online when you do this because Android Studio’s Gradle (a project build tool) pulls quite a few files from online repositories when you start a new project.
../images/457413_2_En_3_Chapter/457413_2_En_3_Fig1_HTML.jpg
Figure 3-1

Opening screen

During the creation process, Android prompts for what kind of project you want to build; choose Phone and TabletEmpty Activity, as shown in Figure 3-2—we’ll discuss Activities in the coming chapters, but for now, think of an Activity as a screen or a form; it’s something that the user sees and interacts with.
../images/457413_2_En_3_Chapter/457413_2_En_3_Fig2_HTML.jpg
Figure 3-2

Create a new project; choose an Activity type

In the next screen, we get to configure the project. We set the app’s name, package name (domain), and the target Android version. Figure 3-3 shows the annotated picture of the “Create New Project” screen.

Name. This is also known as the project name; this also becomes the name of the top-level folder, which will contain all of the project’s files. This name also becomes part of the application’s identity, should you release it in the Play Store.

Package name. This is your organization or company’s domain name in reverse DNS notation. If you don’t have a company name, you can use anything that resembles a web domain. At the moment, it won’t matter if we use a real company name or not, since we won’t release this to the Play Store.

Save location. This is a location in your local directory where the project files will be kept.

Language. You can use either Kotlin or Java; for this project, we will use Java.

Minimum API level. The min API level will determine the lowest version of Android, which your application can run on. You need to choose wisely and prudently because it can severely limit the potential audience for your app.

Help me choose. This shows the percentage of Android devices that your app can run on. If you click the “Help me choose” link, it will open a window that shows the distribution of Android devices, per Android version.

Legacy Android support libraries. These are support libraries. They’re included so that you can use modern Android libraries (like the ones included in Android 9) but still allow your app to be run on devices with lower Android versions.

../images/457413_2_En_3_Chapter/457413_2_En_3_Fig3_HTML.jpg
Figure 3-3

Configure your project

When you’re all done, click “Finish” to begin the project creation. Android Studio scaffolds the project and creates startup files like the main Activity file, Android manifest, and other files to get the project propped up. The build tool (Gradle) will pull quite a few files from online repos; it can take some time.

After all that, hopefully, the project is created, and you get to see Android Studio’s main editor window, as shown in Figure 3-4.
../images/457413_2_En_3_Chapter/457413_2_En_3_Fig4_HTML.jpg
Figure 3-4

Main editor window

Android Studio’s screen is composed of several sections that can collapse and expand, depending on your needs. The section on the left (Figure 3-4) is the Project panel. It’s a tree-like structure that shows all the (relevant) files in the project. If you want to edit a particular file, simply select it in the Project panel and double-click; at that point, it will be opened for editing in the main editor window. In Figure 3-4, you can see the MainActivity.java file available for editing. Over time, we will spend a lot of hours doodling in the main editor window, but for now, we simply want to go through the basic process of application development. We won’t add or modify anything in the Java files or any other files in the project. We’ll leave them as is.

Create an AVD

The next step is to build and test the app. We can do that either by running it in an emulator or plugging in a physical Android device to your workstation. This section covers setting up an emulator.

From Android Studio’s main menu bar, go to ToolsAVD Manager, as shown in Figure 3-5.
../images/457413_2_En_3_Chapter/457413_2_En_3_Fig5_HTML.jpg
Figure 3-5

Menu bar, Tools, AVD Manager

The AVD manager window launches; AVD stands for Android Virtual Device. It’s an emulator that runs a specific version of the Android OS, which we can use for testing the app. The AVD manager (shown in Figure 3-6) shows all the defined emulators in the local development environment.
../images/457413_2_En_3_Chapter/457413_2_En_3_Fig6_HTML.jpg
Figure 3-6

AVD manager

As you can see, I’ve already created a couple of emulators, but let’s create another one; to do that, click the “+ Create Virtual Device” button, as shown in Figure 3-6. That action launches the “Virtual Device Configuration” screen, as shown in Figure 3-7.
../images/457413_2_En_3_Chapter/457413_2_En_3_Fig7_HTML.jpg
Figure 3-7

Virtual Device Configuration

Choose the “Phone” category and then choose the device resolution. I chose the Pixel 5.0” 420dpi screen. Click the “Next” button, and we get to choose the Android version for the emulator; we can do this on the “System Image” screen, shown in Figure 3-8.
../images/457413_2_En_3_Chapter/457413_2_En_3_Fig8_HTML.jpg
Figure 3-8

System Image

I want to use Android 10 (API level 29) or Q, as some may call it; but as you can see, I don’t have that system image in my machine just yet—when you can see the “Download” link next to the Android version, that means you don’t have that system image locally yet. To get the system image for Android 10, click the “Download” link. You need to agree to the license agreement before you can proceed. Click “Accept” and then click “Next,” as shown in Figure 3-9.
../images/457413_2_En_3_Chapter/457413_2_En_3_Fig9_HTML.jpg
Figure 3-9

SDK Quickfix Installation

The download process can take some time, depending on your Internet speed; when it’s done, you’ll get back to the “System Image” selection screen, as shown in Figure 3-10.
../images/457413_2_En_3_Chapter/457413_2_En_3_Fig10_HTML.jpg
Figure 3-10

Virtual Device Configuration

As you can see, we can now use Android 10 as a system image for our emulator. Select it, then click “Next.” The next screen shows a summary of our past choices for creating the emulator; the “Verify Configuration” screen is displayed next (Figure 3-11).
../images/457413_2_En_3_Chapter/457413_2_En_3_Fig11_HTML.jpg
Figure 3-11

Verify Configuration

The “Verify Configuration” screen not only shows the summary of our past choices, you can configure some additional functionalities here. If you click the “Show Advanced Settings” button, you can also configure the following:
  • Front and back camera

  • Emulated network speed

  • Emulated performance

  • Size of internal storage

  • Keyboard input (whether enabled or disabled)

When you’re done, click the “Finish” button. When the AVD gets created, we’ll be back in the “Android Virtual Device Manager” screen, as shown in Figure 3-12.
../images/457413_2_En_3_Chapter/457413_2_En_3_Fig12_HTML.jpg
Figure 3-12

Android Virtual Device Manager

Now we can see the emulator (Pixel API 29). Click the little green arrow on the “Actions” column to launch the emulator—the pencil icon edits the emulator’s configuration, and the green arrow launches it.

When the emulator launches, you’ll see an image of the Pixel phone pop up on the desktop; it needs time to boot up completely. Go back to the main editor window of Android Studio to run the app.

From the main menu bar, go to RunRun app, as shown in Figure 3-13.
../images/457413_2_En_3_Chapter/457413_2_En_3_Fig13_HTML.jpg
Figure 3-13

Main menu bar, Run

Android Studio compiles the project; then, it looks for either a connected (physical) Android device or a running emulator. We already launched the emulator a while ago, so Android Studio should find it and install the app in that emulator instance.

If all went well, you should see the Hello World app that Android Studio scaffolded for us, as shown in Figure 3-14.
../images/457413_2_En_3_Chapter/457413_2_En_3_Fig14_HTML.jpg
Figure 3-14

Hello World

Summary

  • An Android project (almost) always has an Activity. You can start with a basic project and choose “Empty Activity,” as we did in the examples. Then, build from there.

  • Pay some attention to the project package name during creation; if you release the project to Google Play, you won’t be able to change the package name; it will be part of your application.

  • Choose the minimum SDK carefully; it will limit the number of potential users of your app.

  • You can use an emulator to run the app and see how it’s shaping up. Testing using an emulator is much better if you have HAXM (emulator accelerator) enabled on your system; if you’re on Linux, you can have acceleration using KVM.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.22.169