Use of Android project library (reference other projects)

Source: Internet
Author: User

Use Library Project Development

Library Project sample code

The SDK contains a sample application called tictactoemain, which shows how the related application uses the code and resources of the android library project. The tictactoemain application uses the code and resources of the tictactoelib example library project.

To download the sample applications and run them as projects in your development environment, useAndroid SDK andavd ManagerDownload the samples for sdkapi 8 component to your SDK.

For more information and sample source code, see tictactoemain application.

AndroidLibrary ProjectIs a development project with shared Android source code and resources. Its own androidapplication project can be used to reference the Library Project, and its source code can be compiled and translated into the .apk file during construction. 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 commonly used in multiple projects, you can move them to a single library project, which makes it easier to maintain them in multiple applications and versions. The following are common scenarios for using library projects:

· If you are developing multiple related applications that use the same components, you should remove these redundant components from your respective application projects, create separate and reusable component sets in the library project.

· If the applications you are creating have free and paid versions, you should move the common parts of these two versions to a library project. Two related projects with different package names will reference the library project and only provide the difference between the two application versions.

In terms of structure, a library project is similar to a standard Android Application project. For example, the project root directory contains the configuration file, which is similar to src/and Res. The library project can contain source code and resources of the same type as the standard Android project and be stored in the same way. For example, the source code of the library project can access its own resources through its R class.

However, the objective of library projects and standard androidapplication items is not the same as that of you. You cannot directly compile them into .apk files or run them on the Android platform. Similarly, you cannot export a library project as a self-contained JAR file like a real library. Instead, you must indirectly compile the libraries referenced in the build path of the relevant application and then build the application.

When you rely on a library of projects to build applications, the SDK tool compiles and merges the source code into the main project, and then generates the .apk file. Hosts file. This gives the application flexibility to use or redefine the behavior or value of any resources in any database.

To further organize the code, your application can add references to multiple database projects and specify the relative priority of resources in each database. This allows you to establish the resources actually used in the application in a cumulative manner. When an application references two databases that define the same resource ID, these tools select resources with higher priority from the database and discard the other.

Once you add references, these tools allow you to edit the building parameters of an Application project to set their relative priorities. During building, these tools merge databases one by one with applications based on their priorities from low to high.

Note that the library project itself cannot reference other library projects, and the library project cannot be referencedNoMerge with each other. However, please note that the library project can use the normal method to import an external library (jar ).

The following sections describe how to use ADT to create and manage your project library. Once you have created a library project and moved them into the source code, you can import the classes and resources to the application in a normal way.

 

Development Requirements

The androidlibrary Project is a building-time structure. For this reason, you can use them to build the ultimate application. APK based on any API level, and compile it with any version of the android library.

However, to use a library project, you need to upgrade the development environment to the latest tools and platforms, because older versions of tools and platforms do not support library project building. Specifically, you need to download and install the following versions.

Table 1.The minimum version of SDK tools and platforms required by the Development Library.

Components

Lowest version

SDK tools

R6 (or higher)

Android 2.2 Platform

R1 (or higher)

Android 2.1 Platform

R2 (or higher)

Android 2.0.1 Platform

Not Supported

Android 2.0 Platform

Not Supported

Android 1.6 Platform

R3 (or higher)

Android 1.5 platform

R4 (or higher)

ADT plugin

0.9.7 (or higher)

You can useAndroid SDK andavd ManagerTo download these tools and platforms. For more information, see Adding sdkcomponents.

 

Create a new library project

The Library Project is a standard Android project, so you can create a library project just like creating a new application. Specifically, you can use the android tool to generate necessary files and directories for new library projects.

 

Create a library project

To create a new library project, go to the <SDK>/tools/directory under the SDK and run the following command:

Android create Lib-project -- name<Your_project_name>\

-- Target<Target_id>\

-- PathPath/to/Your/Project\

-- Package<Your_library_package_namespace>

The create Lib-Project command creates a standard project structure, which contains the preset properties specified to the build system as the Library Project. This is achieved by adding this line to the project file default. properties:

Android. Library = true

Once the command is complete, a library project is created, and you can move the source code and resources to the project. This is described in the following chapter.

If you want to convert an existing application project to a library project for other applications to use it, you can default to the application file. add android. library = true attribute.

 

Create a configuration file

Just like a standard Android Application, the inventory file of the library project must declare all the shared components it contains. For more information, see androidmanifest. xml.

For example, the library project example tictactoelib declares activity -- gameactivity:

<Manifest>
...
<Application>
...
<Activity Android: Name = "gameactivity"/>
...
</Application>
</Manifest>

 

Update Library Project

If you want to update the building properties (Build targets and locations) of the Library Project, run the following command:

Android update Lib-project \
-- Target<Target_id>\
-- PathPath/to/Your/Project

 

Reference a library project from an application

If you are developing an application and want to include shared code or resources in the library project, you only need to simply add the library project reference in the building properties of the application project.

To add the import project reference, go to the <SDK>/tools/directory under the SDK and run the following command:

