android共享库,Android Studio 共享库项目

https://www.jetbrains.com/idea/help/sharing-android-source-code-and-resources-using-library-projects.html

Sharing Android Source Code and Resources Using Library Projects

IntelliJ IDEA supports Android library projects that hold shared Android source code and resources. Other Android application projects can reference a library project and include its compiled sources in their .apk files at build time.

In IntelliJ IDEA, library projects are supported through separate library modules.

To enable sharing Android source code and resources, do one of the following:

Create a new Library module.

Convert an Application module that contains sources you want to share into a Library module.

In this topic:

Creating a Library module

On the first page of the wizard, select Android in the left pane and Library Module in the right pane.

Complete the wizard.

Converting an Application module into a Library module

You can convert an Application module into a Library module by updating its Android facet.

Open the Project Structure dialog box by choosing File | Project Structure from the main menu.

Select Modules in the left pane. In the central pane, expand the node of the module that you want to turn into a Library module and click Android.

In the right-hand pane, select the Library module check box on top of the Android facet page.

Using a Library module in another project

To use a Library module in another project, you need to import this module into it.

From the main menu, select File | New | Module from Existing Sources.

In the dialog that opens, browse to the .iml module file that you want to import and click OK. The module node will be added to the tree view.

Add dependencies on the imported library module to the modules where its data is going to be used:

Open the settings of the non-library module: navigate to File | Project Structure, select Modules in the left pane and select your non-library module in the central pane.

In the right pane, switch to the Dependencies tab. Click the Add button

0818b9ca8b590ca3270a3433284dd417.png in the toolbar on the right and select Module Dependency from the context menu.

In the Choose Modules dialog box that opens, select the imported library module from the list and click OK.

Adding data from AndroidManifest.xml for a library module to AndroidManifest.xml for the entire application

To successfully integrate a library module into another application, its components must be declared in the application AndroidManifest.xml file. The manifest file contains the information that is required to run the application (for more information, see App Manifest). You can either add this information on a library module manually, or extract it from AndroidManifest.xml of the library module and add it to the AndroidManifest.xml of the application automatically.

The second approach is referred to as merging manifests.

To have the manifest of a library module merged with the application manifest automatically:

Open the Project Structure dialog box by choosing File | Project Structure from the main menu.

Select Modules in the left pane. In the central pane, expand the non-library module and click the Android facet under its node.

In the right pane, on the Android facet page, switch to the Packaging tab and select the Enable manigest merging option.

Including the .dex file of a library module into the .apk of the entire application without rebuilding (pre-dexing)

During the application packaging, the .class files of a library module are converted into .dex files. This operation is referred to as dexing. Finally, the .dex files output from the library module is included in the final application .apk (learn more about the building procedure from Building and Running).

As a rule, the contents of a library module remain unchanged. In this case you can have them dexed only once, whereupon the output .dex files are included in the .apk. This approach is referred to as pre-dexing.

By default, IntelliJ IDEA pre-dexes library mode dependencies as well as external jars that have not been updated since the previous build. You can change these settings so that all .class files are always dexed.

Open the Project Structure dialog box by choosing File | Project Structure from the main menu.

Select Modules in the left pane. In the central pane, expand the non-library module and click the Android facet under its node.

In the right pane, on the Android facet page, switch to the Packaging tab and select the Pre-dex external jars and Android library dependencies option.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值