Android Studio for beginners, Part 4: Debugging tools and productivity plugins

Use built-in tools and plugins to improve code quality and boost productivity in Android Studio

Debugging tools and productivity, Android Studio offers a wealthy palette of built-in improvement tools, and a fair greater ample plugin environment. The first 3 articles on this series targeted on installation and setup and coding and going for walks a simple cellular app with Android Studio.

Now you may get familiar with some of the greater superior tools: three integrated gear and 3 plugins for debugging and enhancing the excellent and productivity of your Android application code.

This article introduce the subsequent gear for Android Studio:

Built-in debugging tools

  • Logcat
  • Debug
  • Lint

Plugins for developer productivity

  • ADB Idea
  • Codota
  • Lombok Plugin

Logging and debugging Android apps

A buggy app is a sure manner to power away capability customers. Logcat and Debug are equipment that come built-in to Android Studio, which you could use to discover and connect insects on your Android code.

Logging Android: Logcat

You may not want or need a full-blown debugger for every “buggy” situation. In some cases it’s sufficient to log and correct the values of sure variables. Android Studio’s Logcat is ideal for this mission.

Logcat is a command-line tool (included into Android Debug Bridge — ADB) that dumps a log of machine messages that will let you locate and fix troubles on your code.

Messages include stack strains for while the device throws an error, and log messages that you’ve purposefully embedded with the android.Util.Log class.

Logcat is so useful it is been included at once into Android Studio, where you could get right of entry to it from the View menu or the tool window bar.

Logging Android

The Logcat window is divided into a horizontal row of drop-down lists and different controls followed by way of a message area. From left-to-right, the controls are:

  1. A drop-down listing identifying linked gadgets and emulators. (See Figure 1.)
  2. A drop-down listing figuring out hooked up APK programs on the chosen device.
  3. A drop-down list of logging levels:
  • Verbose: display all messages.
  • Debug: show debug log messages which might be useful in the course of improvement simplest, in addition to the message stages lower inside the drop-down list.
  • Info: show anticipated log messages for ordinary usage, as well as the message ranges decrease inside the drop-down listing.
  • Warn: display possible troubles that are not but errors, in addition to the message stages decrease within the drop-down listing.
  • Error: show issues which have brought on errors — thrown exceptions, as well as the Assert message degree.
  • Assert: display issues that the developer expects ought to by no means take place.
  1. A search bar in which the simplest messages that seem are the ones containing the textual content you type in this manipulate.
  2. A test field that helps you to use everyday expressions inside the seek bar. For example, within the context of Figure 1, you may specify SystemClock have to include both word).
  3. A drop-down listing for filtering messages, inclusive of the following options:
  • No Filters (the default) reasons all messages for the selected tool to seem, no matter the selected APK bundle.
  • Show simplest decided on application affords only the ones messages for the selected APK package on the chosen device, which can be of the best log level or decrease.
  • Edit Filter Configuration lets you create a custom filter out.

Logcat

In the supply code, you can insert calls to diverse android.util.log techniques to log messages. For example, you might name public static int i(String tag, String msg) to log an INFO-level message, or public static int w(String tag, String msg) to log a WARN-stage message.

For both approach, the string surpassed to tag identifies the message’s supply (e.G., the technique wherein this logging approach turned into known as), and the string surpassed to msg identifies the message being logged.

Alternatively, you could name System.Out’s and System.Err’s print() and println() strategies. Calls to the System.Out methods accomplish. The same aspect as log.i() calls. calls to the System.Err methods accomplish the equal component as log.W() calls.

In Figure 2, the handiest displayed messages are for the instance app procedure running at the emulated Nexus 5X tool. Furthermore, these messages should incorporate onCreate and be prevalent through the Show simplest decided on application clear out.

Debugging Android: Debug

Using Logcat to log and accurate code is okay for very simple apps. For more complex apps, this shape of debugging may be tedious. Instead you’ll want something that helps you to debug the app’s executable code.

