Android Studio debugs Android Framework layer code

First, preparation

Download the latest version of Android Studio

Developer.android.com/studio/inde…

2, download the Android system source code

This is flexible, you can download the code of the company’s internal model, as well as the native AOSP code

Environment Configuration Reference

Source.android.com/source/init…

Source code download reference

Source.android.com/source/down…

Compile run reference

Source.android.com/source/buil…

Compile the entire code project, if there are any questions can refer to

source.android.com/source

Configure Android Studio

1. Configure VM options

A completed Android source code will be very large, so in order to avoid OOM when importing and generating index, it is best to configure the VM options of Android Studio in advance, you can refer to my configuration below

2. Set the maximum number of open files

During the import process, a large number of files will be opened at the same time. In order to avoid the error of too many Files open, configure the number of files to be opened in the profile file in advance. I set 2048 here

Create an Android Framework project

After the entire Android source code is compiled successfully, you also need to compile the IDEgen module to generate the Android Studio project configuration file, for Qualcomm has the modem source code project here there is a pit, is an HLOS under an Android soft link will link to the source root directory, The stack overflows and the project configuration file cannot be generated, so the soft link needs to be deleted

1. Compile idegen module separately

Vincent$ mmm development/tools/idegen/

After a successful compilation, the idegen.jar is generated

host Java: idegen (out/host/common/obj/JAVA_LIBRARIES/idegen_intermediates/classes) Install: out/host/darwin-x86/framework/idegen.jar

2. Run the IDEgen script

Vincent $development/tools/idegen idegen. Sh run after success will see the following information is hit

Read excludes: 5ms Traversed tree: 85308ms

At the same time, generate engineering configuration files such as Android. Ipr in the source code root directory

3. Speed up code import

For code directories that you don’t care about, you can modify android.iml to remove useless source code and related directories to improve loading speed

4. Import and configure the Android Framework project

1. Import project

Open Android Studio, then locate the android.ipr file generated in the root directory and open it

Wait about 10 minutes for the index to be established. The time may vary with hardware configurations

2. Configuration engineering

There will be a prompt in the upper right corner when the import is complete

And then I’m gonna hit Configure

Click OK, then click Project in the upper left corner and select the Project View mode

Then enter the project configuration according to the following figure

Delete the classpath and Sourcepath contents of the JDK as follows to locate the source code in the project during debugging

Then delete the Android SDK’s classpath and sourcepath as well, and select the Java SDK as just configured JDK1.8

Set Modules dependencies and remove all dependencies except those shown in the following figure

Then click on the + in the lower left corner to select Jars or Directories to add a framework related source folder

Click OK and wait for index creation to complete and the Attach Debugger to Android Process toolbar will become clickable

Debug the Android Framework

After the above configuration is complete, you can start online debugging, but only if the ro.debuggable is 1 in the phone, or userDebug, ENG version of the system software

Attach to the system_process process

2. Set breakpoints

After attaching successfully, open the source file where you want to set the breakpoint, and left-click set breakpoint in the space to the right of the line

3. Check the runtime status

After setting up, you can operate the phone. When the code runs to the breakpoint, you can stop. Then you can view the status of the runtime, including the value of variables, backtrace, etc

You can also add the value of the expression you want to view in the Watches window

4. Obtain the Backtrace snapshot

In addition, after attaching, snapshots of backtrace of all threads can be obtained in real time without setting breakpoints. The function is similar to kill -3, but only trace of Java layer

Contact information:

Wechat official account –>> He Xiao