Two ways to view AOSP in AndroidStudio

Author: Geralt of Rivia

This article mainly shares some methods of using AndroidStudio to view aosp in my usual work. At the same time, I hope that the big guys who know other convenient and effective viewing and debugging methods can give me advice, share with each other, and make progress together.

If you directly open the aosp root directory with AndroidStudio, then open any Java class, the default may be like this:

The tab page of the Java file displays the icon as

, which means "Java class located out of the source root", and its internal member variables and the like are not syntax highlighted.

After our configuration, the recognized Java file is shown in the following figure:

The tab page of the Java file shows the icon as
, and the member variables are also highlighted.

More importantly, at this time, the code has already established an index, such as code completion (the code here not only completes the fields defined in the current class, but also completes the fields of the parent class and other classes. area):

Or check which classes a domain is called by:

And a series of convenient functions such as code jumping.

1 mainstream method

1.1 Setup steps

The first method should be known to many people. I have used this method for a long time since I came into contact with Android. This method requires a compiled AOSP.

1), first make sure that it has been executed:

soruce build/envsetup.sh

Wait for the command to load the environment variables required for compilation.

2), then execute:

mmm development/tools/idegen/ -j16

After successful compilation, it will output:

Before compiling here, I set the environment variable of the generated directory to out_sys, so the generated file is in out_sys.

3) At this time, you can execute the command:

./development/tools/idegen/idegen.sh

If your generated directory is also in out_sys like mine, you may need to create a new out/host/linux-x86/framework/ directory, and then copy idegen.sh to it:

cp out_sys/host/linux-x86/framework/idegen.jar out/host/linux-x86/framework/

When seeing output similar to:

It will be ok, and finally two files will be generated in the aosp root directory, android.ipr, android.iml:

4) Open this android.ipr through Android. Since it is the first time to open the source code, all modules will be indexed, so it takes a very long time, and the AndroidStudio card cannot be used for a long time. This is the main reason why I gave up using this method to view aosp. To be honest, I personally haven’t used this method for a long time. This time I tried this method again for writing this article. , sometimes you can't even tell whether the index is really being built or it's just stuck.

1.2 Advantages and disadvantages

Because the card time is too long and I don’t want to wait, so the following steps will not be demonstrated. In fact, there is no content. Let me talk about the possible useful skills under this viewing method:

  • Set the code directory you don't use to excluded. This operation seems to be of little use in this way.
  • In android.iml, change the code directory that you don't use from sourceFolder to excludeFolder. This method is very useful. It can help you filter out many irrelevant files whether it is when indexing or when you view aosp later. code.
  • Delete all the jar packages under Project Structure -> Modules -> Dependencies to avoid jumping to other messy places when the code jumps.

This way of viewing aosp has the advantage of indexing all modules at one time, but it is also a disadvantage. The first loading is extremely slow because of the need to index, and subsequent re-opening is much faster than the first time, but it is also relative. Personally, I think it takes a long time.

In addition, the method of modifying android.iml mentioned earlier is indeed a good method, but it is not without disadvantages. For example, it may be troublesome to configure this. Although this configuration operation is once and for all, if you change the code of another project, Then you need to reconfigure the android.iml of this project, and you need to go through a long process of indexing. What I did at the time was to reuse android.ipr and android.iml. For example, I configured these two files on project A. Then when I downloaded a set of code for project B, I could directly copy the project Copy the android.ipr and android.iml of A to project B to reuse these two files. The advantage is that it does save a lot of time and energy. The disadvantage is that if the names or directories of the files you want to view in the two projects happen to be different (for example, the Android platform has been upgraded), it may be troublesome, and there may be some I An unknown problem that has never been encountered.

All these things make me eager to find another way to view aosp.

2 Easy way

2.1 Setup steps

I have complained a lot about the shortcomings of the first method, but there is another point that cannot be ignored, that is, you need a set of compiled aosp. In many cases, I may only pull the code of a certain library in aosp, such as frameworks /base library, so I can't build an internal index for viewing?

The answer now is yes, and it's actually quite simple. But here is still an example of the whole set of aosp, the operation is the same:

