diff --git a/docs/html/guide/developing/projects/index.jd b/docs/html/guide/developing/projects/index.jd new file mode 100644 index 0000000000000..1daf73864986f --- /dev/null +++ b/docs/html/guide/developing/projects/index.jd @@ -0,0 +1,493 @@ +page.title=Creating and Managing Projects +@jd:body + +
+
+

In this document

+ +
    +
  1. Android Projects
  2. + +
  3. Library Projects
  4. + +
  5. Test Projects
  6. +
+
+
+ +

Projects act as containers for storing things such as code and resource files. The SDK tools + expect your projects to follow a specific structure so it can compile and package your + application correctly, so it is highly recommended that you create them with Eclipse and ADT or + with the android tool on the command line. There are three types of projects, and + they all share the same general structure but differ in function:

+ +
+
Android Projects
+ +
An Android project is the container for your application's source code, resource files, and + files such as the Ant build and Android Manifest file. An application project is the main type + of project and the contents are eventually built into an .apk file that you install on a + device.
+ +
Test Projects
+ +
These projects contain code to test your application projects and are built into + applications that run on a device.
+ +
Library Projects
+ +
These projects contain shareable Android source code and resources that you can reference + in Android projects. This is useful when you have common code that you want to reuse. + Library projects cannot be installed onto a device, however, they are + pulled into the .apk file at build time.
+
+ +

When you use the Android development tools to create a new project, the essential files and + folders will be created for you. There are only a handful of files and folders generated for you, + and some of them depend on whether you use the Eclipse plugin or the {@code android} tool to + generate your project. As your application grows in complexity, you might require new kinds of + resources, directories, and files.

+ +

Android Projects

+ +

Android projects are the projects that eventually get built into an .apk file that you install + onto a device. They contain things such as application source code and resource files. + Some are generated for you by default, while others should be created if + required. The following directories and files comprise an Android project:

+ +
+
src/
+ +
Contains your stub Activity file, which is stored at + src/your/package/namespace/ActivityName.java. All other source code + files (such as .java or .aidl files) go here as well.
+ +
bin
+ +
Output directory of the build. This is where you can find the final .apk file and other + compiled resources.
+ +
jni
+ +
Contains native code sources developed using the Android NDK. For more information, see the + Android NDK documentation.
+ +
gen/
+ +
Contains the Java files generated by ADT, such as your R.java file and + interfaces created from AIDL files.
+ +
assets/
+ +
This is empty. You can use it to store raw asset files. Files that you save here are + compiled into an .apk file as-is, and the original filename is preserved. You can navigate this + directory in the same way as a typical file system using URIs and read files as a stream of + bytes using the the {@link android.content.res.AssetManager}. For example, this is a good + location for textures and game data.
+ +
res/
+ +
+ Contains application resources, such as drawable files, layout files, and string values. See + Application Resources for more + information. + +
+
anim/
+ +
For XML files that are compiled into animation objects. See the Animation resource + type.
+ +
color/
+ +
For XML files that describe colors. See the Color Values resource + type.
+ +
drawable/
+ +
For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that describe + Drawable shapes or a Drawable objects that contain multiple states (normal, pressed, or + focused). See the Drawable resource type.
+ +
layout/
+ +
XML files that are compiled into screen layouts (or part of a screen). See the Layout resource type.
+ +
menu/
+ +
For XML files that define application menus. + See the Menus + resource type.
+ +
raw/
+ +
For arbitrary raw asset files. Saving asset files here instead of in the + assets/ directory only differs in the way that you access them. These files + are processed by aapt and must be referenced from the application using a resource + identifier in the {@code R} class. For example, this is a good place for media, such as MP3 + or Ogg files.
+ +
values/
+ +
For XML files that are compiled into many kinds of resource. Unlike other resources in + the res/ directory, resources written to XML files in this folder are not + referenced by the file name. Instead, the XML element type controls how the resources is + defined within them are placed into the {@code R} class.
+ +
xml/
+ +
For miscellaneous XML files that configure application components. For example, an XML + file that defines a {@link android.preference.PreferenceScreen}, {@link + android.appwidget.AppWidgetProviderInfo}, or Searchability + Metadata. See Application Resources + for more information about configuring these application components.
+
+
+ +
libs/
+ +
Contains private libraries.
+ +
AndroidManifest.xml
+ +
The control file that describes the nature of the application and each of its components. + For instance, it describes: certain qualities about the activities, services, intent receivers, + and content providers; what permissions are requested; what external libraries are needed; what + device features are required, what API Levels are supported or required; and others. See the + AndroidManifest.xml + documentation for more information
+ +
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. If you use Eclipse, this file + is not used.
+ +
build.xml
+ +
The Ant build file for your project. This is only applicable for projects that + you create on the command line.
+ +
default.properties
+ +
This file contains project settings, such as the build target. This files is integral to + the project, as such, it should be maintained in a Source Revision Control system. Do not edit + the file manually.
+
+ +

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 Android 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:

+ + + +

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 and run it on an Android device. + 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 in the dependent application and 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 have added references to library projects to your Android project, + you can set their relative priority. 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 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.

