MEIC/MERC 2015/2016

Mobile and Ubiquitous Computing

Lab Guide 1

Introduction to Android

 


Objectives:

Material:


Exercise I – Hello World

Implement a Hello World application which renders the two activities shown in Figure 1. The left-hand side activity is shown when the app is started, and displays a textbox and a "Send" button. When the user types a string in the textbox and presses the button, the right-hand side activity is created, echoing the input string back to the user.

Figure 1: Activities implemented by the Hello World app.

1. Create an Android project

The first step to implement the app is to create an Android project:

Navigate to the project view (in the upper left panel) and explore the project directory tree. Describe the role of the following files (Hint: read "Building Your First App"):

2. Run the application

To test your app you can use the emulator or a real device.

Emulator: To run the application on the emulator, perform the following steps:

Real Device: If you have a real device and a USB cable, proceed as follows:

It is possible to manage multiple AVDs in Android Studio or in a command-line interface. To learn more about this topic, read "Managing Virtual Devices".

3. Debug the application

Add the following code line to the very end of method onCreate of the MainActivity class. Then re-deploy the app code and spot the message that gets displayed in the logcat panel.

    Log.d("MainActivity", "This is my first debugging message.");

Place a breakpoint anywhere in the application code (e.g., in the Log.d line), and execute the program in the debugger view. Explore the debugger functionalities by inspecting the state of the application and then resuming the process execution. While the app is stopped, it is possible to inspect the threads running on the emulator, resource utilization, etc.

4. Build a simple user interface

Implement the activity shown in the left-hand side of Figure 1. To this end, you must modify the default user interface (UI) layout in order to include a text field and a button. This will be done in XML (res/layout/activity_my.xml). It is possible to specify the layout of an activity using Android Studio's GUI, but we leave it out as an exercise.

In Android, as shown in Figure 2, the layout of an activity's UI consists of a hierarchical tree of elements, each of which is called View. The leaves of the tree correspond to single UI elements (e.g., buttons). Climbing upwards in the tree, these elements are aggregated into groups (ViewGroup), which in turn can be arranged into higher-level groups. The root of the tree represents the top level panel of the activity. A ViewGroup is also a View.

Figure 2: General representation of a UI layout tree.

Different ViewGroup elements specify different arrangements for its child views. For example, the view group named LinearLayout lays out child views in sequence, according to the way they appear in the XML. This sequence can be either vertical or horizontal orientation, as specified by the android:orientation attribute. For more information see "Layouts".

Implement the new layout of Hello World's main activity:

5. Start another activity

Complete your app by starting another activity when the user presses the "Send" button:

Answer the following questions:

Exercise II – Demystifying Gradle

To manage the build process of a project, Android uses Gradle, an open source building automation system akin to Ant. It is important to understand how Gradle works because it will help you to fix build-related problems more promptly and enable you to manage your projects from the command line.

1. Basic Gradle commands

To introduce Gradle, we first explore its most basic commands. First, download it and install it on a local directory. Note that, first you will use the standard Gradle distribution, and later the distribution that comes with Android Studio.

On a terminal window, type the following commands and learn what they do:

    gradle -v
    gradle -q help
    gradle -q tasks
    gradle -q properties

Create a folder for a hypothetical project (e.g., example1) and create a file named build.gradle. This file can be seen as Gradle's Makefile. Add the following contents to the file:

    task compileTask << { 
        System.out.println "compiling..." 
    }

Repeat the gradle -q tasks command, and spot the task defined in build.gradle. Then, run the task by executing:

    gradle -q compileTask

Modify build.gradle as follows:

    defaultTasks 'buildTask'

    task compileTask << {
      System.out.println "compiling..." 
    }

    task buildTask (dependsOn:compileTask) << {
      System.out.println "building..."
    }

Interpret the output of Gradle when issuing the following commands:

    gradle -q compileTask
    gradle -q buildTask
    gradle -q

As you may have guessed by now, tasks are the building blocks to manage a project. They do things like: compile, build, run tests, package, etc. However, you don't have to write all these tasks by hand. In fact, Gradle can be extended with plugins that define tasks for specific project types: Java plugin, WAR plugin, Android plugin, etc. Next, you will build a Java project using the Gradle Java plugin.

2. Simple Java project with Gradle Java plugin

To introduce the Gradle Java plugin, we will implement a simple Java library. This library implements a domain class called Quote for managing famous quotes. A quote contains an ID, the author of the quote, and the quote text.

Create a directory for the project called example2. Place file build.gradle in the project directory and file Quote.java in a project subdirectory src/main/java/pt/ulisboa/tecnico/cmov/quote.

Answer the following questions (Hint: refer to "Gradle Tutorial: Part 2: Java Projects"):

Modify the class to implement the following Main method:

    public static void main(String[] args) {
        System.out.println("Running the Quote application");
        Quote q = new Quote();
        q.setId(new Long(1));
        q.setWho("Julius Caesar");
        q.setWhat("I came, I saw, I conquered.");
        System.out.println(q);
    }

In order to execute this code from Gradle, we need to add an additional plugin. Modify build.gradle by adding the following code, and then execute "gradle run". Analyze what happened. Hint: read "Application Plugin".

    apply plugin:'application'
    mainClassName = "pt.ulisboa.tecnico.cmov.quote.Quote"

3. Multiple Java projects

Typically, a project is not a monolithic piece of software, but consists of multiple components, e.g., libraries or APIs. Gradle can gracefully handle complex projects by structuring them into sub-projects. Each sub-project is placed in a sub-directory and managed by a specific build.gradle file.

To understand how Gradle manages sub-projects, download and uncompress the project file example3.zip and answer the following questions (Hint: see "Gradle Tutorial: Part 3: Multiple Java Projects"):

4. Gradle and Android Studio

Android Studio leverages Gradle to manage its projects using the Gradle Android plugin. Refer to the Hello World project implemented in Exercise I. With the help of the tutorial "Gradle Tutorial: Part 6: Android Studio + Gradle", perform the following exercises:

At this point, it should be clear that there's no magic under the hood. For future reference, consider the following guides:

Exercise III – TODO list app

Implement a simple TODO list application consisting of a single activity. The activity must show two views arranged vertically: an EditText view, and a ListView view. The user types a string in the EditText view, and the ListView will be updated with the new string placed in the top of the list.