The
android
tool provides you with commands to create all three types of
projects. An Android project contains all of the files and resources that are needed to build a
project into an .apk file for installation.
- An Android project contains all of the files and resources that are needed to build a project into an .apk file for installation. You need to create an Android project for any application that you want to eventually install on a device.
- You can also designate an Android project as a library project, which allows it to be shared with other projects that depend on it. Once an Android project is designated as a library project, it cannot be installed onto a device.
- Test projects extend JUnit test functionality to include Android specific functionality. For more information on creating a test project, see Testing from other IDEs .
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.
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 defaultActivity
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 aname
. -
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
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
platform-tools/
directory — to send your application to the emulator (discussed
later). So you need access between your project solution and the
platform-tools/
folder.
Tip:
Add the
platform-tools/
as well as the
tools/
directory
to your
PATH
environment variable.
Caution:
You should refrain from moving the location of the
SDK directory, because this will break the SDK location property located in
local.properties
.
If you need to update the SDK location, use the
android update project
command.
See
Updating a Project
for more information.
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
Setting up a 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.
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
project.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
project.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
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 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
project.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
project.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 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 and Running . 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.