Sharing and re-using Android resources through library projects

From IntelliJ-Wiki

Jump to: navigation, search



IntelliJ IDEA supports development of applications to be executed on mobile phones that run under the Android operating system. Besides general coding assistance, IDE lets you test Android applications on user-configured emulators of physical devices.

IntelliJ IDEA helps:

This tutorial will walk you step-by-step through arranging existing sources in an IntelliJ IDEA project and sharing them for further re-use. We will take the CubeLiveWallpaper sample from the Android SDK, create a project around it, and declare the new project as library so its data can be re-used.


  • You are working with IntelliJ IDEA Ultimate edition version 10.5 or higher.
  • Java SDK and Android SDK are installed on your machine. This tutorial uses SDK 3.1 Honeycomb.

Creating a New Project

Let’s start from the very beginning and create a project for our application.

Choose File | New Project on the main menu or click the Create New Project icon on the Welcome screen (Image 1).

Image 1.

On the first page of the New Project Wizard (Image 2), select the option Create Java project from existing sources.

Image 2.

On the second page of the Wizard (Image 3), specify the name of the project (1), for example, WALLPAPER.

Image 3.

Specify the project folder. Click the Browse button (3) next to the Project files location text box (2) and choose the …\ android-sdk-windows\Samples\android-12\CubeLiveWallpaper folder in the Select project file directory dialog box (Image 4).

Image 4.

When you click OK, you return to the second Project Wizard page (Image 5) where the path to the selected folder is displayed in the Project files location text box (2). .

Image 5.

On the third page of the Wizard, specify which directories you want IntelliJ IDEA to treat as the source paths of the module: make sure that the check box next to the …\ android-sdk-windows\Samples\android-12\CubeLiveWallpaper\src (1) path is selected and check boxes next to any other paths are cleared.

Image 5.

Note: IntelliJ IDEA may detect a src folder in the tests directory. We are not going to create and run unit tests right now, so we do not need to have the …\ android-sdk-windows\Samples\android-12\CubeLiveWallpaper\tests\src folder treated as source path. Still you can have this folder treated as a test path as well.

The fourth page of the Wizard is intended for managing libraries detected in the specified folders. In our case, the sample sources do not contain any libraries, so just click Next to skip the page.

The fifth page of the Wizard (Image 6) shows the structure of modules within the project and dependencies between them. The IDE suggests to create one module in the project. Make sure that the check box next to the module name CubeLiveWallpaper (1) is selected and click Next.

Image 6.

The sixth page of the Wizard (Image 7) displays the detected Android facet and the module where IntelliJ IDEA has detected this facet. Make sure the check boxes next to the CubeLiveWallpaper module icon (1) and the Android facet (2) below it are selected.

Image 7.

When you click Finish, IntelliJ IDEA starts project creation.

Exploring a Project

To explore our project, we’ll use a dedicated tool window that shows the following files and folders (Image 8):

Image 8.

The .idea (1) folder contains a number of subfolders, mainly with internal IntelliJ IDEA information.

The gen (2) folder contains the (3) file that links the visual resources and the Java source code. The file belongs to the package.

The res (4) folder contains the visual resources that make up the appearance of the application.

  • The drawable (5) subfolder contains only one file ic_launcher_wallpaper.png which implements the icon for starting the application.
  • The values (6) subfolder contains two resource definition files. The shapes.xml (7) file defines the appearance, size, and behavior of objects. The strings.xml (8) file defines subscripts and explanations of objects.
  • The xml (9) folder contains <nowiki<xml</nowiki> files with configuration information.

The src (10) folder contains the source code that implements the functionality of your application. The External Libraries (11) folder contains all the libraries required for Android development that are located outside the project root.

Navigating Between Resources and

IntelliJ IDEA helps you quickly switch between resource definitions and corresponding field and class declarations in the file. All you need is just click the File:idea_10_navigation_icon.png icon in the left gutter area next to the relevant resource definition or the corresponding piece of code in

