This practical codelab is part of Unit 1: Get started in the Android Developer Fundamentals (Version 2) course. You will get the most value out of this course if you work through the codelabs in sequence.
In this practical you learn how to install Android Studio, the Android development environment. You also create and run your first Android app, Hello World, on an emulator and on a physical device.
You should be able to:
After you successfully install Android Studio, you will create, from a template, a new project for the Hello World app. This simple app displays the string "Hello World" on the screen of the Android virtual or physical device.
Here's what the finished app will look like:
Android Studio provides a complete integrated development environment (IDE) including an advanced code editor and a set of app templates. In addition, it contains tools for development, debugging, testing, and performance that make it faster and easier to develop apps. You can test your apps with a large range of preconfigured emulators or on your own mobile device, build production apps, and publish on the Google Play store.
Android Studio is available for computers running Windows or Linux, and for Macs running macOS. The newest OpenJDK (Java Development Kit) is bundled with Android Studio.
To get up and running with Android Studio, first check the system requirements to ensure that your system meets them. The installation is similar for all platforms. Any differences are noted below.
In this task, you will create an app that displays "Hello World" to verify that Android studio is correctly installed, and to learn the basics of developing with Android Studio.
Next
button.Hello World
for the Application name,Once you are happy with the settings click Finish
Android Studio creates a folder for your projects, and builds the project with Gradle (this may take a few moments).
Tip: See the Configure your build developer page for detailed information.
You may also see a "Tip of the day" message with keyboard shortcuts and other useful tips. Click Close to close the message.
The Android Studio editor appears. Follow these steps:
Code
, Split
and Design
buttons at the top right-hand corner allow you to change the display from XML, mixed XML and Screen design, and just design. Select Split
:MainActivity.java
tab to see the code editor as shown below.In this step, you will explore how the project is organized in Android Studio.
The Gradle build system in Android Studio makes it easy to include external binaries or other library modules to your build as dependencies.
When you first create an app project, the Project > Android pane appears with the Gradle Scripts folder expanded as shown below.
Follow these steps to explore the Gradle system:
buildscript
block to define the Gradle repositories and dependencies that are common to all modules in the project. When your dependency is something other than a local library or file tree, Gradle looks for the files in whichever online repositories are specified in the repositories block of this file. By default, new Android Studio projects declare JCenter and Google (which includes the Google Maven repository) as the repository locations:allprojects {
repositories {
google()
jcenter()
}
}
build.gradle
file, each module has a build.gradle
file of its own, which allows you to configure build settings for each specific module (the HelloWorld app has only one module). Configuring these build settings allows you to provide custom packaging options, such as additional build types and product flavors. You can also override settings in the AndroidManifest.xml
file or the top-level build.gradle
file. This file is most often the file to edit when changing app-level configurations, such as declaring dependencies
in the dependencies section. You can declare a library dependency using one of several different dependency configurations. Each dependency configuration provides Gradle different instructions about how to use the library. For example, the statement implementation fileTree(dir: 'libs', include: ['*.jar'])
adds a dependency of all ".jar" files inside the libs
directory.apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.1"
defaultConfig {
applicationId "uk.aston.helloworld"
minSdkVersion 18
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
All code and resources for the app are located within the app
and res
folders.
app
folder, the java
folder, and the uk.aston.helloworld
(your package name may be different) folder to see the MainActivity.java
file. Double-clicking the file opens it in the code editor.java
folder includes Java class files in three subfolders, as shown in the figure above. The uk.aston.helloworld
(or the domain name you have specified) folder contains all the files for an app package. The other two folders are used for testing and described in another lesson. For the Hello World app, there is only one package and it contains MainActivity.java
. The name of the first Activity
(screen) the user sees, which also initializes app-wide resources, is customarily called MainActivity (the file extension is omitted in the Project > Android pane).package uk.aston.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
res
folder and the layout
folder, and double-click the activity_main.xml
file to open it in the layout editor.res
folder holds resources, such as layouts, strings, and images. An Activity
is usually associated with a layout of UI views defined as an XML file. This file is usually named after its Activity
. Our layout file is called activity_main.xml
and it contains the following code:<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The manifests folder contains files that provide essential information about your app to the Android system, which the system must have before it can run any of the app's code.
manifests
folder.AndroidManifest.xml
file.AndroidManifest.xml
file describes all of the components of your Android app. All components for an app, such as each Activity
, must be declared in this XML file. In other course lessons you will modify this file to add features and feature permissions. For an introduction, see App Manifest Overview. Our Manifest contains the following code:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uk.aston.helloworld">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In this task, you will use the Android Virtual Device (AVD) manager to create a virtual device (also known as an emulator) that simulates the configuration for a particular type of Android device, and use that virtual device to run the app. Note that the Android Emulator has additional requirements beyond the basic system requirements for Android Studio.
Using the AVD Manager, you define the hardware characteristics of a device, its API level, storage, skin and other properties and save it as a virtual device. With virtual devices, you can test apps on different device configurations (such as tablets and phones) with different API levels, without having to use physical devices.
In order to run an emulator on your computer, you have to create a configuration that describes the virtual device.
Click the +Create Virtual Device. The Select Hardware window appears showing a list of pre configured hardware devices. For each device, the table provides a column for its diagonal display size (Size ), screen resolution in pixels (Resolution ), and pixel density (Density ).
There are many more versions available than shown in the Recommended tab. Look at the x86 Images and Other Images tabs to see them.
If a Download link is visible next to a system image you want to use, it is not installed yet. Click the link to start the download, and click Finish when it's done.
If you find that the emulator does not start correctly, try changing the Graphics option to Software - GLES 2.0 and the Boot Option to Cold boot.
In this task, you will finally run your Hello World app.
The emulator starts and boots just like a physical device. Depending on the speed of your computer, this may take a while. Your app builds, and once the emulator is ready, Android Studio will upload the app to the emulator and run it.
You should see the Hello World app as shown in the following figure.
Tip: When testing on a virtual device, it is a good practice to start it up once, at the very beginning of your session. You should not close it until you are done testing your app, so that your app doesn't have to go through the device startup process again. To close the virtual device, click the X button at the top of the emulator, choose Quit from the menu, or press Control-Q in Windows or Command-Q in macOS.
In this final task, you will run your app on a physical mobile device such as a phone or tablet. You should always test your apps on both virtual and physical devices.
What you need:
To let Android Studio communicate with your device, you must turn on USB Debugging on your Android device. This is enabled in the Developer options settings of your device.
On Android 4.2 and higher, the Developer options screen is hidden by default. To show developer options and enable USB Debugging:
Now you can connect your device and run the app from Android Studio.
Android Studio installs and runs the app on your device.
If your Android Studio does not recognize your device, try the following:
If your computer still does not find the device or declares it "unauthorized", follow these steps:
You may need to install the appropriate USB driver for your device. See the Using Hardware Devices documentation.
In this task you will change something about the app configuration in the build.gradle(Module:app)
file in order to learn how to make changes and synchronize them to your Android Studio project.
Follow these steps:
defaultConfig
block, change the value of minSdkVersion
to 20
as shown below (it was originally set to 18). This change will mean that devices running Android API 19 or lower will not be able to run this app.The code editor shows a notification bar at the top with the Sync Now link.
When you make changes to the build configuration files in a project, Android Studio requires that you sync the project files so that it can import the build configuration changes and run some checks to make sure the configuration won't create build errors.
To sync the project files, click Sync Now in the notification bar that appears when making a change (as shown in the previous figure), or click the Sync Project with Gradle Files icon in the toolbar.
When the Gradle synchronization is finished, the message Gradle build finished
appears in the bottom left corner of the Android Studio window.
For a deeper look into Gradle, check out the Build System Overview and Configuring Gradle Builds documentation.
In this task, you will add Log statements to your app, which display messages in the Logcat pane. Log
messages are a powerful debugging tool that you can use to check on values, execution paths, and report exceptions.
To see the Logcat pane, click the Logcat tab at the bottom of the Android Studio window as shown in the figure below.
In the figure above:
Log
statements to your app, Log
messages appear here.Log
level menu set to Verbose (the default), which shows all Log
messages. Other settings include Debug, Error, Info, and Warn.Log
statements in your app code display messages in the Logcat pane. For example:Log.d("MainActivity", "Hello World");
The parts of the message are:
Log
: The Log
class for sending log messages to the Logcat pane.d
: The DebugLog
level setting to filter log message display in the Logcat pane. Other log levels are e
for Error, w
for Warn, and i
for Info.Activity
from which the message originates. However, you can make this anything that is useful to you for debugging.By convention, log tags are defined as constants for the Activity
:
private static final String LOG_TAG = MainActivity.class.getSimpleName();
"Hello world"
: The second argument is the actual message that is displayed.Follow these steps to add your own Log
message to your Hello World app:
MainActivity
.android.util.Log
required for using Log
), choose File > Settings in Windows, or Android Studio > Preferences in macOS.onCreate()
method of MainActivity
, add the following statement:Log.d("MainActivity", "Hello World");
The onCreate()
method should now look like the following code:@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MainActivity", "Hello World");
}
The following message should appear in the Logcat pane:
07-13 18:08:03.573 29359-29359/uk.aston.helloworld D/MainActivity: Hello World
Challenge: Now that you are set up and familiar with the basic development workflow, do the following:
"Hello World"
greeting to "Happy Birthday to "
and the name of someone with a recent birthday.Log.e()
, that you can use for this purpose. Explore methods you can use to include an exception with a Log
message. Then, write code in your app to trigger and log an exception.build.gradle(Module:app)
file when you need to add new libraries to your project or change library versions.app
and res
folders. The java
folder includes activities, tests, and other components in Java source code. The res
folder holds resources, such as layouts, strings, and images.AndroidManifest.xml
file to add features components and permissions to your Android app. All components for an app, such as multiple activities, must be declared in this XML file.Log
statements to your app, which display messages in the Logcat pane as a basic tool for debugging.The related concept documentation is in 1.0: Introduction to Android and 1.1 Your first Android app.