Developing applications for Android using existing sources
From IntelliJ-Wiki
Contents |
Introduction
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.
- Create a run configuration.
- Configure an emulator of a physical device to run Android applications on.
- Run an application.
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.
Prerequisites
- You are working with IntelliJ IDEA Ultimate edition version 9 or higher.
- JDK is available on your machine.
- Android SDK is installed on your machine. This tutorial uses SDK 2.2. FroYo.
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.
On the first page of the New Project Wizard, select the option Create Java project from existing sources.
On the second page of the Wizard, specify the location of the sample sources that will become the basis of our module. Click the Browse button next to the
Project files location text box and choose the
…\ android-sdk-windows\Samples\android-7\Snake folder.
When you click OK, you return to the second Project Wizard page where the path to the selected folder is displayed in the Project files location text box (1). IntelliJ IDEA automatically assigns the folder name Snake to the project (2).
On the third page of the Wizard, specify which directory you want IntelliJ IDEA to treat as the source path of the module: make sure that the check boxes next to the …\ android-sdk-windows\Samples\android-7\Snake\src (1)
and …\ android-sdk-windows\Samples\android-7\Snake\gen (2) paths are 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
the …\ android-sdk-windows\Samples\android-7\Snake\tests\src (3) folder treated as source path.
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 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 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. 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:
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.
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.
IntelliJ IDEA opens the Run/Debug Configurations dialog box 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.
- If not, click the Browse button (3) 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.
Start Application
Now we can have IntelliJ IDEA run our application. On the toolbar, click
(2) next to the Run/Debug Configuration (1) drop-down list where the Snake run configuration is already selected by default.
IntelliJ IDEA launches the selected emulator:
Next, IDE deploys the Snake application to the emulator and displays the following screen:
Click Menu. The application starts:
Press the Up keyboard key and enjoy the game!















