The recommended way to develop an Android application is to use Eclipse with the ADT plugin. The ADT plugin provides editing, building, debugging, and .apk packaging and signing functionality integrated right into the IDE.
However, if you'd rather develop your application in another IDE, such as IntelliJ, or in a basic editor, such as Emacs, you can do that instead. The SDK includes all the tools you need to set up an Android project, build it, debug it and then package it for distribution. This document is your guide to using these tools.
When developing in IDEs or editors other than Eclipse, you'll require familiarity with the following Android SDK tools:
In addition to the above tools, included with the SDK, you'll use the following open source and third-party tools:
In the topics that follow, you'll be introduced to each of these tools as necessary. For more advanced operations, please read the respective documentation for each tool.
To create an Android project, you must use the android
tool. When you create
a new project with android
, it will generate a project directory
with some default application files, stub files, configuration files and a build file.
If you're starting a new project, use the android create project
command to generate all the necessary files and folders.
To create a new Android project, open a command-line,
navigate to the tools/
directory of your SDK and run:
android create project \ --target <targetID> \ --path /path/to/your/project \ --activity <your_activity_name> \ --package <your_package_namespace>
target
is the "build target" for your application. It corresponds
to an Android platform library (including any add-ons, such as Google APIs) that you would like to
build your project against. To see a list of available targets and their corresponding IDs,
execute: android list targets
.path
is the location of your project directory. If the directory does not exist,
it will be created for you.activity
is the name for your Activity class. This class file
will be created for you inside
<path_to_your_project>/src/<your_package_namespace_path>/
.package
is the package namespace for your project, following the same rules as for
packages in the Java programming language.Here's an example:
android create project \ --target 1 \ --path ./myProject \ --activity MyActivity \ --package com.example.myproject
The tool generates the following files and directories:
AndroidManifest.xml
- The application manifest file,
synced to the specified Activity class for the project.build.xml
- Build file for Ant.default.properties
- Properties for the build system. Do not modify
this file.build.properties
- Customizable properties for the build system. You can edit this
file to overried default build settings used by Ant.src/your/package/namespace/ActivityName.java
- The Activity class
you specified during project creation.bin/
- Output directory for the build script.gen/
- Holds Ant
-generated files, such as R.java
. libs/
- Holds private libraries.res/
- Holds project resources.src/
- Holds source code.tests/
- Holds a duplicate of all-of-the-above, for testing purposes.Once you've created your project, you're ready to begin development.
You can move your project folder wherever you want for development, but keep in mind
that you must use the Android Debug Bridge
(adb) — located in the SDK tools/
directory — to send your application
to the emulator (discussed later). So you need access between your project solution and
the tools/
folder.
Note: You should refrain from moving the location of the SDK directory, because this will break the build scripts. (They will need to be manually updated to reflect the new SDK location before they will work again.)
If you're upgrading a project from an older version of the Android SDK or want to create
a new project from existing code, use the
android update project
command to update the project to the new development
environment. You can also use this command to revise the build target of an existing project
(with the --target
option). The android
tool will generate any files and
folders (listed in the previous section) that are either missing or need to be updated,
as needed for the Android project.
To update an existing Android project, open a command-line
and navigate to the tools/
directory of your SDK. Now run:
android update project --target <targetID> --path path/to/your/project/
target
is the "build target" for your application. It corresponds to
an Android platform library (including any add-ons, such as Google APIs) that you would
like to build your project against. To see a list of available targets and their corresponding IDs,
execute: android list targets
.path
is the location of your project directory.Here's an example:
android update project --target 2 --path ./myProject
As you begin developing Android applications, understand that all Android applications must be digitally signed before the system will install them on an emulator or device. There are two ways to do this: with a debug key (for immediate testing on an emulator or development device) or with a private key (for application distribution).
The Android build tools help you get started by automatically signing your .apk files with a debug key at build time. This means that you can compile your application and install it on the emulator without having to generate your own private key. However, please note that if you intend to publish your application, you must sign the application with your own private key, rather than the debug key generated by the SDK tools.
Please read Signing Your Applications, which provides a thorough guide to application signing on Android and what it means to you as an Android application developer.
There are two ways to build your application: one for testing/debugging your application — debug mode — and one for building your final package for release — release mode. As described in the previous section, your application must be signed before it can be installed on an emulator or device.
Whether you're building in debug mode or release mode, you need to use the Ant tool to compile and build your project. This will create the .apk file that is installed onto the emulator or device. When you build in debug mode, the .apk file is automatically signed by the SDK tools with a debug key, so it's instantly ready for installation (but only onto an emulator or attached development device). When you build in release mode, the .apk file is unsigned, so you must manually sign it with your own private key, using Keytool and Jarsigner.
It's important that you read and understand Signing Your Applications, particularly once you're ready to release your application and share it with end-users. That document describes the procedure for generating a private key and then using it to sign your .apk file. If you're just getting started, however, you can quickly run your applications on an emulator or your own development device by building in debug mode.
If you don't have Ant, you can obtain it from the Apache Ant home page. Install it and make sure it is in your executable PATH. Before calling Ant, you need to declare the JAVA_HOME environment variable to specify the path to where the JDK is installed.
Note: When installing JDK on Windows, the default is to install
in the "Program Files" directory. This location will cause ant
to fail, because of
the space. To fix the problem, you can specify the JAVA_HOME variable like this:
set JAVA_HOME=c:\Prora~1\Java\
. The easiest solution, however, is to
install JDK in a non-space directory, for example: c:\java\jdk1.6.0_02
.
For immediate application testing and debugging, you can build your application in debug mode and immediately install it on an emulator. In debug mode, the build tools automatically sign your application with a debug key. However, you can (and should) also test your application in release mode. Debug mode simply allows you to run your application without manually signing the application.
To build in debug mode:
ant debug
This creates your Android application .apk file inside the project bin/
directory, named <your_DefaultActivity_name>-debug.apk
. The file
is already signed with the debug key.
Each time you change a source file or resource, you must run Ant again in order to package up the latest version of the application.
To install and run your application on an emulator, see the following section about Running Your Application.
When you're ready to release and distribute your application to end-users, you must build your application in release mode. Once you have built in release mode, it's a good idea to perform additional testing and debugging with the final .apk.
To build in release mode:
ant release
This creates your Android application .apk file inside the project bin/
directory, named <your_DefaultActivity_name>.apk
.
Note: The .apk file is unsigned at this point. You can't install it on an emulator or device until you sign it with your private key.
Because release mode builds your application unsigned, your next step is to sign it with your private key, in order to distribute it to end-users. To complete this procedure, read Signing Your Applications.
Once you have signed your application with a private key, you can install it on an emulator or device as discussed in the following section about Running Your Application. You can also try installing it onto a device from a web server. Simply upload the signed APK to a web site, then load the .apk URL in your Android web browser to download the application and begin installation. (On your device, be sure you have enabled Settings > Applications > Unknown sources.)
Unless you'll be running your application on device hardware, you need to launch an emulator upon which you will install your application. An instance of the Android emulator runs a specific Android platform with specific device configuration settings. The platform and configuration is defined with an Android Virtual Device (AVD). So before you can launch your emulator, you must define an AVD.
If you'll be running your application on device hardware, please read about Developing On a Device instead.
tools/
directory.android list targets
This will output a list of available Android targets, such as:
id:1 Name: Android 1.1 Type: platform API level: 2 Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P id:2 Name: Android 1.5 Type: platform API level: 3 Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P
Find the target that matches the Android platform upon which you'd like
to run your application. Note the integer value of the id
—
you'll use this in the next step.
android create avd --name <your_avd_name> --target <targetID>
From your SDK's tools/
directory, launch an emulator
using an existing AVD (created above):
emulator -avd <your_avd_name>
An instance of the emulator will now launch, running the target and configuration defined by your AVD.
From your SDK's tools/
directory, install the .apk on the emulator:
adb install /path/to/your/application.apk
If there is more than one emulator running, you must specify the emulator upon which to install
the application, by its serial number, with the -s
option. For example:
adb -s emulator-5554 install /my/project/path/myapp.apk
In the emulator, open the list of available applications to find and open your application.
If you don't see your application on the emulator. Try restarting the emulator (with the same AVD). Sometimes when you install an Activity for the first time, it won't show up in the application launcher or be accessible by other applications. This is because the package manager usually examines manifests completely only on emulator startup.
Tip: If you have only one emulator running,
you can build your application and install it on the emulator in one simple step.
Navigate to the root of your project directory and use Ant to compile the project
with install mode:
ant install
. This will build your application, sign it with the debug key,
and install it on the currently running emulator.
If there is more than one emulator currently running
when using the install
command, it will fail — it can't select between the
multiple emulators.
For more information on the tools used above, please see the following documents:
This section describes how to display debug information on the screen (such as CPU usage), as well as how to hook up your IDE to debug running applications on the emulator.
Attaching a debugger is automated using the Eclipse plugin, but you can configure other IDEs to listen on a debugging port to receive debugging information: