IntelliJ IDEA 14.1.0 Help

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

  1. Do one of the following:
  2. On the first page of the wizard, select Android in the left pane and Library Module in the right pane.
  3. 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.

  1. Open the Project Structure dialog box by choosing File | Project Structure from the main menu.
  2. 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.
  3. 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.

  1. From the main menu, select File | New | Module from Existing Sources.
  2. 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.
  3. Add dependencies on the imported library module to the modules where its data is going to be used:
    1. 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.
    2. In the right pane, switch to the Dependencies tab. Click the Add button add.png in the toolbar on the right and select Module Dependency from the context menu.
    3. 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:

  1. Open the Project Structure dialog box by choosing File | Project Structure from the main menu.
  2. Select Modules in the left pane. In the central pane, expand the non-library module and click the Android facet under its node.
  3. 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.

  1. Open the Project Structure dialog box by choosing File | Project Structure from the main menu.
  2. Select Modules in the left pane. In the central pane, expand the non-library module and click the Android facet under its node.
  3. 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.

See Also

Last modified: 14 July 2015