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
- Add a New Run/Debug Configuration
- Test the Application on an Emulator
- Test the Application on a Physical Device
- Debug your Application
- Analyze Debug Output
Review the default run/debug configuration
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:
- In the Run/Debug Configurations dialog, click the Add button and select Android Application from the list:
- 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,
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.
- 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.
When you have selected a module, the red cross disappears, as now your run/debug configuration is filled with the minimum required information.
- 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.
- 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
AndroidManifestfile, or select a custom activity. Since your sample application only has one activity, select the Default Activity option.
- 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:
- 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:
- 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:
- On the last step, you can modify the AVD name and select the startup size and orientation of the virtual device screen:
- Click Finish to complete the wizard. The newly created Android virtual device will be added to the devices list:
- Return to the Run/Debug Configurations dialog.
- 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 icon on the right. In the Virtual Device Configuration dialog click the Show Advanced Settings button:
These parameters are then passed to the emulator as command line parameters.
- In the Run/Debug Configurations dialog, click OK to save the newly created build configuration.
- 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:
- Launch the SDK Manager: in the main menu, select Tools | Android | SDK Manager.
- Scroll down to the folder called Extras.
- In this folder, locate Intel x86 Emulator Accelerator (HAXM installer), select the corresponding checkbox and click the Install packages button.
- In the dialog that opens, accept the license agreement and click Install.
- 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.exefile and double-click to launch it.
- 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 in the Navigation bar in the top-right corner of the editor:
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:
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:
Test the Application on a Physical Device
To test your application on a real Android device, you need to:
- Add a run/debug configuration and make sure that you select USB device under Target Device.
- Make sure that an Android device is connected to the computer through a USB cable.
- Press Shift+F10 or click the Run icon 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:
To start a debug session, press Shift+F9 or click the Debug icon 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.
You can also configure additional logcat options in the Miscellaneous tab of your run/debug configuration: