Developing In Other IDEs

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.

Essential Tools

When developing in IDEs or editors other than Eclipse, you'll require familiarity with the following Android SDK tools:

android
To create/update Android projects and to create/move/delete AVDs.
Android Emulator
To run your Android applications on an emulated Android platform.
Android Debug Bridge
To interface with your emulator or connected device (install apps, shell the device, issue commands, etc.).

In addition to the above tools, included with the SDK, you'll use the following open source and third-party tools:

Ant
To compile and build your Android project into an installable .apk file.
Keytool
To generate a keystore and private key, used to sign your .apk file.
Jarsigner (or similar signing tool)
To sign your .apk file with a private key generated by keytool.

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.

Creating an Android Project

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.

Creating a new Project

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 <target_ID> \
--name <your_project_name> \
--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.
  • name is the name for your project. This is optional. If provided, this name will be used for your .apk filename when you build your application.
  • 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 default Activity class. This class file will be created for you inside <path_to_your_project>/src/<your_package_namespace_path>/ . This will also be used for your .apk filename unless you provide a the name.
  • 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 \
--name MyAndroidApp \
--path ./MyAndroidAppProject \
--activity MyAndroidAppActivity \
--package com.example.myandroid

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 override default build settings used by Ant and provide a pointer to your keystore and key alias so that the build tools can sign your application when built in release mode.
  • 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.

Caution: 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.)

Updating a project

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) and the project name (with the --name 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 --name <project_name> --target <target_ID>
--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.
  • name is the name for the project. This is optional—if you're not changing the project name, you don't need this.

Here's an example:

android update project --name MyApp --target 2 --path ./MyAppProject

Preparing to Sign Your Application

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.

Building Your Application

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:\Progra~1\Java\<jdkdir>. The easiest solution, however, is to install JDK in a non-space directory, for example: c:\java\jdk1.6.0_02.

Building in debug mode

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 and optimize the package with zipalign. 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:

  1. Open a command-line and navigate to the root of your project directory.
  2. Use Ant to compile your project in debug mode:
    ant debug

    This creates your debug .apk file inside the project bin/ directory, named <your_project_name>-debug.apk. The file is already signed with the debug key and has been aligned with zipalign.

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.

Building in release mode

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.

Before you start building your application in release mode, be aware that you must sign the resulting application package with your private key, and should then align it using the zipalign tool. There are two approaches to building in release mode: build an unsigned package in release mode and then manually sign and align the package, or allow the build script to sign and align the package for you.

Build unsigned

If you build your application unsigned, then you will need to manually sign and align the package.

To build an unsigned .apk in release mode:

  1. Open a command-line and navigate to the root of your project directory.
  2. Use Ant to compile your project in release mode:
    ant release

This creates your Android application .apk file inside the project bin/ directory, named <your_project_name>-unsigned.apk.

Note: The .apk file is unsigned at this point and can't be installed until signed with your private key.

Once you have created the unsigned .apk, your next step is to sign the .apk with your private key and then align it with zipalign. To complete this procedure, read Signing Your Applications.

When your .apk has been signed and aligned, it's ready to be distributed to end-users.

Build signed and aligned

If you would like, you can configure the Android build script to automatically sign and align your application package. To do so, you must provide the path to your keystore and the name of your key alias in your project's build.properties file. With this information provided, the build script will prompt you for your keystore and alias password when you build in release mode and produce your final application package, which will be ready for distribution.

Caution: Due to the way Ant handles input, the password that you enter during the build process will be visible. If you are concerned about your keystore and alias password being visible on screen, then you may prefer to perform the application signing manually, via Jarsigner (or a similar tool). To instead perform the signing procedure manually, build unsigned and then continue with Signing Your Applications.

To specify your keystore and alias, open the project build.properties file (found in the root of the project directory) and add entries for key.store and key.alias. For example:

key.store=path/to/my.keystore
key.alias=mykeystore

Save your changes. Now you can build a signed .apk in release mode:

  1. Open a command-line and navigate to the root of your project directory.
  2. Use Ant to compile your project in release mode:
    ant release
  3. When prompted, enter you keystore and alias passwords.

    Caution: As described above, your password will be visible on the screen.

This creates your Android application .apk file inside the project bin/ directory, named <your_project_name>-release.apk. This .apk file has been signed with the private key specified in build.properties and aligned with zipalign. It's ready for installation and distribution.

Once built and signed in release mode

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.)

