Android 12 source code analysis - application layer 1 (SystemUI preparation)

Android 12 source code analysis - application layer 1 (SystemUI preparation)

In the coming time, Pixel 3 (blueline) will be used as the research object, and the android-12.0.0_r34 branch of AOSP will be selected as the source code.

First, we will analyze the application layer of Android, then slowly go into the framework of Android, then enter the hal layer of Android, and finally end with the Linux kernel of Android. Some other articles may be interspersed during this period, such as analysis of Android's art virtual machine, etc.

This article is the first in the entire series, but before that, please be sure to read the following articles, they will appear from time to time in future source code analysis

  1. How android analyzes application memory (2) - xdd, gdb command line ( http://t.csdn.cn/RENbG)
  2. How to analyze application memory in android (3) - LLDB command line ( http://t.csdn.cn/Vpw8e)
  3. How to analyze application memory in android (4) - LLDB of Visual studio code ( http://t.csdn.cn/8ZM8A)
  4. How android analyzes application memory (fourteen) - jdb command line ( http://t.csdn.cn/99r2G)
  5. How to analyze the memory of android applications (15) - Visual Studio Code debugging Android applications ( http://t.csdn.cn/dWeF5)

The articles listed above are about how to use gdb, lldb, jdb for android source level debugging. It is very useful without IDE.

As the beginning of the application layer, this article selects SystemUI as the research object.

Why choose SystemUI?

There are many applications in the Android application layer that can be used for source code analysis, such as Launcher, Camera, Gallery, Bluetooth, SIM, telephony, Settings and other applications. Considering that SystemUI may be a module with more customization, it is necessary to carefully analyze it. analysis.

The primary goal of this article is - to build an environment for analyzing SystemUI - how to use Android Studio for AOSP development and debugging

Note: When using Android studio, there are certain requirements for PC performance. If the PC performance is indeed not enough, you can refer to the 5 articles listed above and use VS code instead. In the previous five articles, there was no mention of how to build a java project, only how to debug it. Readers can refer to other information about: Use of Extension Pack for Java plug-in. With the previous 5 debugging articles, AOSP development can also be carried out.

How to use Android Studio for AOSP development and debugging

To use Android Studio to edit SystemUI, you need to configure the following:

  1. Use the aidegen tool in the AOSP source code to build dependent modules
  2. Using JDK from AOSP source code
  3. Use the SDK in the AOSP source code
  4. How to make AndroidManifest.xml and various resource xml can refer to each other
  5. How to modify the static code analysis tool lint to solve various red errors in the IDE (actually non-errors)
  6. How to step through SystemUI using Android studio

Next we solve the above steps in turn.

Use the aidegen tool to build dependent modules

In the aosp source code, after loading the compilation environment, that is, after running the following command:

. build/envsetup.sh
lunch

aidegen is ready to use.

Next, use the aidegen tool to generate a project configuration file that can open SystemUI in as. as follows:

aidegen SystemUI -i s -p /media/wanbiao/disk1t/root/IDE/android-studio/bin
# SystemUI:表示要生成工程文件的模块
# -i s:表示生成的工程文件对应的IDE为Android studio。
#        j=IntelliJ s=Android Studio e=Eclipse c=CLion v=VS Code
# -p <路径>:表示对应的IDE的安装路径,在生成工程文件完成之后,会自动打开IDE
# 其他常见选项如下:
# -n:表示不用打开IDE
# -s:表示跳过编译各种依赖,如果以前运行过make等命令,可以添加-s
# -e:表示排除一些目录,这个非常有用,尤其是大型模块
# -d:源码引用的模块的深度
# -r:重置所有的aidegen保存的配置
# -v:显示debug级别的log
# -a:生成整个Android 源码树的工程文件
# -l:用指定的语言打开IDE,j=java,c=c/c++,r=Rust
# -h:打开帮助

After running successfully, you will get the following picture
Insert image description here

In the frameworks/base/package/SystemUI directory, several files will appear:

  1. .idea folder, the project folder used by Android studio, how many modules can be configured in it. As you can see from the picture above, there are three modules: R, SystemUI, dependencies
  2. SystemUI.iml: Configuration file for configuring the SystemUI module
  3. dependencies.iml: configure the configuration file of the dependencies module

Next we will configure the JDK and SDK for the above modules. In this way, as can jump between java classes correctly.

Using JDK from AOSP source code

You can use the which javac command to view the specific jdk path as follows:
Insert image description here

Then open Android studio, add this jdk to the configuration,

  1. File->Project Structure
  2. Configure as shown below
    Insert image description here

Insert image description here

As shown above, we selected the jdk in the aosp directory and named the jdk configuration: aosp-jdk11

Next configure the sdk

Use the SDK in the AOSP source code

In the android-12.0.0_r34 branch, the SDK is not compiled by default. Therefore, run the following command to compile the source code.

. build/envsetup.sh
lunch
make sdk -j8

During the Android compilation process, errors may be reported, such as: module can not be located. Or the xxx file does not exist.

Solution: Just find the corresponding error file, then open it to see if the corresponding module exists. If it does not exist, change it to the correct path. Of course, you can simply comment out the error line, provided that you can be sure that the line will not affect the function of the SDK.

After successful compilation, it will appear in the following path:

./out/host/本机平台/sdk/aosp_blueline/android-sdk_eng.wanbiao_linux-x86

Then add the SDK as shown below.

Insert image description here

Insert image description here

Note: In the picture above, make sure that the JDK used inside the SDK is the JDK we configured earlier, that is, aosp-jdk11. At the same time, make sure that the build target is Android API 31.

Associate SDK and JDK with SystemUI project

The above two steps only add the corresponding SDK configuration and JDK configuration, but have not yet been associated with SystemUI. Please refer to the following steps to associate it.

Insert image description here

Next, configure the SDK for each module under the project, as shown below
Insert image description here

After clicking apply, there will be a period of indexing...please wait for a moment.

So far, as already has the functions of editing and jumping java code. Before configuring xml resources, we need to solve several problems
as follows:

  1. How to correctly jump to the source code instead of jumping to the SDK
  2. How to deal with duplicate files in source code

How to correctly jump to the source code

In the above configuration, if we jump to the source code, it will jump to the SDK first. In order to successfully jump to our source code instead of the SDK,
there are two ways:

  1. Modify the priority of dependencies
  2. Remove android.jar in sdk

Both methods need to be modified, the project structure. We use the following pictures to explain the two methods.
The first method:
Insert image description here

The first approach usually needs to be modified frequently, because as often moves the sdk to high priority

The second way:
Insert image description here

After the above configuration, you can correctly jump to the location in the source code

How to deal with duplicate files

We deal with duplicate files by marking their folders as exclude directories, as shown in the figure
Insert image description here
below. The figure above excludes the duplicate SystemProperties.java under the Stub module from the source file.

Note: To add the exclude folder, in addition to the above right click, you can also add the following format through the corresponding .iml file:

<content url="file://$MODULE_DIR$/../../../../system/tools/sysprop/stub">
    <sourceFolder url="file://$MODULE_DIR$/../../../../system/tools/sysprop/stub" isTestSource="false" />
    <excludeFolder url="file://$MODULE_DIR$/../../../../system/tools/sysprop/stub/android/os" />
</content>

Note again: In addition to the above two methods, you can also modify it by opening the project structure, as shown below

Insert image description here

Configure Android

The above processing can only perform jumps and configurations between java codes, and we also need to process AndroidManifest.xml and resource files. Configure as follows.

  1. Add Android
    Insert image description here

  2. Select the manifest file and resource folder. The manifest file and resource folder are required
    Insert image description here

Note: In Android, only one resource folder can be added at a time. If there are multiple resource folders to be edited, you can modify the configuration here. For example, if I want the IDE to give me the correct prompt when editing resources in res-product, I can change the Resource directory here to the corresponding folder.

So far, Android resources can basically be used in the IDE. But before using it, there are still two issues that need to be dealt with:

  1. When referencing system resources, jump to source code instead of sdk
  2. Handle errors reported in IDE, but use them correctly

How to adjust to system resources

In my example, in order to be correctly associated with system resources, I deleted the resource folder in the compiled sdk, and then pointed it to the framworks/base/core/res/res directory through the link folder. Just use the ln command and won’t go into details again.

Handle illegal usage in xml

In AndroidManifest.xml, if system permissions are used such as:

<uses-permission android:name="android.permission.BIND_CONTROLS" />

Another example is using a useid that does not belong to the application.

android:sharedUserId="android.uid.systemui"

These will be marked with red lines in the IDE.

Another example is using it in the resource folder

@*android:integer/config_mediumAnimTime

Accessing non-public resources will also result in an error.

These are all functions of static code inspection tools, and we will make some modifications to them.

In response to the above errors, the following modifications can be made:
Method 1:

  1. After opening the AndroidManifest.xml file, right-click
  2. 选中Analyze->Configure Current File Analysis
  3. Finally choose Syntax

The above steps are to tell the IDE to only check the syntax of the current file and not to do other checks

Method 2:
The above is just a very rude way to turn off the prompt. You can also perform fine conditions in the opened panel through File->Settings->Editor->Inspections. This involves a detailed interpretation of the inspect function, so I won't repeat it here.

Method three:
In addition to operating through the graphical interface, you can also configure it in the lint.xml file in the current directory.
The options supported by lint.xml can be obtained through the following command:

./prebuilts/devtools/tools/lint --list

So far, you can freely edit java code and xml code in Android Studio.

Of course, you can also turn a blind eye to the error report on the IDE editor, which has no substantial impact on our editing work.

Debugging in Android studio

Regarding how to debug Android applications in Android studio, I will not introduce it here. Please refer to:
How to analyze the memory of android applications (5) - LLDB of Android studio: http://t.csdn.cn/jSurw and android How to analyze the application memory Memory (15) - Visual Studio Code debugging Android applications: http://t.csdn.cn/wHOgd

This article only introduces how to make SystemUI debug correctly.

In fact, when debugging SystemUI, you will find scenarios where local variables cannot be debugged. To solve this problem. The following steps need to be processed:

  1. When confirming compilation, add javac -g option
  2. Turn off the optimization configuration in AOSP

Make sure the compiled class file contains debugging information

In the article How to analyze application memory in Android (14) - jdb command line http://t.csdn.cn/GtbBH , the command to use javap to view is explained.

Next, we directly check whether SystemUIApplication.class contains debugging information.
Insert image description here

If there is no output above, try modifying as follows:

1. Android.mk中增加:LOCAL_JAVACFLAGS += -g
2. Android.bp中增加:javacflags:["-g"]

Then recompile. and confirm again if there is still no debugging information. You can try switching to the userdebug version

Turn off module optimization in AOSP

In the link of AOSP generating apk, there is an optimization link, which includes resource compression, useless code stripping, obfuscated code, etc. In order not to destroy the debug information, we need to turn off optimization. as follows:

1. Android.mk中添加:LOCAL_PROGUARD_ENABLED := false
2. Android.bp中添加:optimize:[enabled:false]

After completing the above steps, you can turn off the optimization function and view local variables in single-step debugging.

Start debugging code

In order to allow SystemUI to wait for the debugger to join when it starts, we run the following command:

adb shell am set-debug-app -w com.android.systemui

For a detailed explanation of the above code, please refer to: How to analyze the memory of an application in android (15) - Visual Studio Code Debugging Android Applications http://t.csdn.cn/JPBjC The starting test section of the article

Debugging results

The single-step debugging result is as shown below

Insert image description here

In the above figure, you can go step by step and complete the familiarity with the entire execution process.

This article is the basic part of our analysis of SystemUI, an essential part, because it can be run in a single step, which reduces a lot of work in analyzing source code. To put it bluntly, work less overtime.

So far, how Android studio edits and debugs the source code of SystemUI is introduced.

Starting from the next article, we will officially enter the source code of SystemUI to see how the fascinating SystemUI is written.

In addition, as an aside, if your PC performance is okay, you can use some AI tools to assist programming. For example, the author is using the tabnine plug-in. Another example is using variable extractor to translate the object into json format, and then use the tool to view it graphically. , such as the Debug Visualizer tool mentioned in the previous article

Guess you like

Origin blog.csdn.net/xiaowanbiao123/article/details/132366807