Sharing and re-using Android resources through library projects
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:
- Create an Android application using the New Project wizard.
- Explore an Android application as a tree-view of files and folders.
- Navigate between resources and the R.java file..
- Re-using the resources.
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).
On the first page of the New Project Wizard (Image 2), select the option Create Java project from existing sources.
On the second page of the Wizard (Image 3), specify the name of the project (1), for example,
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).
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). .
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.
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
…\ 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.
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.
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):
The .idea (1) folder contains a number of subfolders, mainly with internal IntelliJ IDEA information.
The gen (2) folder contains the
R.java (3) file that links the visual resources and the Java source code. The file belongs to the com.example.android.livecubes 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.pngwhich 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 R.java
IntelliJ IDEA helps you quickly switch between resource definitions and corresponding field and class declarations
in the R.java file.
All you need is just click the 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)
R.java (2). When you click the 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
R.java file and positions the cursor at the corresponding class field declaration (5).
Image 10 illustrates navigation between the
R.java file (1) and drawable resources.
When you click the icon (3) next to the declaration of a field in the
drawable class (4),
IntelliJ IDEA opens the corresponding image (2) in the editor.
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.
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.
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):
Then select the project location in the dialog box that opens (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):
On the first page of the Add Module wizard (Image 16), choose the Import existing module option (1) and click the Browse button (2).
In the dialog box that opens (Image 17), select the CubeLiveWallpaper module.
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).
When you click Finish (2), a new module is created in the project. Let's take a look at the project structure (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
- 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).
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.
Upon clicking OK, you return to the Module Structure (Image 22) where the CubeLiveWallpaper module is added to the list of dependencies.
Click OK to complete integration of the imported data with the previously created module.