1) For example, I downloaded a set of project codes now, and then directly opened the root directory of the project in AndroidStudio. Initially, all directories are like this:

2) At this time, close the indexing operation in the lower right corner:

3), and then first set all directories to Excluded:

After the directory is set to Excluded, it looks like this:

This step can also be omitted. I just hope that the irrelevant modules will be excluded at the beginning to facilitate subsequent indexing and viewing of codes.

4), and then reopen AndroidStudio, at this time you will find that the step of building the index is gone, because all directories have been excluded.

5), and then you want to see which codes, mark the java directory or src directory where these codes are located as Sources Root (this step should be based on the configuration principle of IntelliJ IDEA, but I have not found specific theoretical support content), For example, I often read WMS-related content, in

frameworks\base\services\core\java\com\android\server\wm\

package, then I can put

frameworks\base\services\core\java

This directory is set as Sources Root, like this:

After the directory is set as Sources Root, the result is:

At this point, you can re-index this directory. For example, I create a new test method in the WindowContainer.java class to see which methods this can call:

You can see the function of code completion.

Check in which classes the isDescendantOf member method is called, or:

I won't introduce too much about the others. I personally use this way of viewing aosp.

2.2 Special case of jumping to SDK

Let me talk about the situation that may be encountered here, that is, the code jump may be transferred to the SDK instead of aosp. For example, I wanted to jump to the Rect class through Ctrl + left mouse button, and found that it jumped to the SDK. Instead of Rect.java in aosp:

At this time, you need to depend on the SDK in Project Structure -> Modules -> Dependencies:

Replace with local JDK package:

After selection is like this:

At this time, try to jump to Rect again, and it will be fine:

If not, it may be because the location of Rect.java is:

frameworks\base\graphics\java\android\graphics\Rect.java

You need to put the java directory of the directory where Rect.java is located:

frameworks\base\graphics\java

Set as Sources Root:

3 IntelliJ IDEA Supplements

Configure folder structure

Folder categories

Folders within a content root can be assigned to several categories.

This folder contains production code that should be compiled.

The IDE considers that files in the Generated Sources folder are generated automatically rather than written manually, and can be regenerated.

These folders keep code related to testing separately from production code. Compilation results for sources and test sources are normally placed into different folders.

The IDE considers that files in this folder are generated automatically rather than written manually, and can be regenerated.

(Java only) Resource files used in your application: images, configuration XML and properties files, and so on. During the build process, resource files are copied to the output folder as is by default. You can Change the output path for resource files in your project.

Similarly to sources, you can specify that your resources are generated. You can also specify which folder within the output folder your resources should be copied to.

These folders are for resource files associated with your test sources.

Files in excluded folders are ignored by code completion, navigation and inspection. That is why, when you exclude a folder that you don’t need at the moment, you can increase the IDE performance. Normally, compilation output folders are marked as excluded.

Apart from excluding the entire folders, you can also exclude specific files

Configure folder categories

Android study notes

Android Performance Optimization: https://qr18.cn/FVlo89
Android Vehicle: https://qr18.cn/F05ZCM
Android Reverse Security Study Notes: https://qr18.cn/CQ5TcL
Android Framework Principles: https://qr18.cn/AQpN4J
Android Audio and Video: https://qr18.cn/Ei3VPD
Jetpack (including Compose): https://qr18.cn/A0gajp
Kotlin: https://qr18.cn/CdjtAF
Gradle: https://qr18.cn/DzrmMB
OkHttp Source Code Analysis Notes: https://qr18.cn/Cw0pBD
Flutter: https://qr18.cn/DIvKma
Android Eight Knowledge Body: https://qr18.cn/CyxarU
Android Core Notes: https://qr21.cn/CaZQLo
Android Past Interview Questions: https://qr18.cn/CKV8OZ
2023 Latest Android Interview Question Collection: https://qr18.cn/CgxrRy
Android Vehicle Development Job Interview Exercises: https://qr18.cn/FTlyCJ
Audio and Video Interview Questions:https://qr18.cn/AcV6Ap

Guess you like

Origin blog.csdn.net/weixin_61845324/article/details/132063310