Debugging tools and productivity, Android Studio’s built-in Debug device offers many talents, which include the subsequent:

  • Select a tool on which to debug your app.
  • Set breakpoints to your utility code (Java, Kotlin, or C/C++).
  • Examine variables and evaluate expressions at runtime.

There are a few stipulations earlier than the use of this debugging tool:

  1. If your app consists of C/C++ source code, you will need to install LLDB from the SDK Manager (see Figure three). Fortunately, the instance app for this collection (W2A) would not incorporate C/C++ code, so we will forget about this prerequisite.
  2.  

2. You need to enable debugging on a related tool. However, in case you’re the use of an emulator (which we are for this case), you may forget about this prerequisite. Debugging is enabled by using default on emulated devices.

3. You must run a debuggable construct version. By default, this is created for you, so in lots of instances (together with this example) you don’t should worry approximately it.

Let’s run through a quick debugging session.

Debugging tools and productivity, Simple Android debugging

Assuming you’re strolling Android Studio with the editor window for the example app (W2A.Java) open, your first step is to set a breakpoint on the public void onCreate(Bundle savedInstanceState) line. Set the breakpoint through highlighting this line and doing one of the following:

  • Click the gutter area to the left of the road; the line should flip purple in response.
  • Select Toggle Line Breakpoint from the Run menu.
  • Press Ctrl and F8 simultaneously.

Debugging tools and productivity, Now pick out Debug ‘app’ from the Run menu, or click the suitable icon at the device bar.

If the emulator is not running, you will have a look at the Select Deployment Target dialog field. I selected Nexus 5X API 15 and clicked OK.

While you’re expecting the emulated tool to return on-line, click the Debug button on the tool-window bar to display the Debug window. Eventually, you should see something similar to Figure 4.

The Debug window affords icons for stepping over, into, and out of a way, and more. Figure 5 shows what happens when we step over a method.

Debugging tools and productivity

Click the suitable icon and select Step Over from the Run menu, or press F8. This shall we us proceed deeper into the onCreate() approach while stepping over each approach name.

Select the road with the final person for the onCreate() technique, then choose Run to Cursor. You’ll obtain a note that the application is jogging. After unlocking the tool, you must see the example app’s interest window.

More about Debug Check out Google’s “Debug your app” to learn more about debugging in Android Studio; for example, how to use various kinds of breakpoints.
Debugging with GAPID While not covered here, I recommend checking out GAPID: Graphics API Debugger, a collection of debugging tools that let you inspect, tweak, and replay calls from an Android app to a graphics driver.

Debugging tools and productivity, Code inspection with Lint

No count how carefully you write it, your source code is in all likelihood to contain insects, stylistic problems, and may reference resources that are inactive for one cause or another. A linter is a kind of device that scans supply code for proof of these and other bits of nasty fluff, which it reviews to the developer.

Debugging tools and productivity, Lint is a integrated linter for Android SDK. You can use it to locate problems inclusive of deprecated elements and API calls that are not supported through your goal API.

To run Lint from inside Android Studio, pick Inspect Code… From the Analyze menu. Doing so activates the Specify Inspection Scope conversation container.

Debugging tools and productivity, From the dialog box, select the favored scope (whole venture, in this example), then click on OK to begin the inspection.

The outcomes will seem in the Inspection Results window, in which they may be organized by category.

True to its nature, Lint has picked out some portions of fluff that would in any other case litter the application code. Once spotted, it is clean to fix the 3 Java warnings shown in Figure 8: definitely claim androidAnimation personal and do away with the casts from the 2 findViewById() technique calls.

More about Lint Check out “Improve your code with lint exams” to learn more about the use of Lint in Android Studio, and to study the Android SDK’s standalone Lint tool.

Installing and using Android Studio plugins

While integrated tools have their merit, many different equipment are to be had as plugins. In this section, we’re going to observe 3 plugins for enhancing the productiveness of your coding in Android Studio:

  • ADB Idea
  • Codota
  • Lombok Plugin

Android Studio plugins manager