Creating an AVD

An Android Virtual Device (AVD) is a device configuration for the emulator that allows you to model real world devices. In order to run an instance of the emulator, you must create an AVD.

To create an AVD using the SDK tools:

  1. Navigate to your SDK's tools/ directory and execute the android tool with no arguments:
    android

    This will launch the SDK and AVD Manager GUI.

  2. In the Virtual Devices panel, you'll see a list of existing AVDs. Click New to create a new AVD.
  3. Fill in the details for the AVD.

    Give it a name, a platform target, an SD card size, and a skin (HVGA is default).

    Note: Be sure to define a target for your AVD that satisfies your application's build target (the AVD platform target must have an API Level equal to or greater than the API Level that your application compiles against).

  4. Click Create AVD.

Your AVD is now ready and you can either close the AVD Manager, create more AVDs, or launch an emulator with the AVD by clicking Start.

For more information about AVDs, read the Android Virtual Devices documentation.

Running Your Application

Running your application on a virtual or real device takes just a couple steps. Remember to first build your application.

Running on the emulator

Before you can run your application on the Android Emulator, you must create an AVD.

To run your application:

  1. Open the SDK and AVD Manager and launch a virtual device
  2. From your SDK's tools/ directory, execute the android tool with no arguments:

    android

    In the Virtual Devices view, select an AVD and click Start.

  3. Install your application

    From your SDK's tools/ directory, install the .apk on the emulator:

    adb install <path_to_your_bin>.apk

    Your APK file (signed with either a release or debug key) is in your project bin/ directory after you build your application.

    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 path/to/your/app.apk

    To see a list of available device serial numbers, execute adb devices.

If you don't see your application on the emulator. Try closing the emulator and launching the virtual device again from the SDK and AVD Manager. 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.

Be certain to create multiple AVDs upon which to test your application. You should have one AVD for each platform and screen type with which your application is compatible. For instance, if your application compiles against the Android 1.5 (API Level 3) platform, you should create an AVD for each platform equal to and greater than 1.5 and an AVD for each screen type you support, then test your application on each one.

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.

Running on a device

Before you can run your application on a device, you must perform some basic setup for your device:

  • Declare your application as debuggable in your manifest
  • Enable USB Debugging on your device
  • Ensure that your development computer can detect your device when connected via USB

Read Setting up a Device for Development for more information.

Once your device is set up and connected via USB, navigate to your SDK's tools/ directory and install the .apk on the device:

adb -d install path/to/your/app.apk

The -d flag specifies that you want to use the attached device (in case you also have an emulator running).

For more information on the tools used above, please see the following documents:

Working with Library Projects

An Android library project is a development project that holds shared Android source code and resources. Other Android application projects can reference the library project and, at build time, include its compiled sources in their .apk files. Multiple application projects can reference the same library project and any single application project can reference multiple library projects.

If you have source code and resources that are common to multiple application projects, you can move them to a library project so that it is easier to maintain across applications and versions. Here are some common scenarios in which you could make use of library projects:

  • If you are developing multiple related applications that use some of the same components, you could move the redundant components out of their respective application projects and create a single, reuseable set of the same components in a library project.
  • If you are creating an application that exists in both free and paid versions, you could move the part of the application that is common to both versions into a library project. The two dependent projects, with their different package names, will reference the library project and provide only the difference between the two application versions.

Structurally, a library project is similar to a standard Android application project. For example, it includes a manifest file at the project root, as well as src/, res/ and similar directories. The project can contain the same types of source code and resources as a standard Android project, stored in the same way. For example, source code in the library project can access its own resources through its R class.

However, a library project differs from an standard Android application project in that you cannot compile it directly to its own .apk or run it on the Android platform. Similarly, you cannot export the library project to a self-contained JAR file, as you would do for a true library. Instead, you must compile the library indirectly, by referencing the library from a dependent application's build path, then building that application.

When you build an application that depends on a library project, the SDK tools compile the library and merge its sources with those in the main project, then use the result to generate the .apk. In cases where a resource ID is defined in both the application and the library, the tools ensure that the resource declared in the application gets priority and that the resource in the library project is not compiled into the application .apk. This gives your application the flexibility to either use or redefine any resource behaviors or values that are defined in any library.

