Developing applications for Android using 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 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.

File:welcome_screen_create_new_project.png

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

File:create_project_around_existing_sources_page_1.png

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.

File:create_project_around_existing_sources_select_directory.png

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

File:create_project_wizard__from_existing_sources_page2.png

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.

File:create_project_wizard_around_existing_sources_page3.png

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.

File:create_project_around_existing_sources_page_5.png

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.

File:create_project_around_existing_sources_page_6.png

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.

File:create_project_wizard_around_existing_sources_create_run_configuration.png

Exploring an Android Application

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

File:snake_project_structure.png

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.

File:choose_run_configuration.png

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

File:run_configuration_dialog_snake.png

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 File:run.png (2) next to the Run/Debug Configuration (1) drop-down list where the Snake run configuration is already selected by default.

File:application_from_existing_sources_run_default_configuration_snake.png

IntelliJ IDEA launches the selected emulator:

File:emulator_1_froyo.png

Next, IDE deploys the Snake application to the emulator and displays the following screen:

File:emulator_2_froyo.png

Click Menu. The application starts:

File:emulator_3_froyo.png

Press the Up keyboard key and enjoy the game!

File:emulator_4_froyo_play.png

Related Articles

Developing applications for Android in IntelliJ IDEA

Personal tools