TABLE OF CONTENTS (HIDE)

Android SDK

How to Install and Get Started

Introduction

Android Robot

Android is an Operating System for mobile devices developed by Google, which is built upon Linux kernel. Android competes with Apple's iOS (for iPhone/iPad), RIM's Blackberry, Microsoft's Windows Phone, Symbian OS, and many other proprietary mobile OSes.

The latest Android 5.0 supports Phone/Tablet, TV, Wear (watch and glass) and Automobile.

Android Platform

Android is based on Linux with a set of native core C/C++ libraries. Android applications are written in Java. However, they run on Android's own Java Virtual Machine, called Dalvik Virtual Machine (DVM) (instead of JDK's JVM) which is optimized to operate on the mobile devices.

Android Stack

The mother site for Android is http://www.android.com. For developers, visit http://developer.android.com to download the SDK, Android Training, API Guides and API documentation.

How to Install Android Software Development Kit (SDK) with Android Studio IDE

Installing all the necessary software needed for Android programming takes times! From 30 minutes to n hours to forever - depending on your luck and your PC (You probably need a fairly decent PC to run the Android emulator)!

Step 0: Pre-Installation Check List
  1. Before installing Android SDK, you need to install Java Development Kit (JDK). Read "How to install JDK". Ensure that your JDK is at or above 1.7. You can check your JDK version with command "javac -version".
  2. Read the "Android Training" @ Android Developers (http://developer.android.com). There are three main menus: "Design", "Develop", and "Distribute". Choose "Develop", you can find the Android "Training", "API Guides", "Reference" and "Tools". For beginners, browse through the "Training".
Step 1: Install Android Studio IDE / SDK Bundle
(For Windows)
  1. Check the system requirements @ http://developer.android.com/sdk/index.html#Requirements.
  2. Goto "Android Developer" @ http://developer.android.com/index.html ⇒ Select "Get the SDK" ⇒ "Download Android Studio For Windows", e.g., android-studio-bundle-135.xxx-windows.exe (817MB).
  3. Check that environment variable JAVA_HOME is set to the JDK installation directory via command "set JAVA_HOME". Otherwise, set the variable via "Control Panel". Check the detail steps HERE.
  4. Run the downloaded installation file.
(For Mac OS X)
  1. Check the system requirements @ http://developer.android.com/sdk/index.html#Requirements.
  2. Goto "Android Developer" @ http://developer.android.com/index.html ⇒ Select "Get the SDK" ⇒ "Download Android Studio For Mac". E.g., android-studio-ide-135.xxx-mac.dmg (261MB).
  3. Run the downloaded .dmg installation file.
  4. Drag and drop Android Studio into the Applications folder.
    Note: If you see a warning that says the package is damaged and should be moved to the trash, go to System Preferences ⇒ Security & Privacy ⇒ under Allow applications downloaded from ⇒ select Anywhere. Then run again.
Step 2: Post-Installation - Adding SDK Packages

Adding too many SDK packages (especially the so-called system-images) will take an extremely LONG time, especially if everyone is downloading and jamming up the network. The system-images also take up a lot of disk space - more than 10 GB per API level!!! For our toy project, we only need a small set of SDK packages.

(For Windows)
  1. Launch Android Studio. Follow the on-screen instructions to complete the setup.
  2. You do NOT need any additional SDK package. The 800MB installer provides the minimum packages for our toy project. Proceed to the next section to write your Hello-world program.
    The SDK are installed under "C:\Users\your-username\AppData\Local\Android\sdk" for Windows ("appData" is a hidden directory).
(For Mac OS X)
  1. Launch Android Studio. Follow the on-screen instructions to complete the setup.
  2. Select "Configure" ⇒ SDK Manager.
  3. When you open the SDK Manager for the first time, several packages are selected by default. UNCHECK ALL of them (otherwise, you will need 3 hours and 10GB of disk).
  4. Check that these packages are "installed". Otherwise, check them to install:
    1. Under "Tools":
      1. "Android SDK Tools"
      2. "Android SDK Platform-tools"
      3. "Android SDK Build-tools 21.1.2" (Don't use API 22, which just appeared last week and is buggy!)
    2. Under "Android 5.0.1 (API 21) (Again, don't use API 22 yet):
      1. "SDK Platform"
      2. At least one "System Image". If anyone of the "xxxxxx System Images" is already installed, okay. Otherwise, check "Google APIs" and "Google APIs Intel x86 Atom System Image" (which seems to be the fastest a few I tried).
        [Notes: (1) System Image is huge (>1GB each). (2) Android devices could be built on many different hardware processors, e.g., Intel x86, ARMv5, and ARMv7. Android SDK provides "emulator" for you to test your app on ALL hardware, by building a System-Image for EACH of the hardware. Our toy project does not need to be simulated on ALL hardware but just anyone of the hardware!]
    3. Under "Extras":
      1. "Android Support Repository"
      2. "Android Support Library"
      3. "Google Repository"
      4. "Google USB Driver"
      5. "Intel x86 Emulator Accelerator (HAXM Installer)"
  5. Install.
    This step is SLOW! very SLOW! The SDK will be installed under "~/Library/Android/sdk" for Mac OS X.

Write your First Android App

Android apps are written in Java, and use XML extensively. I shall assume that you have basic knowledge of Java programming and XML.

Take note that Android emulator is slow - VERY VERY VERY SLOW!!! Be Patient!!!

Hello-world

Step 0: Read

Goto "Android Training" @ http://developer.android.com/training/index.html. Read "Get Started", "Building your first app".

Step 1: Create a New Android Project
  1. Launch Android Studio.
  2. Choose "Start a new Android Studio Project".
  3. In "Create New Project" dialog ⇒ Set "Application Name" to "Hello Android" (which is the title in your app menu) ⇒ Set your "Company Domain" to "example.com" ⇒ Take note of your "Project Location" ⇒ Next.
  4. In "Target Android Devices" dialog ⇒ Check "Phone and Tablet" ⇒ For minimum SDK, choose "API 14: Android 4.0 (IceCreamSandwich)" ⇒ Next.
  5. In "Add an activity to Mobile" dialog ⇒ Select "Blank Activity" ⇒ Next.
  6. In "Customize the Activity" dialog ⇒ Set "Activity Name" to "MainActivity" ⇒ Set "Layout Name" to "activity_main" ⇒ Set "Title" to "MainActivity" ⇒ Set "Menu Resource Name" to "menu_main" (These are actually the default values) ⇒ Finish.
  7. Be patient! It could take a while to set up the "first" app. The app appears after "Indexing..." completes. By default, a hello-world app is created.

Problem & Error Notes:

  1. If error "missing style..." occurs, select "File" ⇒ "Invalidate Caches / Restart...".
  2. If error "Rendering Problems: Exception raised during rendering: action bar" occurs, under the "activity_main.xml" panel, look for the android icon "2x" (e.g., "22"), pick a lower API Level. Check the problem persists, check the SDK packages installed under Android X.X (highest number).
Step 2: Setup Emulator (aka Android Virtual Device (AVD))

To run your Android app under the emulator, you need to first create an Android Virtual Devices (AVD). An AVD models a specific device. You can create AVDs to emulate different android devices (e.g., phone/tablet, android version, screen size, and etc.).

  1. In Android studio, select "Tools" ⇒ Android ⇒ AVD Manager.
  2. Click "Create Virtual Device".
  3. In "Select Hardware" dialog ⇒ Choose a device.
    For the purpose of these exercises, choose the "smallest" device, such as "2.7" QVGA" for fast operation (You can try the bigger devices later) ⇒ Next.
  4. In "System Image" ⇒ Use the defaults ⇒ Next.
  5. In "Android Virtual Device (AVD) ⇒ Change "AVD Name" to "QVGA" ⇒ Finish.
  6. Click the "Run" button (under the "Actions" column of the AVD just created) to launch the emulator. It is SLOW to launch the emulator. Check the "Event Log" for the progress.
    Once the emulator is launched, DO NOT CLOSE the emulator, as it is SLOW to launch again.
  7. Unlock the device. Play around with the emulator. You may use these shortcut keys for the device's buttons, if the buttons are not available in the emulator:
    • "Home" button: HOME key.
    • "Back" button: ESC key.
    • "LEFT Soft-key / Menu / Setting" button: F2 (or PageUp).
    • "Right Soft-key / Star" button: Shift+F2 (or PageDown).
    • More: google "Android Studio Emulator Shortcut keys".
  8. You can close the "AVD Manager".

Problem & Error Notes:

  1. If you encountered error "HAX is not working..." ⇒ Open "SDK Manager" ⇒ Check if "Extras / Intel x86 Emulator Accelerator (HAXM Installer)" is installed ⇒ Place your cursor under "Intel x86 Emulator Accelerator (HAXM Installer)" to show the location of the downloaded installer (by default, "C:\Users\your-username\AppData\Local\Android\Sdk\extras\intel\Hardware_Accelerated_Execution_Manager" for Windows or "~/Library/Android/sdk/extras/intel/Hardware_Accelerated_Execution_Manager" for Mac OS X) ⇒ Goto the the directory and run "intelhaxm-android.exe" to install HAXM ⇒ Be patient! The installer may take a while to launch ⇒ Follow the screen instructions to complete the setup.
    Take note that: (a) In Wiindows, the "AppData" directory is hidden. You need to unhide via "Control Panel" ⇒ "Folder Options" ⇒ Check "Show hidden files, folders, and drives". (b) In Mac OS X, the "Library" folder is hidden. You can unhide via "Finder" ⇒ Go ⇒ Home ⇒ Settings ⇒ Show View Option.
    If the problem persists, remove and then re-install.
  2. If you encountered error "Intel virtualization technology (VT-x) is not turned on ⇒ Check your BIOS setting to ensure that "Intel virtualization technology" is enabled. Some antivirus options may also prevent HAXM installation. See http://stackoverflow.com/questions/21635504/error-during-installing-haxm-vt-x-not-working.
  3. If you have problem creating AVD via "AVD Manager" (On Mac OS X having error "Studio quit unexpectedly"), open the AVD manager via command line as follows:
    // For Mac OS X
    $ cd ~/Library/Android/sdk/tools
    $ ./android avd
     
    // For Windows
    > cd C:\Users\your-username\AppData\Local\Android\sdk\tools
    > android avd
    
Step 3: Run the Android App On Emulator
  1. In Android Studio, select your project and click the "Run" button from the toolbar (or menu "Run" ⇒ "Run app").
  2. In "Choose Device" dialog:
    • If the emulator already started, under "Choose a running device" ⇒ Select the emulator ⇒ OK.
    • If the emulator has NOT been started, check "Launch emulator" and select the AVD created earlier ⇒ OK.
    Be patient! It takes a few MINUTES to fire up the emulator, if the emulator is not already launched.
  3. Unlock the device. You app shall be launched automatically. Otherwise, find your app on the app menu and Launch it. DO NOT CLOSE the emulator, as it really takes a long time to start. You could always re-run or run new app on the same emulator.

Problem & Error Notes:

  1. If you encounter problem using the emulator, you could run your app in the real device (see below).
Step 5: Run the Android App on Real Devices

Reference: "Running Your App", "Run on Real Device" @ http://developer.android.com/training/basics/firstapp/running-app.html.

To run the Android app on a real device (Android Phone or Android Tablet):

  1. Connect the real device to your computer. Make sure that you have the "USB Driver" for your device installed on your computer. You can find the "Google USB Driver" @ http://developer.android.com/sdk/win-usb.html, and Google's certified "OEM USB Drivers" @ http://developer.android.com/tools/extras/oem-usb.html. If you device is not certified there, good luck! It took me many hours to find a compatible driver for my cheap Android Tablet.
  2. Enable "USB Debugging" mode on your real device:
    (On Android 3.2 and older) From "Settings" ⇒ "Applications" ⇒ "Development" ⇒ Check "USB Debugging". This allows Android SDK to transfer data between your computer and your device. Also enable "Unknown source" from "Applications". This allows applications from unknown sources to be installed on the device.
    (On Android 4.0 and newer) From "Settings" ⇒ "Developer options" ⇒ Check "USB Debugging".
    (On Android 4.2 and newer) Need to enable "Developer options" via "Settings" ⇒ About Phone ⇒ tap "Build number" seven (7) times until "Developer Mode" is displayed. Return to the previous screen to find "Developer options".
  3. You shall see the message "USB Debugging Connected" when you plugs the USB cable into your computer.
  4. From Android Studio, select the project ⇒ Click "Run" from the toolbar.
  5. In the "Choose Device" dialog ⇒ Under "Choose a running device" ⇒ Select your device.

Notes: To delete a project, select "File" ⇒ "Close Project" ⇒ On the "Recent Projects" ⇒ Press "Delete" key on the project.

Hello-world "by Coding"

Let's continue from the previous example.

MainActivity.java

Expand the "app" node. Expand the "java" node. Expand the "com.example.helloandroid" package node. Open the "MainActivity.java" (which actually has been opened). Modify the onCreate() method as follows. Do not touch the rest of the methods.

package ......;
 
import ......;
import android.widget.TextView;
 
public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textView = new TextView(this);   // Construct a TextView UI component
        textView.setText("Hello, from my code!"); // Set the text message for TextView
        setContentView(textView);  // this Activity sets its content to the TextView
    }
    
    // Do not touch the rest of the methods
    ......
    ......
}