To organize your code further, your application can add references to multiple library projects, then specify the relative priority of the resources in each library. This lets you build up the resources actually used in your application in a cumulative manner. When two libraries referenced from an application define the same resource ID, the tools select the resource from the library with higher priority and discard the other.

Once you've have added references, the tools let you set their relative priority by editing the application project's build properties. At build time, the tools merge the libraries with the application one at a time, starting from the lowest priority to the highest.

Note that a library project cannot itself reference another library project and that, at build time, library projects are not merged with each other before being merged with the application. However, note that a library can import an external library (JAR) in the normal way.

The sections below describe how to use ADT to set up and manage library your projects. Once you've set up your library projects and moved code into them, you can import library classes and resources to your application in the normal way.

Development requirements

Android library projects are a build-time construct, so you can use them to build a final application .apk that targets any API level and is compiled against any version of the Android library.

However, to use library projects, you need to update your development environment to use the latest tools and platforms, since older releases of the tools and platforms do not support building with library projects. Specifically, you need to download and install the versions listed below:

Table 1. Minimum versions of SDK tools and plaforms on which you can develop library projects.

Component Minimum Version
SDK Tools r6 (or higher)
Android 2.2 platformr1 (or higher)
Android 2.1 platformr2 (or higher)
Android 2.0.1 platformnot supported
Android 2.0 platformnot supported
Android 1.6 platformr3 (or higher)
Android 1.5 platformr4 (or higher)
ADT Plugin0.9.7 (or higher)

You can download the tools and platforms using the Android SDK and AVD Manager, as described in Adding SDK Components.

Setting up a new library project

A library project is a standard Android project, so you can create a new one in the same way as you would a new application project. Specifically, you can use the android tool to generate a new library project with all of the necessary files and folders.

Creating a library project

To create a new library project, navigate to the <sdk>/tools/ directory and use this command:

android create lib-project --name <your_project_name> \
--target <target_ID> \
--path path/to/your/project \
--package <your_library_package_namespace>

The create lib-project command creates a standard project structure that includes preset property that indicates to the build system that the project is a library. It does this by adding this line to the project's default.properties file:

android.library=true

Once the command completes, the library project is created and you can begin moving source code and resources into it, as described in the sections below.

If you want to convert an existing application project to a library project, so that other applications can use it, you can do so by adding a the android.library=true property to the application's default.properties file.

Creating the manifest file

A library project's manifest file must declare all of the shared components that it includes, just as would a standard Android application. For more information, see the documentation for AndroidManifest.xml.

For example, the TicTacToeLib example library project declares the Activity GameActivity:

<manifest>
  ...
  <application>
    ...
    <activity android:name="GameActivity" />
    ...
  </application>
  ...
</manifest>

Updating a library project

If you want to update the build properties (build target, location) of the library project, use this command:

android update lib-project \
--target <target_ID> \
--path path/to/your/project

Referencing a library project from an application

If you are developing an application and want to include the shared code or resources from a library project, you can do so easily by adding a reference to the library project in the application project's build properties.

To add a reference to a library project, navigate to the <sdk>/tools/ directory and use this command:

android update lib-project \
--target <target_ID> \
--path path/to/your/project
--library path/to/library_projectA

This command updates the application project's build properties to include a reference to the library project. Specifically, it adds an android.library.reference.n property to the project's default.properties file. For example:

android.library.reference.1=path/to/library_projectA

If you are adding references to multiple libraries, note that you can set their relative priority (and merge order) by manually editing the default.properties file and adjusting the each reference's .n index as appropriate. For example, assume these references:

android.library.reference.1=path/to/library_projectA
android.library.reference.2=path/to/library_projectB
android.library.reference.3=path/to/library_projectC

You can reorder the references to give highest priority to library_projectC in this way:

android.library.reference.2=path/to/library_projectA
android.library.reference.3=path/to/library_projectB
android.library.reference.1=path/to/library_projectC

Note that the .n index in the references must begin at "1" and increase uniformly without "holes". References appearing in the index after a hole are ignored.

At build time, the libraries are merged with the application one at a time, starting from the lowest priority to the highest. Note that a library cannot itself reference another library and that, at build time, libraries are not merged with each other before being merged with the application.

