Introduction to Android Studio

by Gisle Hannemyr

This chapter is an introduction to the Android Studio. It tells you how to locate information about Android Studio. It also introduces the basic components of Android Studio.

Table of contents

Introduction

The current version of Android Studio is ver. 4.0.1. (aug. 2020).

[TBA]

Android Studio comes with, and makes use of, Gradle. This is an open-source build automation tool that may be viewed as a replacement for GNU Make.

Projects will by default be saved in the directory named “AndroidStudioProjects” in your home directory.

Finding information about Android Studio

There exists many sources that provides documentation, tutorials and other information about Android Studio, including some well-written books. However, the most up to date information is on the web. The following links are the some starting points when looking for information about Android Studio on the web:

Installing Android Studio

First download the installer from, the Developers website.

The installer must be run as administrator, so make sure you have the admin credentials (Clipperz) before trying to run it.

When the installation is complete, you should check for any Android SDK Updates. Navigate to Tools » SDK Manager » Android SDK. Select the tab “SDK Tools”. The column with the heading “Status” will tell you about any available updates. If there are any updates, select and install them.

Source

Setting up the Android emulator

The Android emulator in Android Studio requires Hyper-V to create virtual machines. This software is not available on Windows 10 Home, only exists on Windows 10 Enterprise, Pro, or Education.

The Intel HAXM accellerator can be used instead. During installation of Android Studio this will be installed. In August 2020, on a Dell half tower running Windows 10 Pro, this works.

To add virtual devices, select “Open AVD Manager” and click “+ Create Virtual Device…”, for example “Pixel 2” (phone) and “Pixel C” (tablet), both with API 30. It will be downloaded and installed. You can then use it for testing instead of a physical device.

After you've istalled the virutal device, you may activate an app by pressing the “Run app” button in Android Studio. Getting it to start may take some time.

The camera lets you take a screenshot. It is usually saved as a file on your desktop (it will not be on the clipboard).

Troubleshooting

If you get the following error message when trying to install HAXM:

haxm_error.png

To use the intel HAXM, you need to have a CPU with virtualization support set up in BIOS:

Note that HP (the manufacturer of my laptop) does not recommend this. Changing the BIOS settings may have some uninteden side-effects. My recommendention is not to do this. Set up a physical device instead.

Setting up a physical device

tipYou may not have to install an OEM USB driver, but if this is not present, here is how:
Android: Install OEM USB drivers
Intel: Installing the Android Debug Bridge (ADB) USB Driver on a Windows Computer.
(The Windows 10 Windows and File Explorer is the icon hat looks like a folder in the toolbar. To launch the “Computer Management” panel, navigate to Computer » Manage.)

On Android 4.2 and higher, you must enable Developer options screen as follows:

  1. Open the Settings app.
  2. (Only on Android 8.0 or higher) Select System.
  3. On a pad, click the “General” tab
  4. Scroll to the bottom and select “About phone/device”.
  5. If developer options is not visible, scroll to near the bottom and tap Build number 7 times.
  6. Return to the previous screen (or just examine the left marhin) to find “Developer options” near the bottom on the left.

Tick "Stay awake",

Next, you should scroll down a little and enable "USB debugging". This allows Android Studio and other SDK tools to recognize your device when connected via USB, so you can use the debugger and other tools.

When you plug in the device in the USB-port, you may need to agree to USB debugging.

Troubleshoot device connection with the Connection Assistant

The Connection Assistant provides step-by-step instructions to help you set up and use a device over the ADB connection.

To start the assistant, choose Tools » Connection Assistant.

The Connection Assistant provides instructions, in-context controls, and a list of connected devices in a series of pages in the Assistant panel. Use the Next and Previous buttons at the bottom of the Assistant panel to work through the pages as needed:

Using Android Studio

Testing an app

To test an app, select Run » Debug 'app' and select the device to run on. It will take some time to get it ready, but there is a progress bar in the footer.

To stop a running app click on the “Stop” icon in the “app” menubar on the top right (it looks like a square) or press [Ctrl]+[F2].

astudio01.png
Location of the Compiler Error window.

The link to the Compiler Error window is in the left margin of the Build window. Refer to the screen shot on the left to see what icon to press.

Source: Build and run your app

 

 

 

 

 

Importing a project

To import a zipped up project, point to the zip-file containing all the modules of the project.

To import a project from a folder, navaigate to File » New » Import Project…. Locate project, select, and click “OK”.

Deleting a project

When you try to import a new version of an existing project, you may get the following error message:

Cannot import project
The project file already exists.

In order to resolve this, you should delete the outdated version of the project.

noteThe steps required has varied a lot over the lifetime of Android Studio. Make sure the recipe is for the version you use.

Deleting a project in Android Studio 3 requires you to select the project and navigate to Files » Project structure and select one of project's modules. Click on “-”. Repeat until everything has been deleted. However, this will not get rid of the files. You need to delete those in the file system.

This Youtube video explains the steps (the sound is horrible, but it was the best I could find).

