Android如何引用其餘工程

Referencing a library projecthtml

 

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

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

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

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

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

 

 

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

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

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

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

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

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

 

 

按以下方法設置:

1. 假設要引用的android工程叫LibProject,引入到的工程叫MainProject;

2. 設置LibProject,右鍵->Properties->Android,將Is library項選中,而後Apply;

3. 設置MainProject,右鍵->->Properties->Android, 在Library中,點擊Add按鈕,將LibProject工程加入,Apply便可。

 

設置完成後,在MainProject工程中能看到LibProject的代碼等資源都已經引入進來。

 

本文轉載自:http://blog.csdn.net/johnnywsd/article/details/6845451