Image 9 illustrates navigation between the files shapes.xml (1) and (2). When you click the File:idea_10_navigation_icon.png icon (3) in the left gutter area next to the definition of cubelines (4) that form the faces of a cube, IntelliJ IDEA opens the file and positions the cursor at the corresponding class field declaration (5).

Accordingly, clicking the File:idea_10_navigation_icon.png icon (6) next to the cubelines field declaration in brings you to the cubelines view definition in shapes.xml.

Image 10.

Image 10 illustrates navigation between the file (1) and drawable resources. When you click the File:idea_10_navigation_icon.png icon (3) next to the declaration of a field in the drawable class (4), IntelliJ IDEA opens the corresponding image (2) in the editor.

Image 10.

Re-using Sample Sources

Now that we have arranged the downloaded sources in a project so they can be managed in InteliJ IDEA, let's see how we can take advantage of them in other projects. To do that, we only need to declare them as library project. After that they can be imported into any other project as a module.

Creating a Shared Library Project

Although Android developers use the term library project, in the context of IntelliJ IDEA it is a module that can be shared. During the project creation, on the fifth page of the wizard, we accepted the suggested one-module structure, so the data to share are all contained in one module CubeLiveWallpaper.

Open the project structure: choose File | Project Structure' on the main menu (Image 11) or press Ctrl+Alt+Shift+S.

Image 11.

In the Project Structure dialog box (Image 12) that opens, click Modules (1). Then select our module CubeLiveWallpaper (2) and the Android facet (3) below.

Image 12.

In the module facet settings, select the Is Library Project check box (4) and click OK. Congratulations! Now and on you can re-use the resources of the Wallpaper sample whenever and wherever you need.

Importing a Library Module into an Existing Project

Now let's see how the shared resources can animate our created from scratch Hello World application. We will expand it with a module that will contain the shared resources.

Open the Hello World project (Image 13). To do so, choose File | Open Project (Image 13):

Image 13.

Then select the project location in the dialog box that opens (Image 14):

Image 14.

When you click OK, IntelliJ IDEA opens the project. Let's add a new module to it.

Adding a Module

Choose File | New | (Image 15):

Image 15.

On the first page of the Add Module wizard (Image 16), choose the Import existing module option (1) and click the Browse button (2).

Image 16.

In the dialog box that opens (Image 17), select the CubeLiveWallpaper module.

Image 17.

When you click OK, you return to the first page of the Add Module wizard (Image 18), where the CubeLiveWallpaper module is displayed in the text box (1).

Image 18.

When you click Finish (2), a new module is created in the project. Let's take a look at the project structure (Image 19).

Image 19.

Besides the original android_hello_world module (1), the Project tool window shows the new CubeLiveWallpaper module (2) with all our old friends imported:

  • The res folder (3) with drawable (4) resources and definitions (5) of cube shapes and subscripts.
  • The gen folder (6) with the file.
  • The src folder (7) with the code to animate the above resources.

Now that all the data are imported we only need to tell the android_hello_world module that it can use the imported resources.

Integrating Imported Data

Let's enable access to the imported module data from the android_hello_world module. This is done through module dependencies.

Open the project structure: choose File | Project Structure' on the main menu or press Ctrl+Alt+Shift+S. In the Project Structure dialog box (Image 20) that opens, click Modules (1), then select the android_hello_world module (2).

Image 20.

Switch to the Dependencies tab (3), click Add (4), and choose Module Dependency (5) on the context menu. In the Choose Modules dialog box that opens (Image 21), select the CubeLiveWallpaper module.

Image 21.

Upon clicking OK, you return to the Module Structure (Image 22) where the CubeLiveWallpaper module is added to the list of dependencies.

Image 22.

Click OK to complete integration of the imported data with the previously created module.

Related Articles

Developing applications for Android in IntelliJ IDEA

Personal tools