Developing Android applications on the base of existing sources

From IntelliJ-Wiki

Jump to: navigation, search

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:

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 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).

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, Snake.

Image 3.

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).

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 boxes next to the …\ android-sdk-windows\Samples\android-9\Snake\src (1) and …\ android-sdk-windows\Samples\android-9\Snake\ (2) paths are selected and check boxes next to 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-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.

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 Snake module icon (1) and the Android facet (2) below it are selected.

Image 7.

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.

Image 8.

Exploring an Android Application

To explore our application, we’ll use the Project tool window that shows the following files and folders (Image 9):

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 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 R.java.

Image 10 illustrates navigation between the files snake_layout.xml (1) and R.java (2). When you click the File:idea_10_navigation_icon.png icon (3) in the left gutter area next to the snake view definition (4), IntelliJ IDEA opens the R.java 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 snake field declaration in R.java brings you to the snake view definition in layout-resource.html.

Image 10.

Image 11 illustrates navigation between the R.java 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 11.

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).

Image 12.

IntelliJ IDEA opens the Run/Debug Configurations dialog box (Image 13) that shows the details of the default Snake run configuration (1).

Image 13.

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.

Start Application

Now we can have IntelliJ IDEA run our application. On the toolbar, click File:run.png (2) next to the Run/Debug Configuration (1) drop-down list where the Snake run configuration is already selected by default.

Image 14.

IntelliJ IDEA launches the selected emulator (Image 14):

Image 15.

Next, IDE deploys the Snake application to the emulator and displays the following screen (Image 15):

Image 16.

Click Menu. The application starts:

Image 17.

Press the Up keyboard key and enjoy the game!

Image 18.

Related Articles

Developing applications for Android in IntelliJ IDEA

Personal tools