Section 1: Installing the Android SDK
Install JDK (if necessary)
- 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.
- 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
- Note: You will not need to create an Oracle account if you download it from here: http://www.oracle.com/technetwork/java/javase/downloads/jdk6u38-downloads-1877406.html
- Once the JDK downloads, double-click to run the .exe and follow the prompts on screen.
- After installing Java, 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
- 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.
- Download the SDK Tools only from http://developer.android.com/sdk/index.html#ExistingIDE
- Install the ADT Plug-in from within Eclipse following the instructions here: http://developer.android.com/sdk/installing/installing-adt.html
- When Eclipse launches, select a workspace. Your Android projects will be created in this directory by default.
Android in Eclipse
Creating a New Project
- 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.
- 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.
- Open the AVD Manager in Eclipse by clicking the toolbar button (a phone icon) or go to Window > Android Virtual Device Manager.
- Click New...
- 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.
- 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.
- 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:
- 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.
- 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).
- Verify that your device is visible in Eclipse: Window > Show View > Other > Android > Devices.
- 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:
- Create a file called .profile in your home directory (if it doesn't already exist).
- Add these two lines, replacing /path/to/sdk below with the location of the SDK on your machine.
- Save the file. If you currently have Terminal open, close and re-open it.
- Open the Environmental Variables dialog.
- Add this to the end of the PATH value: ;c:\path\to\sdk\tools;c:\path\to\sdk\platform-tools
- Close and re-open the Command Prompt.
In order to build from the command line, you must use a build tool like Apache Ant or Maven.
- Download Ant here: http://ant.apache.org/bindownload.cgi
- Extract the zip file to a location on your computer such as c:\ or Program Files
- Update your machine's environnmental variables as outlined here (there are instructions for each OS): http://ant.apache.org/manual/install.html#windows
- Close and re-open the command prompt.
- 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> \
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> \
for example: android create avd -n MyAVD16 -t 5
- Note: To view the list of available targets, type "android list targets"
Once Ant is installed, build and run Hello World:
- cd into the directory of your Hello World project.
- 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
- Type "android avd". This will launch the AVD Manager. Select the AVD you want to use and click Start.
- When the AVD is ready, type "avd install bin\HelloWorld-debug.apk" to run the app.