IntelliJ IDEA 2018.2 Help

Building and Running the Application

After you have created your first Android application, you are ready to compile it and deploy it to an Android emulator, or a physical device.

Before you build the project and start testing the application, it is recommended that you review the current build configurations.

In this tutorial:

Review the default run/debug configuration

Click Run | Edit Configurations. The following dialog will be displayed:

Android run debug config

IntelliJ IDEA automatically creates a default run/debug configuration with the same name as your project. For a detailed description of each configuration option in an Android run/debug configuration, see Run/Debug Configuration: Android Application.

Add a New Run/Debug Configuration

Let's create a custom build configuration:

  1. In the Run/Debug Configurations dialog, click the Add button icons general add svg and select Android Application from the list:

    Android new build config step1

  2. A new item will be added to the tree in the left pane with the default name Unnamed. Select it and enter a more meaningful name in the Name field, for example, HelloDroid Emulator:
    Android new build config step2

    Note that the newly created configuration is shown with a red cross to indicate that it is not yet ready to be used, as no module has been specified.

  3. Next, you need to specify the executable module. Expand the Module drop-down list and select HelloDroid: it is the only executable module in your project.
    Android new build config step3

    When you have selected a module, the red cross disappears, as now your run/debug configuration is filled with the minimum required information.

  4. Next, make sure the Default APK option is selected under Installation Options. With this option selected, the application will be automatically deployed on the target device.

  5. Next, you need to decide which activity you want to launch as a starter activity for your module. You can launch the default activity configured in the AndroidManifest file, or select a custom activity. Since your sample application only has one activity, select the Default Activity option.

  6. Finally, you need to specify the target device where your application will be launched. Let's select the Emulator option. If you already have Android virtual devices configured, you can select a device from the Prefer Android Virtual Device drop-down list. If you have no devices configured, perform the following steps:
    • From the main menu, choose Tools | Android | AVD Manager to launch the Android Virtual Device Manager:

      Android AVD manager welcome

    • Click Create a virtual device. In the dialog that opens, in the left pane select the type of the Android device you want to mimic: TV, Phone, Wear or Tablet. Let's choose Phone. In the central pane select the phone model (for example, Nexus 5X) and click Next:

      Android AVD manager step1

    • On the next step, select the system image that you want to mimic on the virtual device, i.e. the OS version, the Android API level, the application binary interface (ABI) and the target SDK version:

      Android AVD manager step2

    • On the last step, you can modify the AVD name and select the startup size and orientation of the virtual device screen:

      Android AVD manager step3

    • Click Finish to complete the wizard. The newly created Android virtual device will be added to the devices list:

      Android AVD manager final

    • Return to the Run/Debug Configurations dialog.

  7. Note that an Android Virtual Device (AVD) is just a configuration file through which you define the hardware and software options for the emulator to mimic. In addition to AVD settings, you can configure supplementary parameters, such as network transfer rate to be emulated, network latency (the time delay between the initial input and the output), etc. To access these settings, in the AVD Manager, select an emulator from the list, and click the Edit this AVD icons actions edit svg icon on the right. In the Virtual Device Configuration dialog click the Show Advanced Settings button:
    AVD advanced settings

    These parameters are then passed to the emulator as command line parameters.

  8. In the Run/Debug Configurations dialog, click OK to save the newly created build configuration.

  9. Note that if you selected an x86 application binary interface when configuring an Android virtual device, you will also need to install the Intel x86 Emulator Accelerator before you can run and test the application, otherwise IntelliJ IDEA will throw an error when you try to build your project. To do this:
    1. Launch the SDK Manager: in the main menu, select Tools | Android | SDK Manager.

    2. Scroll down to the folder called Extras.

    3. In this folder, locate Intel x86 Emulator Accelerator (HAXM installer), select the corresponding checkbox and click the Install packages button.

    4. In the dialog that opens, accept the license agreement and click Install.

    5. Navigate to the Android SDK installation directory (the SDK path is shown on top of the Android SDK Manager window). In the \extras\intel\Hardware_Accelerated_Execution_Manager folder, locate the intelhaxm-android.exe file and double-click to launch it.

    6. Follow the instructions of the Intel Hardware Accelerated Execution Manager setup wizard to complete the installation.

Test the Application on an Emulator

To build your project, press Shift+F10 or click the Run icon icons toolwindows toolWindowRun svg in the Navigation bar in the top-right corner of the editor:

Android run project

The project will be built according to the run/debug configuration currently selected in the drop-down list. If no errors occur, IntelliJ IDEA packages the binaries and resources into an APK file and uploads it to the Android emulator:

Android upload APK to emulator

If the Android emulator is not already up and running, IntelliJ IDEA will initialize it before uploading the application. The emulator receives and installs the package and starts it by invoking the specified launch activity:

Android nexus5 hellodroid

Test the Application on a Physical Device

To test your application on a real Android device, you need to:

  1. Add a run/debug configuration and make sure that you select USB device under Target Device.

  2. Make sure that an Android device is connected to the computer through a USB cable.

  3. Press Shift+F10 or click the Run icon icons toolwindows toolWindowRun svg in the Navigation bar in the top-right corner of the editor.

If the installation of your application fails, most probably this means that the device is not configured to install applications outside of the Android application stores. Enable this capability on your device to test the application.

On Android 4.2 or higher, do the following:

  • Open your device's Settings.

  • Scroll to About phone or About tablet and tap it.

  • Scroll to the bottom and tap Build number 7 times until you see the "You are a developer!" message. By doing so, you've unlocked the USB debugging mode on your device.

  • Now navigate to Settings | Developer Options | Debugging | USB Debugging to let your phone deploy non-packaged applications.

Debug your Application

IntelliJ IDEA allows you to launch your application in the debug mode on an emulator or a physical device using the corresponding run/debug configuration.

To debug your application, you need to set breakpoints in the source code: place the caret on an executable line and click the left gutter area. A red circle will appear next to the line where you want to toggle a breakpoint, and the line will be marked with pink:

Android breakpoint

To start a debug session, press Shift+F9 or click the Debug icon icons actions startDebugger svg in the Navigation bar in the top-right corner of the editor. The Debug tool window will be activated where you can step through the program, examine variables, watches, frames and threads and analyse system information and error messages in the Console tab.

Analyze Debug Output

In IntelliJ IDEA, debugging of Android applications is provided through the support of the logcat functionality that stores a log of system debug output. Log messages include a stack trace when the emulator throws an error, so you can navigate to the exception location in the source code.

In IntelliJ IDEA, the logcat functionality is available through the Logcat tab of the Android Monitor tool window (for details, see Debugging with Logcat):

Android logcat

You can also configure additional logcat options in the Miscellaneous tab of your run/debug configuration:

android run debug miscellaneous
Last modified: 20 November 2018

See Also