Declaring library components in the the manifest file

In the manifest file of the application project, you must add declarations of all components that the application will use that are imported from a library project. For example, you must declare any <activity>, <service>, <receiver>, <provider>, and so on, as well as <permission>, <uses-library>, and similar elements.

Declarations should reference the library components by their fully-qualified package names, where appropriate.

For example, the TicTacToeMain example application declares the library Activity GameActivity like this:

<manifest>
  ...
  <application>
    ...
    <activity android:name="com.example.android.tictactoe.library.GameActivity" />
    ...
  </application>
  ...
</manifest>

For more information about the manifest file, see the documentation for AndroidManifest.xml.

Building a dependent application

To build an application project that depends on one or more library projects, you can use the standard Ant build commands and compile modes, as described in Building Your Application, earlier in this document. The tools compile and merge all libraries referenced by the application as part of compiling the dependent application project. No additional commands or steps are necessary.

Development considerations

As you develop your library project and dependent applications, keep the points listed below in mind.

Resource conflicts

Since the tools merge the resources of a library project with those of a dependent application project, a given resource ID might be defined in both projects. In this case, the tools select the resource from the application, or the library with highest priority, and discard the other resource. As you develop your applications, be aware that common resource IDs are likely to be defined in more than one project and will be merged, with the resource from the application or highest-priority library taking precedence.

Using prefixes to avoid resource conflicts

To avoid resource conflicts for common resource IDs, consider using a prefix or other consistent naming scheme that is unique to the project (or is unique across all projects).

No export of library project to JAR

A library cannot be distributed as a binary file (such as a jar file). This is because the library project is compiled by the main project to use the correct resource IDs.

A library project can include a JAR library

You can develop a library project that itself includes a JAR library. When you build the dependent application project, the tools automatically locate and include the library in the application .apk.

A library project can depend on an external JAR library

You can develop a library project that depends on an external library (for example, the Maps external library). In this case, the dependent application must build against a target that includes the external library (for example, the Google APIs Add-On). Note also that both the library project and the dependent application must declare the external library their manifest files, in a <uses-library> element.

Library project cannot include raw assets

The tools do not support the use of raw asset files in a library project. Any asset resources used by an application must be stored in the assets/ directory of the application project itself.

Targeting different Android platform versions in library project and application project

A library is compiled as part of the dependent application project, so the API used in the library project must be compatible with the version of the Android library used to compile the application project. In general, the library project should use an API level that is the same as — or lower than — that used by the application. If the library project uses an API level that is higher than that of the application, the application project will fail to compile. It is perfectly acceptable to have a library that uses the Android 1.5 API (API level 3) and that is used in an Android 1.6 (API level 4) or Android 2.1 (API level 7) project, for instance.

No restriction on library package name

There is no requirement for the package name of a library to be the same as that of applications that use it.

Multiple R classes in gen/ folder of application project

When you build the dependent application project, the code of any libraries is compiled and merged to the application project. Each library has its own R class, named according to the library's package name. The R class generated from the resources of the main project and of the library is created in all the packages that are needed including the main project’s package and the libraries’ packages.

Testing a library project

There are two recommended ways of setting up testing on code and resources in a library project:

  • You can set up a test project that instruments an application project that depends on the library project. You can then add tests to the project for library-specific features.
  • You can set up a set up a standard application project that depends on the library and put the instrumentation in that project. This lets you create a self-contained project that contains both the tests/instrumentations and the code to test.

Library project storage location

There are no specific requirements on where you should store a library project, relative to a dependent application project, as long as the application project can reference the library project by a relative link. You can place the library project What is important is that the main project can reference the library project through a relative link.

Attaching a Debugger to Your Application

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:

  1. Start the Dalvik Debug Monitor Server (DDMS) tool, which acts as a port forwarding service between your IDE and the emulator.
  2. Set optional debugging configurations on your emulator, such as blocking application startup for an Activity until a debugger is attached. Note that many of these debugging options can be used without DDMS, such as displaying CPU usage or screen refresh rate on the emulator.
  3. Configure your IDE to attach to port 8700 for debugging. Read about Configuring Your IDE to Attach to the Debugging Port.
↑ Go to top