Developing Android applications on the base of existing sources
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 [http://developer.android.com/guide/topics/resources/accessing-resources.html R.java] file..
- Create a run configuration.
- Configure an emulator of a physical device to run Android applications on.
- Run an application.
- Share the resources for further re-use.
This tutorial will walk you step-by-step through developing and launching a simple Android application. The application will be based on the sample sources the Android SDK is shipped with. In this tutorial the Snake application sources are used.
- You are working with IntelliJ IDEA Ultimate edition version 10 or higher.
- JDK is available on your machine.
- Android SDK is installed on your machine. This tutorial uses SDK 2.3. Gingerbread.
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 next to the Project files location text box (2) and choose the
…\ android-sdk-windows\Samples\android-9\Snake 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 boxes next to the
…\ android-sdk-windows\Samples\android-9\Snake\src (1)
…\ android-sdk-windows\Samples\android-9\Snake\ (2) paths are selected and check boxes next to 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-7\Snake\tests\src (3) 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. Our project consists of one module. Make sure that the check box next to the module name Snake 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 Snake module icon (1) and the Android facet (2) below it are selected.
When you click Finish, IntelliJ IDEA starts project creation and suggests you to create a run configuration for the Snake module to be created (Image 8). Accept this suggestion by clicking Yes.
Exploring an Android Application
To explore our application, we’ll use the Project tool window that shows the following files and folders (Image 9):
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.snake package.
The res (4) folder contains the visual resources that make up the appearance of the application.
The src (5) folder contains the source code that implements the functionality of your application.
The External Libraries (6) 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 10 illustrates navigation between the files snake_layout.xml (1)
R.java (2). When you click the icon (3) in the left gutter area
next to the snake view definition (4), IntelliJ IDEA opens the
and positions the cursor at the corresponding class field declaration (5).
Image 11 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.
Launching Android Application
Besides generating the above structure and data, IntelliJ IDEA has produced a run configuration Snake. To launch the application straight away, we only need to specify the virtual device to run it on. This device will emulate execution of the application on the target physical device.
Specifying Virtual Device
From the Run/Debug Configuration (1) drop-down list on the toolbar, choose Edit Configurations (Image 12).
IntelliJ IDEA opens the Run/Debug Configurations dialog box (Image 13) that shows the details of the default Snake run configuration (1).
In the Prefer Android Virtual Device for Deployment drop-down list (2), specify the virtual device to run the application on:
- If you already have configured virtual devices, select the desired one from the list (3).
- If not, click the Browse button (4) next to the drop-down list and configure a virtual device in the Select Android Virtual Device dialog box that opens.
Then make sure that the new configuration is selected in the Prefer Android Virtual Device for Deployment drop-down list, apply the changes, and close the Run/Debug Configurations dialog box by clicking OK.
IntelliJ IDEA launches the selected emulator (Image 14):
Next, IDE deploys the Snake application to the emulator and displays the following screen (Image 15):
Click Menu. The application starts:
Press the Up keyboard key and enjoy the game!