How to learn the Android Framework, how to go from the application to the Framework?

Preface

As an author who basically started reading the source code from 0 and has completed a series of technical articles on source code analysis, I think my experience can be used for reference.

How to learn the Framework source code in depth?

First of all, I am also an application layer developer. I think most of the questions about "how to go deep into the framework source code" should be mostly application layer development.

For us, the biggest problem with reading the source code is that there is no application scenario, or in the short term, the cost is high, the profit is low, and it is easy to give up halfway.

One,

To solve this problem, first of all, we must have a certain degree of determination and research spirit, which part of the source code analysis is going to win, even if we encounter many problems, we must find a way to solve it, and we must complete the goal set by ourselves. Secondly, what direction should you start with? As the subject said, there are a lot of source codes. The aosp of ndroid11 ​​is downloaded as a whole, about 150G, so it is very important to find a starting point, otherwise it will only be eaten in the hard disk after the source code is downloaded. Gray

(The picture above shows the size of the aosp source code of Android11)

In terms of application layer development, here are a few interviews that are more frequently asked and easier to get started.

  1. Four major components start-up process
  2. Application startup process
  3. System startup process
  4. Audio related content

The 4 small points here seem to actually take at least half a year to do it, because the content involved is both rich and deep. As far as the first point is concerned, the Activity startup process will be enough for you to do at least two weeks. There will be a meeting here. involving ActivityThread, AMS, Zygote, Bindercross process calls and a series of knowledge

Here is an additional sentence. I saw that Weishu answered that he should not pay attention to the tracking of various processes. In fact, I do not agree with it. Of course, this is just my opinion based on my knowledge and cognition

The same is true for the majority of application layer developers. We have to understand our own positioning. Xiaobai will take the road of Xiaobai, and the big brother will take the road of big brother. Personally, I think that whether it is a call chain with various system processes or It’s good to sort out the whole piece by system service, these are all "processes", and our goal is to go deep into the framework source code. How can we go deep into the source code without even following the call chain?

Of course, I also agree with the big guy Weishu that it is necessary to analyze the thoughts and principles involved, but this is the second layer. Without the foundation of the first layer, I want to do the second layer. It is tantamount to a castle in the air.

two

Back to the topic, we have already figured out where to start. The second problem to be solved is what kind of foundation do we need to understand the source code, or have the ability to read the source code.

I personally think that when you ask the question of how to learn the Framework source code in depth, you already have the most basic conditions-the desire for exploration and the desire for knowledge. Of course this thing is relatively imaginary, let me talk about some real things

At present, the underlying code of the new version of AOSP has basically been refactored in C++, so if you want to go deep into the native layer, such as the handler we mentioned most often, there is actually a set of implementations in the native layer, which will pass when you get the message. The pipeline mechanism performs wake-up notifications to avoid blocking problems such as death. Does that mean that we must first have C++ or C language foundation to read the source code? In my opinion, it is good to have a foundation, and it will not have much impact if there is no foundation. It may be more efficient to continue to read the source code after learning C++.

Therefore, in my opinion, no matter what your foundation is, as long as you have application layer development experience, and have the interest and desire to explore and study Framework, this is enough. Just start, it's progress

three

The third point I want to talk about is how deep is appropriate. When I read the source code, I often dig deeper and deeper with a single call chain. For example, when studying the system startup process, even to the virtual machine level and assembly level, but generally speaking, we don’t need to dig so deep. It’s not necessary. Secondly, it does take a lot of energy and it’s hard to see results.

Therefore, when I am studying a certain point, I will split this point into small questions. To give a specific example, I asked myself these questions when I was studying SystemServer related processes.

  1. How SystemServer was Forked
  2. What did SystemServer do
  3. What is SystemServiceManager responsible for?
  4. How is SystemServiceManager created?
  5. What is ServiceManager responsible for?
  6. How many ways are there to start the service? What is the difference between them?
  7. What is the difference between SystemServiceManager and ServiceManager?
  8. What is LocalService responsible for?
  9. SystemServer is the server process, so who is the client process? How do they communicate? What is the internal mechanism? (Binder related issues)

Of course, you may not be able to ask questions at first, or you don’t know what important classes are involved in it. We can read related articles first and have a general idea. At this time, we can ask some basic questions, and then read the source code. China Re keeps asking questions and summarizing, this is also my steps and ideas for writing source code analysis articles

four

The fourth point I want to mention is that there must be positive feedback. Many people do not have concentration, but they just feel unable to read. The big reason is that there is no positive feedback. My positive feedback comes from my article output, article reading and blog attention, and the ability to communicate with my friends (but most of the time, the deeper the direction, the more limited people can communicate)

I also suggest that you can communicate more when learning framework, produce articles and results, and inspire yourself to continue on this road.

Fives

Fifth point, I need to remind everyone that

If your work involves related content (such as plug-in, audio and video, launcher, setting, AudioManger, etc.), please study the relevant source code first

If not involved, you can refer to the starting point I mentioned above for research. Only if you have the ability to read and research can you better complete the more challenging ones, and even enter the ranks of Framework development.

six

Finally, let’s talk about the benefits of reading the source code. After you have studied one or two modules, look at it, and you may have a deeper experience.

As said by the great god Weishu, the benefit of studying frameworks is not to remember which call flows are remembered. These are not the purpose (but it is indeed an indispensable process!). Our purpose is from a deeper or more holistic perspective. Look at our technology, such as the four major components that are the most commonly used in our development, but fragments are also commonly used in our development. Why can't it be called the "fifth largest component"?

After I studied the source code of the four major components, I found the biggest feature of the four major components-support for cross-process, their startup process will involve whether my process is started, whether I need to communicate with zygote to fork out the process, and then Then execute the startup logic of the component itself, so the heavyweight of the four major components is very heavy, and the fragment is only a part of the activity that depends on it, and it is far from such a heavyweight, so naturally it cannot become the "fifth largest component" Up

Then again, the benefits of reading the source code

  1. It is to have a deeper understanding of application layer development;
  2. When encountering some difficult problems, we have the ability to dig into the cause of the problem by reading the source code, and finally solve the problem;
  3. It lies in the overall improvement of the ability to read the source code. When we look at the source code of other third-party libraries, it will be more handy. Even AOSP, a behemoth of nearly 200G, can handle it. Okhttp is simply the younger brother in front of it.

At last

The editor is here to share with you a learning document of "Android Framework Compiled Kernel Analysis" that I have collected and sorted out, and I hope it can be helpful to everyone. Please click on the GitHub address: https://github.com/733gh/Android-T3 to view. If you like this article, you might as well give me a small like, leave a message in the comment area, or forward and support it~

Guess you like

Origin blog.csdn.net/u012165769/article/details/113853289