In all IntelliJ IDEs, you run and debug your application binaries and tests using run configurations, which specify what target to run and a variety of options for how to run that target.
Bazel run configurations are different from normal IntelliJ run configurations because they are organized around Bazel targets (rather than "modules" and "variants"). Run configurations are managed and launched from this collection of widgets at the top of the IDE:
Run configurations created by default
The IDE automatically creates run configurations for supported
targets explicitly listed in your project view (not pulled in
/...:all expression). If most of your team builds, launches,
and debugs the same few android_binary targets, you should probably add those
targets to a checked-in .bazelproject file.
Run configurations for all other targets must be created manually.
Creating a run configuration manually
To create a run configuration (or edit existing configurations), go to the run configuration dropdown and click Edit Configurations...
- Add/delete/copy a configuration.
- Select the Bazel Command run configuration type. This works for all types of targets (Android/Java/generic). The UI and options will vary depending on the target type.
- Select the target you want to run/debug.
- Customize the launch settings as you see fit. For binaries, you can pick activity launch settings. For tests, you can add test filters. For configurations delegating to Bazel, you can add flags. androidbinary and androidtest targets share a large number of device-oriented settings.
- IntelliJ loves confirmations, so you get two! Your changes are not saved until you click OK or Apply.
Creating and running test configurations with right-click menu
You can quickly create supported test configurations by right-clicking on classes and methods. The tests in the corresponding Bazel test target will be filtered to only run the tests in the target class, or to only run that test method.
- Right click on a class (in the project view, the structure view, the file tabs, or in a source file), or a method (in the structure view or a source file), or a test target in a BUILD file.
- Go to Create a Run Configuration, Run or Debug (to create a run configuration or immediately launch it).
- Select the type of test target as appropriate. If you pick the wrong kind of test, it will fail.
You can re-run the same configuration by launching it from the top of the IDE or right-clicking on the same class or method.
Note: If your bazel target is missing from your project view, try File > Edit Project View... > Save and Sync.
Note: Improved button icon submissions are welcome!