This article will show you how to debug the Framework using Android Studio. With this skill, any Java layer problem in Android development can be solved by actually observing the Framework layer in action.

Why debug the Framework

For Android development has been introduced to the students, it is inevitable to contact the “View drawing process”, “Touch event distribution” these knowledge points, we can through others to understand these knowledge points, but a more thorough way of learning is to read the Android Framework layer of the relevant source code.

Students who have read experience of Framework layer source code will know that the scale of Framework layer source code is extremely large, in the face of this big pile of complex source code, we really need to read everything? In fact, for application developers, we don’t need to read the source code of the Framework layer carefully, after all, we don’t customize the Framework. We usually just focus on the “mainline” of the Framework layer code logic, which is the Happy path, which is the code path that normally (without exception) executes. By debugging the Framework, we can quickly “pick up” the Happy path of a piece of code in the Framework layer.

Take the View measure() as an example:

public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
  boolean optical = isLayoutModeOptical(this);
  if(optical ! = isLayoutModeOptical(mParent)) {// Block A}..}Copy the code

The code listed above must be seen when we analyze the View drawing process. Now the question is, will block A execute? Obviously, if we do not know the meaning of optical variables, it is difficult to solve the problem, and if we do not know the meaning of local variables when analyzing the process of View measure, we will look up information, which may discourage our enthusiasm and reduce the efficiency of reading source code. And we usually do not need to know the meaning of each line of code, such as for the purpose of mastering the custom View analysis View drawing process, we only need to grasp the View drawing will be executed under the circumstances of the code path (happy path), can master the logic of the View drawing process.

As for the answer to the above question, once we have mastered the debugging Framework, we can know what an optical variable actually represents without knowing. Let’s take a look at debugging the Framework.

How to Debug the Framework

Here’s how to debug the Framework using Android Studio. In addition to Android Studio, we need to prepare the following “ingredients” before debugging:

  • SDK source code, can be downloaded through SDK Manager;
  • The SDK version of the Android real machine or emulator must be consistent with the source SDK version.

To use a real Android device, it is important to use an Android device that runs Android native, such as the Nexus series. If you use a third-party ROM, you will encounter code that “breaks” in blank lines because the SDK source code is not the same as the code in the device. Therefore, ensure that the SDK source version is consistent with the SDK version on the device.

If the method we want to debug is exported from the SDK, then all we need to do is download the SDK source code (no explicit import, Android Studio will associate it for us automatically). And the code we want to debug is not in the SDK, such as the system application, we need to manually import this part of the source code.

There are two ways to Debug in Android Studio. One is to simply go to “Run –> Debug ‘XXX'”. The other option is to click “Run –> Attach debugger to Android Process” at runtime. The first will debug the application process by default, while the latter can choose which process to debug. If we want to track the process of drawing a View, we usually just need to debug the application process, because the main process of drawing a View is running in the application process. However, if we want to trace the startup process of an Activity, then debugging the application process alone is not feasible, because much of the code logic is executed in the system_server process when the Activity is started, so we need to debug the system_server process. We can only debug the “debugable process”. The simulator’s system_server process is debugable, as demonstrated here using the simulator (Genymotion).

A simple demo

In fact, the process of debugging the Framework is the same as that of debugging a normal Android app. We just interrupt at key points and resume/step in/ Step over as needed.

Here we use the view.measure () method as an example to demonstrate the effect of debugging the Framework. The simulator software I use here is Genymotion, running Android 5.1 native system, and the SDK source code version associated with Android Studio is Also Android 5.1.

Now let’s answer the question posed at the beginning of the article. Create a New Hello World project in Android Studio, then set a breakpoint on the first line of the view.measure () method and hit “Run –> Debug ‘app'”. When the code runs, we can see that the first line of view.measure () is broken, as shown below:




Then we set the following two breakpoints and let the program continue running:




You can see that the program is broken here:




This indicates that the then block of the if statement does not execute, i.e. the happy path of the measure() code does not contain the then block of the initial if statement.


Follow the public account and reply “simulator” in the background to obtain the Genymotion installation package.