Android update Lib-project \
-- Target<Target_id>\
-- PathPath/to/Your/Project
-- LibraryPath/to/library_projecta

This name updates the building properties of the application project to include library project references. Specifically, it adds an android. Library. Reference. To the project file default. properties.NAttribute. For example:

Android. Library. reference.1 = path/to/library_projecta

If you add multiple database references, you must note that you can manually set their relative priority (and merging order). By editing the default. properties file, each referenced.NIndex adjustment is appropriate. For example, assume that these references exist:

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 use the following method to set library_projectc to the highest priority:

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: The referenced.NThe index must start from 1 and cannot be left blank for continuous increments (cannot be skipped ). References after the left-blank index will be ignored.

Each time an application merges a database during construction, the priority of the database is increased from low to high. Note that the library itself cannot reference other libraries, and the library cannot be merged with the application before it is created.

 

Declare the library component in the inventory file

In the Application project list file, you must add the declaration of all components used by the application and output from the library project. For example, you must declare all <activity>, <service>, <extends ER>, <provider>, and <permission>, <uses-library>, and similar elements.

The referenced library components must be declared by their full package name.

For example, the library project example tictactoelib declares activity -- gameactivity, as shown in the following code:

<Manifest>
...
<Application>
...
<Activity Android: Name = "com. example. Android. tictactoe. Library. gameactivity"/>
...
</Application>
</Manifest>

For more information about the configuration file, see the documentation androidmanifest. xml.

 

Build associated applications

To build an application project that depends on one or more library projects, you can use the standard ant build command and compilation mode. For details, refer to the building Application Section in this article. This tool compiles and merges all the libraries referenced in the application and uses them as part of compiling associated application projects. No additional commands or steps are required.

 

Development Suggestions

When developing database projects and related applications, remember the following points:

Resource Conflict

Since the tool can merge the resources of the Library Project and its associated applications, the specified resource ID can be defined in the two projects. In this case, the tool selects the highest priority resource from the application or library and discards other resources. When developing an application, you must know that a common resource ID may be defined in multiple projects and merged with resources from the application or the top priority database.

Use a prefix to avoid resource conflicts

To avoid resource conflicts with common resource IDs, you can consider using another prefix or other consistent naming schemes, this naming scheme is unique (or unique across all projects) for projects ).

The library project cannot output the jar

The library cannot publish binary files (such as jar files ). This is because the primary project needs to use the correct resource ID to compile the library project.

Library project contains jar Library

You can develop a library project that contains the jar library. When you create an associated application project, the tool automatically locates and contains the library in the .apk file of the application.

Library projects can depend on external jar Libraries

You can develop a library project that depends on an external library (for example, an external Map Library. In this case, the relevant application must build a target consistent with the external library (for example, Google's API plug-in ). Note that both the library project and related applications must declare the external library in the <uses-library element of their configuration file.

The library project cannot contain the original asset

The tool does not support using the original asset file in the library project. Any asset resources used by the application must be stored in the assets/directory of the application project.

The Library Project and application project apply different adnroid platform versions.

The library is compiled as part of the associated application project. Therefore, the API used by the library project must be compatible with the android library version used for compiling the application project. Generally, the library project should be at the same or lower level as the API used by the application. If the library project uses a higher API level than the application, the compilation of the application project will fail. For example, a library that uses the android 1.5 API (API level 3) can be fully accepted. It can be used for Android 1.6 (API Level 4) or android
2.1 (API level 7.

The library package name is unlimited.

The package name of the library is not consistent with the name of the application that uses the library.

Multiple r classes in the application project GEN/folder

When you build an associated application project, the code of all libraries is compiled and merged into the application project. Each database has its own R class. Its name follows the package name of the database. The r class generated from the main project and library is created in all packages that contain the main project package and library package.

Test Library Project

There are two recommended methods for code and resources in the Test Library Project:

· You can create a test project to test the application project of the dependent library project. Then, you can add a test for the specific function of this database to this project.

· You can create a library-dependent standard application project and add testing tools. This allows you to create a self-contained project and contain testing/testing tools and code for testing.

 

Repository project storage location

There is no clear requirement on the storage location of the library project. For associated application projects, as long as the application project can be referenced to the library project through the relative link. The most important thing to place a library project is that the main project can be referenced to the library project through a relative link.

Connect a debugger to an application

This section describes how to display debugging information on the screen (such as CPU utilization), and how to connect your ide To the application that the simulator is up and running.

Using the eclispse plug-in will automatically connect to a debugger, but you can configure other ide listening debugging ports to receive debugging information.

1.StartDalvik debugmonitor server (ddms)Tools,It acts as a port forwarding service between the IDE and the simulator.

2.Set optional debugging configurations on the simulator,For example, stop the application from starting until the debugger is connected. Note that many debugging options can be used without ddms, such as CPU usage or screen update rate displayed on the simulator.

3.Configure the IDE to connect to port 8700 for debugging.Read logging ing youride to attach to the debugging port.

 

(Full text)

Reprinted from: http://dongxi.net/b04mD

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.