At this point, you can already experience the thrill of compiling your first Android application and have it deployed to the Android emulator or to a physical device. Before you build the project and start testing the application, it is recommended that you review the current build configurations.
1. Edit Run/Debug configurations
From the Run → Edit configurations menu, you get the dialog box below.
IntelliJ IDEA creates a default build configuration named after the project and sets it with defaults settings like the preferences you expressed during the creation of the project. The dialog box allows you to edit existing configurations or add new configurations. Let's see how.
2. Add a new build configuration
On the toolbar, click + and select Android Application.
A new item will be added to the tree view with the default name Unnamed. Select it and enter a more significant name in the Name input field: for example, HelloDroid Emulator.
The newly created configuration is rendered with a red cross to indicate it is not ready yet to be used. The reason is that it lacks essential information.
Set the executable module
Expand the Module drop-down and select HelloDroid Emulator: the only executable module in the project. As you select the module, the red cross disappears meaning that the configuration is now complete.
Set the launch activity
Any Android executable module needs a starter activity. Your choice is to use the activity configured as the launch activity in the manifest file or a specific activity. In this case, both default activity or com.example.HelloDroid.MyActivityproduce the same result as the sample application has only one activity.
Set automatic deployment
You decide whether you want the application to be automatically deployed to the target device or not. Most of the time, you just want automatic deployment. However, opting out is as simple as unchecking the Deploy application checkbox.
Choose the Target Device
Finally, you need to assign a target device to the build configuration. In this case, you choose Emulator.
When you set the emulator as the target device, you also need to select an Android virtual device manager (AVD). You can create a new AVD by clicking on the ... button and then interacting with the dialog box in the figure:
An AVD is just a configuration file through which you define defining hardware and software options for the emulator to mimic. In addition to the AVD settings, IntelliJ also offers a few parameters through the Emulator tab. For example, you can set the network latency parameter to simulate a GPRS connection.
Notethat parameters you can set via the IntelliJ IDEA user interface are then passed to the emulator as command line parameters. In case of need, you can also specify additional command line parameters using the text box in the figure.
3. Build the Project
To build the project, you just press Shift + F10or click Run. In both cases, the project will be built according to the currently selected configuration: the same you see in the drop-down list.
By clicking on the drop-down list, or pressing Alt + Shift + F10you bring up a chooser dialog box that allows you to pick up the build configuration of choice.
4. Test the application on the emulator
IntelliJ IDEA builds the file and, if no errors occur, it 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 start and initialize it before uploading the application. The emulator receives and installs the package and starts it invoking the configured launch activity.
5. Test the application on a real device
To test the application on a real device you just need to select the appropriate build configuration and make sure the device is connected to the computer through a USB cable. If there's enough storage on the device, the application installs successfully.
A possible reason for the install to fail is that the device is not configured to install applications outside one of the Android app stores. Enabling this capability on the specific device is required to test applications.
6. Debug the application
Finally, IntelliJ IDEA allows you to set breakpoints in the source code to debug the application. To start a debug session, you explicitly click on Run → Debug...menu items (as opposed to Run → Run...menu item). The IDE does the usual build work and stops execution at the first breakpoint for you to step over.
You debug the application in the same way regardless of where it runs, whether in the emulator or on a real device.
7. View logged events
Android has its own logging system that automatically collects debug output from applications and system. In IntelliJ IDEA, you can view these events in the Logcat window. The Logcat window is where you receive notifications from the Android runtime environment during the execution of the application.
The messages captured by the Logcat window can be filtered through the Log level drop-down list. By default, the Logcat window accumulates messages until you explicitly clear the log through the Clear log button.
Note that the build configuration dialog box you've seen earlier in this page also has a Logcat tab with a check box to clear the log right after the application is launched.