Use library projects to reuse Android program code

Source: Internet
Author: User
Author: du gang

In the software development process,ProgramCodeIs a very important concept. We always need to use some existing modules, packages, frameworks, or develop our own modules, packages, and frameworks to reuse program code. For example, in Java Web programming, frameworks such as Struts and spring that are frequently used can greatly simplify our development process and improve development efficiency.

This requirement also exists in the process of developing Android applications. The Android Application Development Framework included in the Android system is already a framework that can improve development efficiency, but it cannot meet all our needs. For example, when we develop another type of application, we will find that this type of application contains a large amount of code that is repeated. In this case, we need to optimize the design and adopt a certain method, reuse of repeated code makes it easy to develop such applications.

Similar to developing other Java applications, we can pack reusable code into a jar package for all required projects. In this way, a large part of code reuse problems can be solved, such as the core that can be reused in different applications.Algorithm.

However, if reusable code contains a large amount of resources, including images, music files, and even interface la S, code reuse becomes troublesome, these items cannot be packaged together in the jar package for the required project. Without these resources, reusable code cannot run. In this case, the best solution is to use the library project (LIB project) supported by the SDK after android2.2 ).

A library project is a project that contains source code and resources. It can be referenced by other projects so that other projects can use the code and resources it contains. Multiple Android Application projects can reference the same library project. The same Android Application project can also reference multiple library projects.

It is easy to create a library project and reference a library project. Take the development in eclipse environment as an example. To create a library project, you only need to open the panel for setting project properties after creating a common android project, and select the is library check box under the android tab (as shown in ).

When an existing project references a library project, you only need to open the project property settings panel and add the project to be referenced under the android tab, as shown in ).

After completing the above operations, we have successfully created a library project or referenced a library project in an existing project. Isn't it easy?

Of course, when using a library project, because of the special nature of the android project structure and compilation tool, we need to pay attention to some points, as listed below:

1. The resource names of the Work project and library project should not be repeated as much as possible. If they are repeated, the compiler will give priority to the resources of the Work project;

2. A library project cannot be exported as a jar file;

3. The Android system version of the library project must be equal to or lower than the system version of The Work project;

4. each library project generates an R file. When a work project is compiled, the R file of the Work Project and the library project is merged. If a constant in the r file has the same name, the constant in the r file of the Work project will be used. If multiple library projects are referenced, the R file in the library project has the constant name, the usage priority of constants will be determined based on the order in which the library project is referenced.

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.