As I discovered yesterday, it is very easy to debug Android source code in Android Studio

0. Preparation

Install a plugin for Android Studio: ADB Idea

The plugin uses ADB to perform some common operations:

  • ADB Uninstall App
  • ADB Kill App
  • ADB Start App
  • ADB Restart App
  • ADB Clear App Data
  • ADB Clear App Data and Restart
  • ADB Revoke Permissions
  • ADB Start App With Debugger
  • ADB Restart App With Debugger

To use these functions :Tools->Android->ADB Idea Menu

1. Create a Demo

Create a new project in Android Studio with any name you want. Then targetSdkVersion selects the Android version of your phone or emulator.

Then run it on your phone or emulator.

The best phones here are test phones like the Pixel, Google’s own son after all. Or use the Official or Genymotion simulator. Domestic Source code is customized by the manufacturer, a lot of things have been modified, debugging may appear Source code does not match the Bytecode.

2. Open the source code you want to debug

I’m going to pick a random one here, like the source of View. Double-click Shift, type View, and click on it (check include non-project items if no source code for the View appears). I’m going to pick a random method here, like measure method. I put multiple breakpoints on the left side of the Measure method

3. Start debugging

To do so, select the ADB Restart App With Debugger menu of ADB Idea

Here we go, bro. Now AS will stop at the breakpoint you just hit, and we can debug AS usual

4. SAO operation

You can even select a variable and press F2 to change its value

5. SolveSource code does not match the bytecodeThe problem

Sometimes we want to use Huawei or Xiaomi phone, and then we want to debug, how to say? Source code does not match the Bytecode. Let’s try to work it out.

This time to run the above demo to the millet mobile phone. Then we look at the source of View, and its package name is

package android.view;
Copy the code

We will create a package name like this in the demo, and then copy the View source in full.

In your own View file, find the measure method again, make multiple breakpoints on the left, or choose the ADB Restart App With Debugger menu of ADB IDEA

Then we found that we came to the third line, indicating that our code and xiaomi phone source code difference of 2 lines (I’m just here for example, your phone may be 4 lines or 1 line difference is possible), because this time should stay in the first line.

And if you look closely, the red circle on the left, with a little check mark on it, indicates that this line can be debugged. The gray circle with a gray line in the middle indicates that the line cannot be debugged (probably a comment in the source code).

So how do you solve this 2 line difference problem? I added 2 blank lines to measure’s comment.

See, the gray circles on the left disappear, and then all red circles, indicating that the number of lines being debugged corresponds to the number of lines in the source code. At this point we press F6, continue to debug happily, and proceed to the next line.