Debugging

The Logcat window in Android Studio displays system messages, including debug messages added to your app with the Log class. To use it make sure you import the class:

import android.util.Log;

To use it:

Log.d("HelloWorld", "Debug message");
Log.d("HelloWorld", "Value: " + variable);

It creates a massive amount of output, but you can filter to only see the relevant messages.

Distributing an app

Android apps outside of the development environment exists in two formats: .aab (Android App Bundle) .apk (Android application PacKage). Both packages all the files of a project into a zipped package that allows easy distribution.

The .aab-format is a publishing format that may be uploaded to Google Play for signing. If an .aab-file is uploaded to an Android device, it will not run. The .apk-format is the packaging format that should be installed on a device.

AFAIK, you will not be able to run an unsigned .apk-file on a modern Android device. Installation will always fail with the error message “App not installed” and no further diagnostics.

To cut a long story short, make sure you create a key (as described below) and generate signed .apk-files.

To distribute the file outside of Google Play Store, you can send the .apk-file by email or make it available for download on your own website.

However, to be able to install it, the recipent must first alter the security settings and allow apps from unknown sources to be installed. The user do this (Android 5) by navigating Settings » General » Security and ticking “Unknown sources”.

Installation of the file will be blocked by Google Play Protect, and you will get the error message “App not installed”. It is suggested that you disable Google Play Protect, but I think that it is a bad idea, and besides, it does not work.

Note that if your signature is not recognized by Google Play Protect the user will get a warning when trying to install your app.

Source: app-signing.html.

Generate an upload key and keystore

If you don't already have an upload key, which is useful when opting in to App signing by Google Play, you can generate one using Android Studio as follows:

  1. Navigate to Build » Build » Generate Signed Bundle/APK.
  2. In the “Generate Signed Bundle or APK” dialog, select “Android App Bundle” or “APK” and click “Next”.
  3. Below the field for “Key store path”, click “Create new”.
  4. On the New Key Store window, provide the requested information for your keystore and key.
  5. Keystore:
    • Key store path: Select the file where your keystore should be created. It should typically live below your user account and be named something like keystore\a-upload-keystore.jks.
    • Press “Create new”.
  6. Keystore:
    • Password: Create and confirm a secure password for your keystore.
  7. New key store
    • Alias: Enter an identifying name for your key. For the upload key, you may want to call it “Upload”.
    • Password: Create and confirm a secure password for your key. This should be different from the password you chose for your keystore.
    • Validity (years): Set the length of time in years that your key will be valid. Your key should be valid for at least 25 years, so you can sign app updates with the same key through the lifespan of your app.
    • Certificate: Enter some information about yourself for your certificate. This information is not displayed in your app, but is included in your certificate as part of the .apk-file.
  8. Once you complete the form, click “OK”.
  9. If you would like to build and sign your app with your upload key, continue to the next section. If only want to generate the key and keystore, click “Cancel”.
  10. Once you complete the form, click “Finish”.

Sign your app with your key

If you already have an upload key, use it to sign your app. If instead your app is already signed and published to the Google Play store with an existing app signing key, use it to sign your app and make sure to encrypt and export it to opt your app in to app signing by Google Play. You can later generate a separate upload key and register your upload key’s public certificate with Google Play to sign and upload subsequent updates to your app.

To sign your app using Android Studio, and export an existing app signing key, follow these steps:

  1. If you don’t currently have the Generate Signed Bundle or APK dialog open, click Build » Generate Signed Bundle/APK.
  2. In the Generate Signed Bundle or APK dialog, select either Android App Bundle or APK and click Next.
  3. Select a module from the drop down.
  4. Specify the path to your keystore, the alias for your key, and enter the passwords for both. If you don’t have not yet prepared your upload keystore and key, first Generate an upload key and keystore and then return to complete this step.
  5. If you're signing an app bundle with an existing app signing key, and you’d like to later opt your app in to app signing by Google Play, check the box next to Export encrypted key and specify a path to save your signing key as an encrypted *.pepk file. You can then use your encrypted app signing key to opt in an existing app into app signing by Google Play.
  6. Click Next.
  7. In the next window (shown in figure 4), select a destination folder for your signed app, select the build type, choose the product flavor(s) if applicable.
  8. If you are building and signing an APK, you need to select which Signature Versions you want your app to support. To learn more, read APK Signature Scheme v2
  9. Click Finish.

Troubleshooting

Below is a record of problems encountered and how they were solved:

Missing aapt2:3.3.2-5309881

For legacy apps created prior to Android Studio ver. 3.2.1, the builder will produce this error message; “Could not find com.android.tools.build:aapt2:3.3.2-5309881”. To fix it, add google() in the root level build.gradle. I.e.:

buildscript {
    repositories {
        google()   //  <--here
        jcenter()
    }
 }

allprojects {
    repositories {
        google()   //  <-- here
        jcenter()
    }
}

Final word


Last update: 2019-01-03 [gh].