To create Android apps, you first need to install Android Studio. Android Studio is a customized IDE based on IntelliJ, an IDE by JetBrains, that provides a powerful set of tools.
In this chapter, you’ll learn how to:
- Set up Android Studio.
- Set up a physical and emulated device for development.
- Build, install and run an app on your device.
Open a web browser and navigate to https://developer.android.com/studio#downloads.
This is the download page for Android Studio. There are multiple options here because Android Studio runs on a variety of operating systems. Click the package for your operating system to proceed.
Note: This chapter assumes your computer is running macOS; however because Android Studio supports Windows and Linux, this book will provide instructions where possible for those operating systems as well.
After making your selection, the Terms and Conditions screen appears.
To agree to the terms and conditions, check the checkbox at the bottom of the screen, and then click Download Android Studio.
As your computer begins to download Android Studio, in your web browser, navigate to https://developer.android.com/studio/install.
This page provides instructions to install Android Studio. Follow the instructions and/or video relevant to your operating system until the “Welcome to Android Studio” window appears.
Note: If you don’t have a high-speed Internet connection, it can take a while for all of the components to download. Also, depending on how your system is set up, you may need to enter your password or an administrator’s password to allow the installation to complete.
Your first Android project
Now that you have Android Studio installed, it’s time to create your first project. It’s important to note that this chapter focuses on getting your app running as quickly as possible. As such, you’ll encounter a few screens that you won’t understand; but don’t worry, you’ll get a chance to experience those screens in detail when you get to Chapter 6, “Creating a New Project”.
On the Welcome screen, click Create New Project:
The Welcome screen disappears, and a new window takes its place. This is where you choose the type of device you want your project to support.
By default, Phone and Tablet is selected, which shows a variety of screen types. Click Empty Activity to choose it as your preferred project setup. Click Next in the bottom-right of the window to move on to the next step.
This screen asks for some important information:
Name is where you enter the name of the app. For this project, name the app Timefighter.
Package name provides the app with a package name, a concept you might be familiar with from Java or Kotlin. Make sure this field reads as com.raywenderlich.timefighter.
Save location tells Android Studio where to save the project. You can create your project anywhere you want. The folder button to the right of the field provides you with a system navigator to help make your selection.
Language tells Android Studio of the language your project requires. Kotlin is already selected, so no action is needed here.
Minimum SDK sets the minimum version of Android the app will support. This book requires apps to run API 21, or Android Lollipop in English, so ensure API 21: Android 5.0 (Lollipop) is selected.
6. Finish, the button in the bottom-right of the window, completes the project setup. Click this when you’re ready to move on.
Android Studio uses this information and gathers the required libraries and resources to generate a new project. Depending on your Internet connection, this phase may take some time.
When that process finishes, Android Studio displays the newly created project with MainActivity.kt and activity_main.xml already open as tabs:
With the project created, you’re ready to work on it. But don’t jump in so quickly. Android Studio is a complex piece of software. If you dive in too fast, you may find yourself lost.
Before you start building your app, take a moment to review what Android Studio has to offer.
The first window that appears is the Editor. This window provides a space to edit your app’s source code. It includes tools like syntax highlighting, auto-completion for methods and objects as well as the ability to set breakpoints in your code while debugging. You’ll learn more about breakpoints and debugging in Chapter 4, “Debugging”. You’ll spend most of your development time using the Editor to code your app.
To the left of the Editor is the Project navigator. This window shows everything your project contains, including code and image assets. Android Studio provides a lot; you can see what by clicking on the arrow to the left of the items in the Project navigator. For now, don’t worry too much about these files; you’ll get well-acquainted with them as you read through this book.
Creating an Android virtual device
Note: If you have a physical Android device you want to use for development, you can skip to the next section.
Looking at your editor and files is fine, but once you’re done writing code, you need to run the app to see it in action. Before you can do this, you need a device — whether it’s real or virtual. Look at the button highlighted in the following image.
This button opens a new window to show all Android Virtual Devices available, usually abbreviated as an AVD. An AVD is an emulator that acts as a device on your computer. This allows you to test your app without requiring a physical device. If you don’t have a physical device for testing, you’ll need to create a virtual device before you can run your app.
Click the AVD Manager button. A new window appears.
This window shows all of the available AVDs that exist on your machine. At the moment, none are available yet because you just installed Android Studio.
In the middle of the screen, click Create Virtual Device to show the Select Hardware window. In this window, you can select the type of device you want your AVD to emulate.
A device is already selected for you, a Pixel 2. This is an excellent choice because it closely emulates a real device that’s used by a lot of people.
In the bottom-right of the window, click Next to progress to the System Image window. In this window, you can select the version of Android to run on your emulator.
There are a few tabs that run along the top of the list. The most interesting is Recommended, which shows a list of Android versions that Google recommends you use when testing your apps. Currently, those versions are grayed out because none of them are installed on your machine. You can change this by downloading the latest version of Android recommended by Android Studio.
Select the top item in the table and click Download in the Release Name column.
A legal agreement appears describing the terms you need to agree to if you want to use Android in the emulator:
Select Accept, and then click Next.
The Component Installer window appears and automatically downloads the selected version of Android.
Once the download is complete, click Finish in the bottom-right. The Component Installer window disappears, and the System Image window appears once again. At this point, the latest version of Android is ready to use. To move on, click Next in the bottom-right of the window.
The next and final window for creating an emulated device is a summary of the characteristics the device will have.
This window gives you the opportunity to name the AVD and to confirm other aspects of the device such as the Android version. You don’t need to change anything here, so click Finish at the bottom-right of the screen to create the AVD.
The current window disappears. In the AVD window that lists the available AVDs, your newly created AVD shows up and is ready for use:
Setting up an Android device
Note: If you don’t have an Android device to use for development, read the previous section on how to set up an Android Virtual Device.
Before you can install Timefighter onto your device, you need to get your device set up for use with Android Studio. But first, you need to connect your Android device to your machine via a USB cable.
Note: If you’re using a Windows machine, you need to download a USB driver for your device. You can download the driver and find instructions on installing it at https://developer.android.com/studio/run/oem-usb.html.
On your device, open the Settings app.
Scroll through the settings until you find About Phone and tap it.
Note: On some devices, you may need to tap System first to find the About Phone section.
Now for the magical part: Scroll to the bottom of the About Phone screen, until the build number item appears:
When you see the build number item, tap it several times until a message appears that informs you that you’re a few steps away from being a developer. Keep tapping away until another message appears letting you know that you’ve become a developer.
Note: If your device is locked with a PIN, you’ll need to enter it before you can reach this stage.
So what did this magical button do? Tap the back button to go to the Settings page, then tap the System button. Can you see what to look for?
Note: On some devices, you may only need to tap back once to go to the Settings page.
A new item shows up titled Developer Options. Tap this new option to review all of the developer features available.
There’s a lot to go through here. The only option you need right now is USB Debugging.
Scroll down to this option and toggle it to on. A dialog appears informing you of the intended usage of USB debugging.
Granting USB debugging privileges is a potential security hole, so most devices have this turned off by default. Since you need to install apps over USB using Android Studio, you’ll need to turn this on.
When you’re ready, tap OK and the USB Debugging toggle is enabled.
Another pop will appear, asking to confirm your computer’s RSA key fingerprint.
This is another step Android takes to make sure no unauthorized computers can access your device data.
Since you’ve just enabled USB Debugging, it’s safe to assume this is your computer accessing the device. Click Allow, you can also check Always allows from this computer if you don’t want to give your computer permission every time you connect your device.
Congratulations, your device is now set up for development.
Running the app
It’s time to run Timefighter! Along the top of Android Studio, there’s a button that looks like a green Play button, next to a dropdown with the name of your device:
The dropdown allows you to select a device to run Timefighter on.
Depending on what device you setup earlier in the chapter, your physical device’s name or the name of your AVD will appear.
So long as a device is selected and appears in the closed dropdown button, it doesn’t matter which device is used.
With the device selected, click the Run App button. Android Studio begins building Timefighter and installs the app onto your device. You can see this happening at the bottom of Android Studio.
When Android Studio is finished building and installing, Timefighter will appear on your device:
Excellent, you just built your first Android App! Now it’s time to make it a little more personal.
Back in Android Studio, in the Project navigator, open the file in app ‣ res ‣ layout ‣ activity_main.xml.
Don’t worry too much about what this screen shows right now. For now, click the Code tab in the top right of the screen.
Don’t worry too much about what you see in this file. Right now, all you need to know is that it represents the app screen that appears on your device. You’ll learn more about this in the next chapter.
Inside the TextView tag, there’s a property called
android:text with the value of
"Hello World!". Update the value so TimeFighter says hello to you.
To run your app again with the new text, click the Run App button again:
Note: You may notice the Run App button has changed from a green play button to a green repeat button. Don’t worry about this for now. We’ll explain what’s going on in later chapters.
Installing new versions of Android Studio
Note: This book assumes the most current version of Android is Android 11, known to developers as API version 30. However, it’s entirely possible that a new version exists. That said, you may want to install the latest versions of Android Studio and the Android SDK.
Google has decoupled Android Studio from versions of Android. This means you can build apps in Android Studio with any version of the Android operating system, including any future versions of the Android SDK.
Android Studio does its best to prompt you when new versions of either Android Studio or the Android SDK are available. But, you don’t have to wait on Android Studio to update.
In the Android Studio menu, you can select Check for Updates, which gives you a dialog showing the items you can update. It also lets you know if things are already up-to-date.
If you want to download a newer — or even an older — version of Android SDK, select Preferences… from the same menu.
Note: On a windows machine, click File then, select Settings.
In the Preferences dialog, drill down through the menu items in the tree to Appearance & Behavior ‣ System Settings ‣ Android SDK.
In this window, there are two noteworthy tabs: SDK Platforms and SDK Tools. In SDK Platforms, you’ll see a list of the available Android SDKs.
Check the box next to one or more of these SDKs, and then click OK; this will install the selected SDKs.
In SDK Tools, you’ll see a list of the available build tools that Android Studio and your app can access.
Check the box next to one or more of the tools, and click OK; this will install that tool. You’ll learn more about these tools later.
Setting up an environment is often the first and most important step when diving into a new technology. To recap this chapter, you:
Learned how to download and setup Android Studio on your machine.
Created a new project for your first app.
Learned how to setup an Android Virtual Device and setup your own Android device for development.
Changed the text that appears on the screen to say hello to you.
Learned how to update Android Studio and install new versions of the tools / SDKs it relies on.
Where to go from here?
Well done getting your first app up and running! This is just the beginning. The next few chapters in this section will teach you even more about the basics of Android development. As you work through the chapters in this book, you’ll end up with a full-featured app.