Section 1

From CS160 Spring 2014
Jump to: navigation, search

Section Slides

File:Brittany Section1.pdf

File:Steve Section 1.pdf

File:Eric Section 1.pdf

Some information on Installing the Android SDK

Install JDK (if necessary)

  1. Android development requires the Java Development Kit (not just JRE). You may already have the JDK -- Mac OS X 10.6 (Snow Leopard) and later will have it installed by default.
  2. If you do not have the JDK, download it from Oracle.com. We recommend Java SE 6 - 7 may work but many phones do not support it. http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6-419409.html
  3. Once the JDK downloads, double-click to run the .exe and follow the prompts on screen.
  4. After installing Java, if you are on Windows, you may want to include the java executable in your system's PATH variable. This lets you access java from the windows command line. To do so, edit your machine's environmental variables:
    • Add a variable called JAVA_HOME that points to the JDK, e.g. c:\Progra~1\Java\jdk1.6.0_37
    • Add Java to the PATH variable: %JAVA_HOME%\bin

Install Eclipse and Android SDK

  1. In this course we will be developing applications with Eclipse. How to obtain the Android SDK depends on whether you already have Eclipse.
    If you do not have Eclipse the easiest way to jump into Android development is with the ADT (Android Developer Tools) Bundle.
    • Download the bundle that matches your OS from here: http://developer.android.com/sdk/index.html
    • When the download completes, extract it to somewhere like Program Files, c:\, or your Home directory
    • Open the extracted folder and then open the Eclipse folder. Double-click Eclipse to run the application.

    If you already have Eclipse, download the SDK and the ADT plug-in separately.

  2. When Eclipse launches, select a workspace. Your Android projects will be created in this directory by default.


Android in Eclipse

Creating a New Project

  1. The first time you run ADT Eclipse you will see a welcome screen. Go ahead and close that, then click File > New > Android Application Project.
    Note: If Android Application Project is not a top-level option in the menu, click Project... to open the New Project dialog and then select Android > Android Application Project.
  2. Follow the prompts in the wizard to set up your project:
    • Type a name for your application, such as "Hello World". This is the name that will appear to the user in the Launcher and elsewhere.
    • The project name will be auto-completed to remove spaces (e.g., HelloWorld)
    • The package name should be in the format edu.berkeley.cs160.yourname.helloworld
    • For this simple test project, the default SDK version options are fine.
    • The next two screens are optional for this project and may be left at their defaults (but feel free to play around with creating a unique Launcher icon!)
    • Click Next to create a Blank Activity, then give it a name (e.g., HelloActivity) and click Finish. You will see the newly created project in Eclipse's Package Explorer (if not visible, click Window > Show View > Package Explorer).

Running Hello Android

You may run and debug Android projects in one of two ways: in an emulator or on an actual device.
To run it on an emulator:
The first step is to create the emulator, also called an AVD (Android Virtual Device). AVDs are created and managed from the AVD Manager.

  1. Open the AVD Manager in Eclipse by clicking the toolbar button (a phone icon) or go to Window > Android Virtual Device Manager.
  2. Click New...
  3. Configure the AVD as follows:
    • Provide a name, such as MyAVD17. The 17 indicates that this AVD will target Android 4.2 (API level 17) and will help differentiate it when you have many AVDs for different devices.
    • Select the Nexus 7 as your target device/API version.
    • Leave the other options at their default values.
  4. Click OK. Then back in the AVD Manager, click Start to start the AVD.
    • Note: Booting up an emulator is slow; it can take as long as 5 minutes to launch. When developing it's wise to leave the AVD open as long as you are working, there is no need to close and re-open it each time.
  5. When the AVD is running, click the Run button in the Eclipse tool bar (green circle with white arrow).
    • Note: This actually has the same effect as step 4 if no AVDs are currently running. If you have more than one emulator or physical device available Eclipse will show the Android Device chooser first. You can also configure how Eclipse decides which device or AVD to use -- see this page for more information: http://developer.android.com/tools/building/building-eclipse.html#RunConfig


To run it on a device:

  1. First enable USB debugging. This is done through the Settings menu, although the location of the setting varies between Android versions:
    • On most devices running Android 3.2 or older, you can find the option under Settings > Applications > Development.
    • On Android 4.0 and newer, it's in Settings > Developer options.
      Note: On Android 4.2 and newer, Developer options is hidden by default. To make it available, go to Settings > About phone and tap Build number seven times. Return to the previous screen to find Developer options.
  2. Connect the device and make sure it is recognized by your development machine.
    • If using Windows, you may need to install drivers for the device that are obtained from the manufacturer. See this page for more information: http://developer.android.com/tools/extras/oem-usb.html
    • If the device is a Nexus 7 tablet, it will first be recognized as a Media Device (MTP) which is incorrect for development. Click the icon in the notification bar and change this to Camera (PTP).
  3. Verify that your device is visible in Eclipse: Window > Show View > Other > Android > Devices.
  4. Click the Run button (green circle with white arrow). If the device is visible, Eclipse will automatically run your project on it - though if you have more than one device or emulator, you will be presented with the Android Device Chooser first.


Android from the Command Line

Although ADT Eclipse provides an easy-to-use UI for developing and debugging apps, you can also perform all of the above steps from the command line.

Add Android Path

To run command line tools, you must have the path to the SDK in your environmental variables:

On OSX

  1. Create a file called .profile in your home directory (if it doesn't already exist).
  2. Add these two lines, replacing /path/to/sdk below with the location of the SDK on your machine.
    PATH=/path/to/sdk/tools:$PATH
    PATH=/path/to/sdk/platform-tools:$PATH
  3. Save the file. If you currently have Terminal open, close and re-open it.

On Windows

  1. Open the Environmental Variables dialog.
  2. Add this to the end of the PATH value: ;c:\path\to\sdk\tools;c:\path\to\sdk\platform-tools
  3. Close and re-open the Command Prompt.


Install Ant

In order to build from the command line, you must use a build tool like Apache Ant or Maven.

  1. Download Ant here: http://ant.apache.org/bindownload.cgi
  2. Extract the zip file to a location on your computer such as c:\ or Program Files
  3. Update your machine's environnmental variables as outlined here (there are instructions for each OS): http://ant.apache.org/manual/install.html#windows
  4. Close and re-open the command prompt.
  5. Verify ant is installed by typing ant -version


Create a New Project

To create a new project, use the android create project command with arguments formatted as follows:

android create project \
--target <target_ID> \
--name <your_project_name> \
--path path/to/your/project \
--activity <your_activity_name> \
--package <your_package_namespace>

for example: android create project --target 17 --name HelloWorld --path HelloWorld --activity HelloActivity --package edu.berkeley.cs160.jstudent.helloandroid

Create an AVD

AVDs are created with the following command:

android create avd \
-n <avd_name> \
-t <target_ID>

for example: android create avd -n MyAVD16 -t 5

  • Note: To view the list of available targets, type "android list targets"


Run Project

Once Ant is installed, build and run Hello World:

  1. cd into the directory of your Hello World project.
  2. Type "ant debug". Ant will generate an HelloWorld-debug.apk file inside the \bin directory.
    • Note: .apk is the file extension of a compiled and packaged Android app
  3. Type "android avd". This will launch the AVD Manager. Select the AVD you want to use and click Start.
  4. When the AVD is ready, type "avd install bin\HelloWorld-debug.apk" to run the app.