Run the application on the emulator by clicking the "Run" button on the toolbar. You shall see the message "Hello, from my code!" displayed on the emulator.

Dissecting the "MainActivity.java" - Application, Activity & View

An Android application could have one or more Activity.

An Activity, which usually has a screen, is a single, focused thing that the user can interact with the application (hence called activity). The MainActivity extends the android.app.ActionBarActivity class, and overrides the onCreate() method. The onCreate() is a call-back method, which is called by the Android system when the activity is launched.

A View is a UI component (or widget, or control). We construct a TextView (which is a subclass View for showing a text message), and set its text. We then set the content-view of the MainActivity screen to this TextView.

Android Application Descriptor File - "AndroidManifest.xml"

Each Android application has a manifest file named AndroidManifest.xml in under "app ⇒ manifests". It describes the Android app.

For example, our "Hello Android" application, with an activity MainActivity, has the following manifest (generated automatically by the Android Studio when the project was created):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloandroid" >
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>
  • The <manifest> element specifies the package name.
  • The <manifest> contains one <application> element.
  • The <application> element specifies the icon, label (the application's title) and theme of this application. It contains one ore more <activity> elements.
  • This application has one activity. The <activity> element declares its program name ("MainActivity" in default package); and label (the activity's screen title). It may contain <intent-filter>.
  • The <intent-filter> declares that this activity is the entry point (android.intent.action.MAIN) of the application. This activity is to be added to the application launcher (android.intent.category.LAUNCHER).

Hello-world "using XML Layout"

Instead of writing Java codes to create the user interface (UI) (as in the above example using a TextView component). It is more flexible and therefore recommended to layout your UI components via a descriptive XML layout file. In this way, you don't need to hardcode the views, and you can easily modify the look and feel of the application by editing the XML markups. The Java codes can therefore focus on the business logic. (This is similar to the Model-View-Control (MVC) framework used in web applications where the views are written in JSPs using markups and the controls in Servlets, which are clearly separated. This is also similar to client-side programming where HTML is used for contents, CSS for presentation and JavaScript for programming logic. Again, views, contents and programs are clearly separated).

To improve the performance, the XML files are compiled into binary using the Android Asset Packaging Tool (aapt). The devices store them as binary, and the file is read in binary, instead of converting back to XML, for efficiency.

Let's rewrite our hello-world to use XML layout.

Step 1: Create a New Android Application

Close the previous project, via "File" ⇒ Close Project (Always CLOSE the previous project before starting a new project).

Create a new Android project with application name "Hello Android XML", domain name "example.com". Select "Phone and Tablet". Create a "BlankActivity" with "Activity Name" of "MainActivity", "Layout Name" of "activity_main", "Title" name of "MainActivity", and "Menu Resource Name" of "menu_main".

Step 2: Define the Layout in XML - "res\layout\activity_main.xml"

Expand the "app", "res (resource)", "layout" node. Open the "activity_main.xml" (which is actually already opened). Android Studio provides two views for this XML file: "Design (or Graphical)" and "Text (or XML)" - selectable at the bottom of the editor panel. Select the "Text" mode and study the codes:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

The activity_main.xml file declares a TextView that holds a text string. Instead of hardcoding the string content, a string reference (variable) called @string/hello_world is used, with the actual string coded in res/values/strings.xml. This approach is particular useful to support internationalization (i18n), as you can customize different strings for different locales (e.g., English/French/Chinese).

This activity's uses a "relative-layout", where its components are arranged relative to each other. It has width and height matching its parent ("match_parent").

The screen contains a TextView component, with its text obtained from the string reference "@string/hello_world". The TextView component has width and height big enough to hold its content ("wrap_content").

Step 3: Defining String References and Values - "res\values\string.xml"

Expand res/values node. Open strings.xml, and study the XML codes (click on string.xml view):

<resources>
    <string name="app_name">My Application</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
</resources>

This "string.xml" defines these string references and values:

  • A string reference "hello_world" contains value of "Hello world!". Change the value to "Hello, from XML!".
  • A string reference "app_name" contains the application's name, that you entered when you created the project. This reference is used in "AndroidManifest.xml".
Step 4: The Activity - "MainActivity.java"

Next, study the "MainActitivy.java" (under app/java/com.example.helloandroidxml), as follows:

package ......;
 
import ......;
 
public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    ......
    ......
}

The "MainActivity" sets its content-view to "R.layout.activity_main", which is mapped to the XML layout file "res\layout\activity_main.xml" that we have studied earlier.

Step 5: Run the App

Run the application. You shall see the new string "Hello, from XML!" displayed on the emulator.

Next?

Continue with the "Android Training" @ http://developer.android.com/training/index.html.

Read the Android "API Guides" @ http://developer.android.com/guide/components/index.html, and Demos.

REFERENCES & RESOURCES

  1. Android SDK @ http://developer.android.com/sdk/index.html.
  2. "Android Training" @ http://developer.android.com/training/index.html.
  3. Android "API Guides" @ http://developer.android.com/guide/components/index.html.
Link to Android's References and Resources