+ +

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 platforms on + which you can develop library projects.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ComponentMinimum Version
SDK Toolsr6 (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.

+ +

Development considerations

+ +

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

+ + + + +

Test Projects

+ +

Test projects contain Android applications that you write using the + Testing and + Instrumentation framework. The framework is an extension of the JUnit test framework and adds + access to Android system objects. The file structure of a test project is the same as an + Android project.

+ +
+
src/
+ +
Includes your test source files. Test projects do not require an Activity .java + file, but can include one.
+ +
gen/
+ +
This contains the Java files generated by ADT, such as your R.java file and + interfaces created from AIDL files.
+ +
assets/
+ +
This is empty. You can use it to store raw asset files.
+ +
res/
+ +
A folder for your application resources, such as drawable files, layout files, string + values, etc. See Application + Resources.
+ +
AndroidManifest.xml
+ +
The Android Manifest for your project. See The AndroidManifest.xml File. Test + Projects have a special + <instrumentation> + element that connects the test project with the application project.
+ +
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.
+ +
build.xml
+ +
The Ant build file for your project.
+ +
default.properties
+ +
This file contains project settings, such as the build target. This files is integral to + the project, as such, it should be maintained in a Source Revision Control system. It should + never be edited manually — to edit project properties, right-click the project folder and + select "Properties".
+
For more information, see the Testing section. + + +

Testing a library project

+ +

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

+ + \ No newline at end of file diff --git a/docs/html/guide/developing/projects/projects-cmdline.jd b/docs/html/guide/developing/projects/projects-cmdline.jd new file mode 100644 index 0000000000000..de67b29416c47 --- /dev/null +++ b/docs/html/guide/developing/projects/projects-cmdline.jd @@ -0,0 +1,285 @@ +page.title=Managing Projects on the Command Line +@jd:body + +
+
+

In this document

+ +
    +
  1. Creating an Android Project
  2. + +
  3. Updating a Project
  4. + +
  5. Setting up a Library Project
  6. + +
  7. Referencing a Library Project from an + Application
  8. +
+ +

See also

+ +
    +
  1. Testing + in Other IDEs
  2. +
+
+
+ +

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

+ + +

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>
+
+ + + +

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 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>
+
+ + + +

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

+ +

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.

+ diff --git a/docs/html/guide/developing/projects/projects-eclipse.jd b/docs/html/guide/developing/projects/projects-eclipse.jd new file mode 100644 index 0000000000000..45111f3c8383b --- /dev/null +++ b/docs/html/guide/developing/projects/projects-eclipse.jd @@ -0,0 +1,235 @@ +page.title=Managing Projects in Eclipse +@jd:body + +
+
+

In this document

+ +
    +
  1. Creating an Android Project
  2. + +
  3. Setting up a Library Project
  4. + +
  5. Referencing a Library Project
  6. +
+ +

See also

+ +
    +
  1. Testing + in Eclipse, with ADT
  2. +
+
+
+ +

Eclipse and the ADT plugin provide GUIs and wizards to create all three types of projects + (Android project, Library project, and Test project): + +

+ +

Creating an Android Project

+ +

The ADT plugin provides a New Project Wizard that you can use to quickly create a new Android + project (or a project from existing code). To create a new project:

+ +
    +
  1. Select File > New > Project.
  2. + +
  3. Select Android > Android Project, and click + Next.
  4. + +
  5. Select the contents for the project: + + +
  6. + +
  7. Click Finish.
  8. +
+ +

Tip: You can also start the New Project Wizard from the + New icon in the toolbar.

+ +

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.

+ +

When you are creating the library project, you can select any application name, package, and + set other fields as needed, as shown in figure 1.

+ +

Next, set the project's properties to indicate that it is a library project:

+ +
    +
  1. In the Package Explorer, right-click the library project and select + Properties.
  2. + +
  3. In the Properties window, select the "Android" properties group at left + and locate the Library properties at right.
  4. + +
  5. Select the "is Library" checkbox and click Apply.
  6. + +
  7. Click OK to close the Properties window.
  8. +
+ +

The new project is now marked as a library project. You can begin moving source code and + resources into it, as described in the sections below.

+ +

You can also convert an existing application project into a library. To do so, simply open the + Properties for the project and select the "is Library" checkbox. Other application projects can + now reference the existing project as a library project.

+ + + +

Figure 1. Marking a project as an + Android library project.

+ +

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>
+
+ +

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

+ +

To add a reference to a library project, follow these steps:

+ +
    +
  1. In the Package Explorer, right-click the dependent project and select + Properties.
  2. + +
  3. In the Properties window, select the "Android" properties group at left + and locate the Library properties at right.
  4. + +
  5. Click Add to open the Project Selection dialog.
  6. + +
  7. From the list of available library projects, select a project and click + OK.
  8. + +
  9. When the dialog closes, click Apply in the Properties + window.
  10. + +
  11. Click OK to close the Properties window.
  12. +
+ +

As soon as the Properties dialog closes, Eclipse rebuilds the project, including the contents + of the library project.

+ +

Figure 2 shows the Properties dialog that lets you add library references and move + them up and down in priority.

+ +

Figure 2. Adding a reference to a + library project in the properties of an application project.

+ +

If you are adding references to multiple libraries, note that you can set their relative + priority (and merge order) by selecting a library and using the Up and + Down controls. The tools merge the referenced libraries with your application + starting from lowest priority (bottom of the list) to highest (top of the list). If more than one + library defines the same resource ID, the tools select the resource from the library with higher + priority. The application itself has highest priority and its resources are always used in + preference to identical resource IDs defined in libraries.

+ +

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.

+ + + + + + +