Profiler and Inspection

Android Studio 2: The New UI

Course 2 ยท Chapter 8 ยท The Profiler and App Inspection

Logcat and the debugger (Chapter 7) answer "what did the code do?". This chapter is about "how well did it do it?" and "what is it showing and sending?". Three tool windows cover it: the Profiler (CPU and memory), App Inspection (network traffic and the live layout) and the Build Analyzer (why the build is slow).

What was checked, and how: read this first
Checked on the install (screenshots, Rabbit 2): the Profiler's task-based start screen and that App Inspection and Profiler are in the View > Tool Windows list. Checked in the docs (one web search, summaries only): the Network Inspector and the Build Analyzer, marked (docs). General knowledge, not checked: marked (general knowledge). The search returned nothing on the Layout Inspector or the Profiler tasks in detail, so those parts are thin and labelled.

The Profiler: Start from a Task

The Classic UI Profiler opened on a timeline with CPU, memory, network and energy rows. On Rabbit 2 the start screen asks what you want to find out and offers tasks (checked on the install):

Task on the start screenBehind it
Capture System ActivitiesSystem Trace.
View Live TelemetryA live view of the app's resource use.
Find CPU HotspotsJava/Kotlin Method Recording, or Callstack Sample.
Analyze Memory UsageHeap Dump.
Find Memory Leaks with LeakCanaryUses the LeakCanary library's leak detection (general knowledge).
Track Memory ConsumptionJava/Kotlin Allocations, or Native Allocations.

There is no Network task in that screenshot, and no Power or Energy task was visible (I was told one might exist; that is unconfirmed). Network moved to App Inspection, below. A heap dump is a snapshot of the objects in memory, used to find what is holding on to it; sampling and method recording show which functions take the time (general knowledge). You profile a running app, so start a device first (Chapter 6). A blog item I found says Android Performance Analyzer is the updated System Trace viewer in recent builds (docs, unconfirmed for Rabbit 2).

App Inspection and the Network Inspector

(docs) The Network Inspector lives in the App Inspection tool window: View > Tool Windows > App Inspection, then its Network Inspector tab. It shows real-time network activity on a timeline (data sent and received), with two views of a selected stretch of time:

  • Connection View: the files sent and received.
  • Thread View: which of your app's threads made each request.

(docs) It supports HttpsURLConnection and OkHttp, which means libraries built on OkHttp such as Retrofit can be inspected too. A Rules tab can change responses (status code, headers, body) so you can see how the app copes with, say, an error. I could not check the Rules tab on the install because the app was not running when I looked.

The Layout Inspector

(general knowledge, not found by my search) The Layout Inspector is also an App Inspection tool. It shows the running app's UI as a tree and a picture, so you can click an element and read its size and properties. That is the right tool for "why does this look wrong on the device?", and it works for Compose as well as XML. Treat the details as unchecked.

The Build Analyzer

(docs) After each build the Build Analyzer produces a report. Open View > Tool Windows > Build and click the Build Analyzer tab. It can point out configuration problems, for example dynamic dependency versions that cause unexpected downloads. Chapter 4's version catalog (fixed version numbers) is the usual cure for that one.

Compared with the Classic UI
Android Studio: The IDE Itself 5 teaches the Profiler's timeline and its Network Profiler. On Rabbit 2 the Profiler starts from tasks, and network traffic is inspected in the Network Inspector inside App Inspection (confirmed on the install: no Network task exists).

Try It Yourself

Exercise 1

Open the Profiler with the app running and list every task it offers. Compare your list with the table above and note differences (especially anything about Power or Energy).

๐Ÿ“„ View solution
Exercise 2

Open App Inspection and find the Network Inspector and the Layout Inspector. In the Layout Inspector click an element of your running app and report which properties you can read. If your app makes no network calls, say what the Network Inspector shows instead.

๐Ÿ“„ View solution
Exercise 3

Run Build > Rebuild Project, then open the Build Analyzer tab in the Build window. Write down what its report says about your build, and whether it lists any warnings.

๐Ÿ“„ View solution
Not Yet Verified
Checked on the install: the Profiler's six task entries and the absence of a Network task; App Inspection and Profiler in the View > Tool Windows list.
Checked in the docs (one web search, summaries only): the Network Inspector's location, timeline, Connection View, Thread View, OkHttp support and Rules tab; the Build Analyzer's location and purpose; the Android Performance Analyzer note.
Not verified: the Layout Inspector (nothing found); any Power or Energy task; what each Profiler task records and shows; what the Rules tab looks like; the Build menu path used in Exercise 3 (general knowledge); the exercises (not run by me). A screenshot of the Network Inspector with a running app that makes requests, and one of the Layout Inspector, would verify this chapter.
Sources: Inspect network traffic with the Network Inspector (developer.android.com), Troubleshoot build performance with Build Analyzer (developer.android.com), Introducing Android Performance Analyzer (Android Developers' Blog).
Coming up
Chapter 9 covers the Code menu: Inspect Code, Code Cleanup, Analyze Code, Analyze Stack Trace and the Refactor menu, and why there is no Analyze menu any more.