Android Studio offers a rich palette of development tools, and it’s compatible with many plugins. The first three articles in this series focused on basic tools for building simple mobile apps. Now you’ll get acquainted with some of the more advanced tools that are part of Android Studio, along with three plugins you can use to extend Android Studio.
We’ll start with Android Device Monitor, Lint, and Android Monitor–three tools you can use to debug, inspect, and profile application code in Android Studio. Then I’ll introduce you to plugins ADB Idea, Codota Code Search, and Project Lombok.
Debugging with Android Device Monitor
Android Device Monitor is an Android SDK tool for debugging failing apps. It provides a graphical user interface for the following SDK tools:
- Dalvik Debug Monitor Server (DDMS): A debugging tool that provides port-forwarding services, screen capture on the device, thread and heap information on the device, logcat, process, radio state information, incoming call and SMS spoofing, location data spoofing, and more.
- Tracer for OpenGL ES: A tool for analyzing OpenGL for embedded systems (ES) code in your Android apps. It lets you capture OpenGL ES commands and frame-by-frame images to help you understand how your graphics commands are being executed.
- Hierarchy Viewer: A graphical viewer for layout view hierarchies (the layout view) and for magnified inspection of the display (the pixel perfect view). This tool can help you debug and optimize your user interface.
- Systrace: A tool for collecting and inspecting traces (timing information across an entire Android device). A trace shows where time and CPU cycles are being spent, displaying what each thread and process is doing at any given time. It also inspects the captured tracing information to highlight problems that it observes (from list item recycling to rendering content) and provide recommendations about how to fix them.
- Traceview: A graphical viewer for execution logs that your app creates via the
android.os.Debugclass to log tracing information in your code. This tool can help you debug your application and profile its performance.
To launch Android Device Monitor from your command line, execute the
monitor program in your Android SDK’s
tools directory. If you prefer to run the tool from Android Studio, choose Tools > Android > Android Device Monitor.
You might remember from Part 1 that I used Android Studio to launch my W2A example app in the Nexus 4 emulator. I then launched Android Device Monitor from Android Studio. Figure 1 shows the resulting screen.
The Devices tab shows all accessible devices, which happens to be the emulated Nexus 4 device in this example. Underneath the highlighted device line is a list of currently visible
android.app.Activity subclass objects.
I highlighted the
W2A activity object identified by its
ca.javajeff.w2a package name, then clicked Hierarchy View to activate the Hierarchy Viewer tool. Figure 2 shows the result.
Hierarchy Viewer displays a multipane user interface. The Tree View pane presents a diagram of the activity’s hierarchy of
android.view.View subclass objects. The Tree Overview pane offers a smaller map representation of the entire Tree View pane. The Layout View pane (whose contents are not shown in Figure 2) reveals a block representation of the UI. See “Optimizing Your UI” to learn more about the Hierarchy Viewer tool and these panes.
If you attempt to run Hierarchy Viewer with a real (non-emulated) Android device, you could experience the error messages that appear in Figure 3.
These messages refer to the view server, which is software running on the device that returns
View objects diagrammed by Hierarchy Viewer. Production-build devices return these error messages to strengthen security. You can overcome this problem by using the
ViewServer class that was created by Google software engineer Romain Guy.
Inspecting code with Lint
Lint is an Android SDK code-inspection tool for ensuring that code has no structural problems. You can use it to locate issues such as deprecated elements, or API calls that aren’t supported by your target API.
Although Lint can be run from the command line, I find it more helpful to run this tool from within Android Studio. Select Analyze > Inspect Code to activate the Specify Inspection Scope dialog box shown in Figure 4. Then select your desired scope (whole project, in this case), and click the OK button to perform the analysis. The results will appear in the Inspection Results window, where they are organized by category.
As you can see in Figure 5, Lint has spotted a few issues:
Lint also complained about the following:
- A missing
contentDescriptionattribute on the
main.xmlhampers the app’s accessibility.
- The root
main.xmlpaints the background white (
#ffffff) with a theme that also paints a background (inferred theme is
@style/AppTheme). Overdrawing like this can hurt performance.
dimens.xmlfile specifies three dimensional resources that are not used. Specifying unused resources is inefficient.
- On SDK v23 and up, the app data will be automatically backed up and restored on app install. When you specify an
@xmlresource that configures which files to backup, consider adding the attribute
AndroidManifest.xml; otherwise you might face a security issue.
- Support for Google app indexing is missing.
- I stored
drawable, which is intended for density-independent graphics. For a production version of the app, I should have moved them to
drawable-mdpiand considered providing higher and lower resolution versions in
drawable-xhdpi. No harm is done in this example, however.
- Lint checked my spelling, noting the reference to
See “Improve Your Code with Lint” to learn more about using Lint in Android Studio.
Profiling with Android Monitor
Profiling running apps to find performance bottlenecks is an important part of app development. Android Device Monitor’s Traceview tool offers some profiling support. Android Monitor offers even more.
Android Monitor is an Android Studio component that helps you profile app performance to optimize, debug, and improve yours apps. It lets you monitor the following aspects of apps running on hardware and emulated devices:
- Log messages (system-defined or user-defined)
- Memory, CPU, and GPU usage
- Network traffic (hardware device only)
Android Monitor provides real-time information about your app via various tools. It can capture data as your app runs and store it in a file that you can analyze in various viewers. You can also capture screenshots and videos as your app runs.
You can access Android Monitor via Android Studio’s Android Monitor tool window. Select View > Tool Windows > Android Monitor or just press Alt+6:
Figure 6 reveals the Android Monitor tool window, which presents drop-down list boxes that identify the device being monitored (in this case, on my Amazon Kindle Fire device) and the app being debugged on the device. Because ADB integration hasn’t been enabled, “No Debuggable Applications” appears in the latter list. Check Tools > Android > Enable ADB Integration to enable ADB integration.
After enabling ADB integration, I observed that “No Debuggable Applications” was replaced in the drop-down list with “ca.javajeff.w2a,” the package name for the W2A application that was running on my Kindle.
Below the two list boxes are a pair of tabs: logcat and Monitors. The former tab shows logged messages from the device and the latter tab reveals graphics-based memory, CPU, network, and GPU monitors (see Figure 7).
The memory monitor shown in Figure 7 reveals that the app occupies almost 13 megabytes and its subsequent memory usage is constant, which isn’t surprising because the app doesn’t make any explicit memory allocations, and the underlying APIs probably don’t require much additional memory. The CPU monitor shows only a slight amount of CPU use via a narrow red line about 1 minute into the monitoring. This usage arose from clicking the Animate button several times. No networking activity is displayed because the app isn’t making network requests. Finally, the GPU monitor is disabled because I’m running an older version of Android (4.0.3), which doesn’t support GPU monitoring.
The left side of the Android Monitor tool window contains a small tool bar with buttons for obtaining a screenshot (the camera icon), recording the screen, obtaining system information (activity manager state, package information, memory usage, memory use over time, and graphics state), terminating the application, and obtaining help. I clicked the camera button and obtained the screenshot shown in Figure 8.
See “Android Monitor Overview” to learn more about Android Monitor.
Extending Android Studio apps with plugins
Android Studio’s plugins manager makes it very easy to find and install plugins. Activate the plugin manager by selecting File > Settings followed by Plugins from the Settings dialog box:
Next, click Browse repositories . . . to activate the Browse Repositories dialog box, which presents a full list of supported plugins:
I’ll introduce three useful plugins–ADB Idea, Codota Code Search, and Project Lombok– and show you how to install and use them.
ADB Idea speeds up your day-to-day Android development by providing fast access to commonly used ADB commands, such as starting and uninstalling an app:
Select ADB Idea in the repository list of plugins and then click the Install button. Android Studio proceeds to download and install the plugin. It then relabels Install to Restart Android Studio. Restarting activates ADB Idea.
Android Studio lets you access ADB Idea from its Tools menu. Select Tools > Android > ADB Idea and choose the appropriate command from the resulting pop-up menu:
The app must be installed before you can use these commands. For example, I selected ADB Restart App and observed the following messages as well as the restarted app on my Amazon Kindle device.
Codota Code Search
Use the Codota Code Search plugin to access the Codota search engine, which lets you look through millions of publicly available Java source code snippets (on GitHub and other sites) for solutions to coding problems:
To install this plugin, select Codota in the repository list of plugins and then click the Install button. After Android Studio has downloaded and installed the plugin, it will relabel the Install button to Restart Android Studio. Restarting activates Codota Code Search.
Android Studio lets you access Codota Code Search by right-clicking on Java code in the editor window and selecting the Search Open Source (Codota) menu item (or by pressing Ctrl+K), as shown in Figure 15.
Android Studio responds by displaying the Search Codota dialog box whose text field is blank or populated with the full package name of the Java API type that was right-clicked. Figure 16 shows this dialog box.
Codota Code Search passes the search text to the Codota search engine and presents vertically scrollable search results in a CodotaView tool window.
What Can We Learn From TikTok?December 7, 2022
Clean Energy At Home – How Do Solar Generators Work?December 4, 2022
Vintage Ads From The Past That We Don’t See TodayDecember 3, 2022