The Code Menu

Android Studio 2: The New UI

Course 2 ยท Chapter 9 ยท Inspect, Analyze and Refactor

The IDE can check your code without running it (inspections and lint), tidy it in bulk (Code Cleanup) and restructure it safely (refactoring). Older guides send you to an Analyze menu for the first group. On Rabbit 2 that menu is gone, and the commands live in the Code menu. This chapter shows where everything went.

What was checked, and how: read this first
Checked on the install (a screenshot of the Code menu, and your confirmation): the entries of the Code menu and the absence of an Analyze menu. Checked in the docs (one web search, summaries only): Code > Inspect Code and Code Cleanup, marked (docs). General knowledge, not checked: marked (general knowledge). The search found nothing on Analyze Stack Trace, Play Policy Insights or the Refactor menu, so those are described only by name and by what I know of the IDE.

Where the Analyze Menu Went

The main menu (Chapter 1) has: File, Edit, View, Navigate, Code, Refactor, Build, Run, Tools, VCS (or Git), Window, Help. There is no Analyze. The Code menu now contains (checked on the install):

Code menu entryWhat it is for
Inspect Code(docs) Manually runs the configured lint and other IDE inspections; results appear in the Inspection Results window.
Inspect for Play Policy InsightsChecks the project against Google Play policy (general knowledge from the name; not checked).
Code Cleanup(docs, from JetBrains' IntelliJ documentation, which Android Studio is built on) Applies quick-fixes in bulk to a chosen scope without asking about each problem.
Analyze Code >A submenu of further analysis tools (I did not open it).
Analyze Stack Trace or Thread DumpPaste a crash's stack trace and get clickable links into your source (general knowledge).

Android Studio's lint page names the path as Code > Inspect Code; an older Android page still says Analyze > Inspect Code (docs). Same command, older location. The Code menu also holds the editing commands you use daily (reformat, optimise imports and so on, general knowledge).

Lint and the Problems Window

Lint is Android's static checker: it finds likely bugs, missing translations, accessibility and performance problems. Inspect Code runs it together with the IDE's own inspections. Issues also appear as you type (underlines in the editor) and in the Problems tool window, Alt+6 (checked on the install). Use Inspect Code when you want the whole project checked at once rather than the open file.

# Same lint check from the command line, in the Terminal tool window (Alt+F12) ./gradlew lint

(general knowledge: the Gradle task name is standard, but I have not run it on this install; on Windows use gradlew lint.)

The Refactor Menu

Refactoring has its own top-level menu now. (general knowledge, the menu's contents were not captured) The ones to learn first: Rename (changes every use of a name, Shift+F6), Extract (turn selected code into a function, variable or constant) and Safe Delete (delete only if nothing uses it). They check the whole project, which is why they beat find-and-replace.

Compared with the Classic UI
Android Studio: The IDE Itself 7 teaches "Analyze > Inspect Code". On Rabbit 2 use Code > Inspect Code. The Rename, Extract and Safe Delete material in that chapter is still correct.

Try It Yourself

Exercise 1

Open the Code menu and write down every entry. Check there is no Analyze menu. Open the Analyze Code submenu and list what it contains.

๐Ÿ“„ View solution
Exercise 2

Add an unused variable and a hard-coded string to a Kotlin file, then run Code > Inspect Code on the whole project. Find your two problems in the results and say how the results are grouped.

๐Ÿ“„ View solution
Exercise 3

Rename a function with Refactor > Rename and check that every call changed. Then copy a stack trace (invent one if needed) and try Analyze Stack Trace or Thread Dump. Report what each did.

๐Ÿ“„ View solution
Not Yet Verified
Checked on the install: the five Code menu entries above, the absence of an Analyze menu, the Refactor menu's existence and the Problems shortcut (Alt+6).
Checked in the docs (one web search, summaries only): Code > Inspect Code and the Inspection Results window; the older Analyze > Inspect Code path; Code Cleanup as described for IntelliJ and Rider (not Android Studio's own page).
Not verified: the Analyze Code submenu; what Play Policy Insights and Analyze Stack Trace actually do; the Refactor menu's contents and shortcuts; the gradlew lint command; the exercises (not run by me). A screenshot of the Analyze Code submenu and the Refactor menu would verify this chapter.
Sources: Improve your code with lint checks (developer.android.com), Run inspections (jetbrains.com).
Coming up
Chapter 10 covers Git in the New UI: the Version Control widget, the branch widget, the Commit window (Alt+0) and the Git window (Alt+9).