Android Tutorial: The Application Structure

Overview

Now, after the little excitement of having just run your first Android application, let’s go back to Android Studio and take a look at the structure of an Android application. Figure 1.1 shows the left treeview that contains the project components. We recommend for the previous tutorial Android Tutorial: Creating An Application.

The application structure

Figure 1.1: The application structure

There are two main nodes in the Project window in Android Studio, app and Gradle Scripts. The app node contains all the components in the application. The Gradle Scripts node contains the Gradle build scripts used by Android Studio to build your project. I will not discuss these scripts, but it would be a good idea for you to get familiar with Gradle.

There are three nodes under the app node:

  • manifests. Contains an AndroidManifest.xml file that describes your application. It will be explained in more detail in the next section “The Android Manifest.”
  • java. Contains all Java application and test classes.
  • res. Contains resource files. Underneath this directory are these directories: drawable (containing images for various screen resolutions), layout (containing layout files), menu (containing menu files) and values(containing string and other values).

Not visible from inside Android Studio is a generated Java class named R, which can be found in the app/build/generated/source directory of the project. R contains nested classes that in turn contain all the resource IDs for all your resources. Every time you add, change or delete a resource, R is re-generated. For instance, if you add an image file named logo.png to the res/drawable directory, Android Studio will generate a field named logo under the drawable class, a nested class in R.

The purpose of having R is so that you can refer to a resource in your code. For instance, you can refer to the logo.png image file with R.drawable.logo.

The Android Manifest

Every Android application must have a manifest file called AndroidManifest.xml file that describes the application. Listing 1.1 shows a sample manifest file.

Listing 1.1: A sample manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.firstapp" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.firstapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

A manifest file is an XML document with manifest as the root element. The package attribute of the manifest element specifies a unique identifier for the application. Android tools will also use this information to generate appropriate Java classes that are used from the Java source you write.

Under <manifest> is an application element that describes the application. Among others, it contains one or more activity elements that describe activities in your app. An application typically has a main activity that serves as the entry point to the application. The name attribute of an activity element specifies an activity class. It can be a fully qualified name or just the class name. If it is the latter, the class is assumed to be in the package specified by the package attribute of the manifest element. In other words, the name attribute of the above activity element can be written as one of the following:

android:name="MainActivity"
android:name=".MainActivity"

You can reference a resource from your manifest file (and other XML files in the project) using this format:

@resourceType/name

For example, these are some of the attributes of the application element in Listing 1.1:

android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"

The first attribute, android:icon, refers to a drawable named ic_launcher. If you browse the project in Android Studio, you can find an ic_launcher.png file under res/drawable.

The second attribute, android:label, refers to a string resource called app_name. All string resources are located in the strings.xml file under res/values.

Finally, the third attribute, android:theme, references a style named AppTheme. All styles are defined in the styles.xml file under res/values. Styles and themes are discussed in another tutorial, “Styles and Themes.”

There are other elements that may appear in the Android manifest and you will learn to use many of them in this book. You can find the complete list of elements here:

http://developer.android.com/guide/topics/manifest/manifest-element.html

The Android SDK Manager

When you install Android Studio, the setup program also downloads the latest version of the Android SDK. You can manage the packages in the Android SDK using the Android SDK Manager.

To launch the Android SDK Manager, in Android Studio click Tools > Android > SDK Manager. Alternatively, click the SDK Manager button on the toolbar.

The SDK Manager window is shown in Figure 1.2.

The Android SDK Manager window

Figure 1.2: The Android SDK Manager window

In the Android SDK Manager, you can download other versions of the SDK or delete components you do not need.

Using Java 8

By default, Android Studio can compile sources with Java 7 language syntax. However, you can use Java 8 language features, even though Java 8 is not yet officially supported. It goes without saying that you need JDK 8 or later to use the higher language level. Also, even though you can use the Java 8 language features, you still cannot use the libraries that come with Java 8, such as the new Date Time API or the Stream API.

If you really want to use Java 8 to write Android applications, here is how you can change the Java language level from 7 to 8 in Android Studio.

1. Expand the Gradle Scripts node on the Project view. You will see two build.gradle nodes on the list. Double-click the second build file to open it. You will see something like this:

android {
     compileSdkVersion 21
     buildToolsVersion "19.1.0"

     defaultConfig {
       ...
     }
     buildTypes {
       ...
     }
}


2. Add the line in bold to the build file to change the language level to 7.

android {
        compileSdkVersion 21
        buildToolsVersion "19.1.0"

        defaultConfig {
            ...
        }
        buildTypes {
            ...
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }


As changing the language level adds complexity to a project, this book will stick with Java 7.

Opening A Existing Project in Android Studio

You can download the Android Studio projects accompanying this book from the publisher’s website. To open a project, select File > Open and browse to the application directory. Figure 1.3 shows how the Open File or Project window looks like.

Opening a project

Figure 1.3: Opening an existing project

Summary

This tutorial discusses how to create your first application. You also learned how to create a virtual device so you can test your app on multiple devices without or with physical devices.

Comments

Android Tutorial: Environment Setup - Digital Juices

[…] Then, follow the setup wizard to install the SDK. Next Article: Android Tutorial: The Application Structure […]

Leave a Reply

Your email address will not be published.