Android Studio’s plugins supervisor makes it very clean to locate and deploy plugins. Activate the plugins supervisor by means of selecting File > Settings observed through Plugins from the Settings conversation field:

Next, click Browse repositories… To spark off the Browse Repositories dialog field, which gives a full list of supported plugins. We’ll use this dialog to choose our first plugin, ADB Idea.

Boost your Android productivity

Debugging tools and productivity, Code shortcuts: ADB Idea

ADB Idea hurries up everyday Android improvement through providing rapid get right of entry to to normally used ADB commands, together with beginning and uninstalling an app.

Debugging tools and productivity, Select ADB Idea within the repository list of plugins, then click the Install button. Android Studio proceeds to download and install the plugin.

It then relabels Install to Restart Android Studio. For every plugin, I needed to reselect the plugin after clicking Restart Android Studio, after which click this button a 2d time as a way to restart Android Studio.

Once installed, Android Studio lets you get entry to ADB Idea from its Tools menu. Select Tools > ADB Idea and pick out the suitable command from the ensuing pop-up menu.

Once hooked up, Android Studio lets you get admission to ADB Idea from its Tools menu. Select Tools > ADB Idea and pick the correct command from the resulting pop-up menu.

Debugging tools and productivity, Find usable code: Codota

You can use the Codota plugin to get right of entry to the Codota search engine, which helps you to look through millions of publicly to be had Java supply code snippets for answers to Android coding problems.

Select Codota within the repository list of plugins and then click the Install button. After Android Studio has downloaded and established the plugin, it’s going to re-label the Install button to Restart Android Studio. Restarting turns on Codota.

Upon restart, you may stumble upon the Codota Authentication conversation field. Click the link and follow commands to acquire the token. Then, paste the token into the aforementioned conversation container and click Go.

Debugging tools and productivity, Android Studio lets you get entry to Codota via proper-clicking on Java code in the editor window and deciding on the Get applicable examples menu item.

Clicking this menu object effects in the browser providing a web page with applicable code examples. For example, the subsequent page affords examples that are applicable to findViewById:

Debugging tools and productivity, Autogenerate Java code: Lombok

Project Lombok gives a hard and fast of annotations that you may use in area of boilerplate code, thus saving you time writing out this code by using hand.

Select Lombok Plugin in the repository list of plugins, then click on Install. After Android Studio downloads and installs the plugin, it will prompt you to Restart Android Studio. Restarting activates Lombok Plugin.

A easy example will exhibit Project Lombok’s usefulness. Suppose we add the subsequent minimum Robot class to the instance app’s W2A.Java supply file:

class Robot { private String name; }

The concept with this class is that we will save the call of the robotic being animated and potentially any other records that is probably useful, and display this data as a toast (a small message that appears like a device tip).

Debugging tools and productivity, We should take a couple of minutes to add a constructor and getter/setter techniques to this code.

However, a better choice is to permit Lombok add this boilerplate for us. Here are the steps:

  1. Right-click on Robot.
  2. Select Refactor from the pop-up menu.
  3. SLombok from the next pop-up menu.
  4. Select Default @Data from the next pop-up menu.

You need to now take a look at an @lombok.Data annotation prefixed to the magnificence Robot header. This annotation reasons the constructor, getter/setter methods, and other boilerplate code to be auto-generated.

You can see all of this boilerplate by means of repeating the previous steps, but this time pick Delombok in preference to Lombok. You’ll see that Lombok has generated something like the following:

class Robot
{
    private String name;

    public Robot() {
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean equals(final Object o) {
        if (o == this) return true;
        if (!(o instanceof Robot)) return false;
        final Robot other = (Robot) o;
        if (!other.canEqual((Object) this)) return false;
        final Object this$name = this.getName();
        final Object other$name = other.getName();
        if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
        return true;
    }

    protected boolean canEqual(final Object other) {
        return other instanceof Robot;
    }

    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final Object $name = this.getName();
        result = result * PRIME + ($name == null ? 43 : $name.hashCode());
        return result;
    }

    public String toString() {
        return "Robot(name=" + this.getName() + ")";
    }
}