During those years of staying in Changsha, I saw two types of people around me. One kind of people did business logic repeatedly, just wanted to write out functions, and went to and from work repeatedly every day without too much bother. There are also those who constantly reflect, summarize and learn, who do not just stay at the level of doing, but really like doing this job and find it very interesting. There is no good or bad, just different pursuit of everyone. But if we want to go to a bigger company, or find a job that pays a little bit more, there will be a lot of things to do, and one of them is to read source code, so in this episode we’ll focus on some of the postures of reading source code. Before we actually embark on this road, let’s be clear about the following points:
-
No one can read the source code right away. We all started with Hello World, so there are no shortcuts. It’s just a matter of who takes the most time and who is more willing to mess around.
-
Everyone has to go to work, why others get high wages and good benefits, while their own size week, occasionally need to work overtime all night. I only got 10K for three or four years while other people got 20K. Notice THAT I said 20K. I wrote an article before “From 3K to 30K, what happened to me at the age of 23”. This time I was also afraid that some guys would spray, which is so high? We must have a good faith in our hearts, to have the passion for continuous progress.
In the process of improving our ability, we can get a lot of things from it, and our heart will become stronger. The key is that when we do a project, we will have a lot of success, which can be exchanged for money and time, provided that we are willing to exchange time.
I. Common tools
The first is our development tool Android Studio, here we take a specific example, suppose now I want to see setContentView source code, so we can directly follow up to the source code method inside:
If we look inside and find an abstract class, we must find the implementation class. In general, we can search (CTRL + F) to find where the instance was created:
It’s red, and we can’t follow it anymore. In this case, we can try a global search (double click shift).
But in many cases we can not find the global search, next to introduce you to the second tool, we can browse the source code online to read: http://androidxref.com, which not only has the Java layer source code and native layer source code
Online viewing is generally more accurate to see which class, and can not meet our needs for quick viewing. I just want to see it in Android Studio, so I can quickly follow it down. In fact, we usually download the source code when downloading the SDK, but it just happens that the source code of the class is not included in the Android. jar package, so we can’t find it. At this time, we need a relatively complete Android. jar to replace the android.jar file in our SDK, the key is how to make this JAR? The best way is to compile it yourself, but many friends may feel that the compilation cost is too high, so we can also go to Github to download. https://github.com/anggrayudi/android-hidden-api keep original change a name, to download a copy, and then restart the Android Studio to see again, find not only not red and can click on the.
For example, now that we have learned advanced C++, I would like to have a look at the source code of native layer, such as the Binder drivers at the bottom or the loading mechanism of classes. I will also send you a link, where all the source code can be found basically: https://pan.baidu.com/s/1tGtBt5Y1G50yI10EkVRPAw
Again, if we are interested in source code, I recommend compiling the source code yourself, so that we can use Android Studio to debug and track the source code, and it works every time.
Ii. The power of predecessors
We all started with Hello World, so there are no shortcuts. It’s just a matter of who takes the most time and who is more willing to mess around. Don’t look at the Internet there are a lot of daniu wrote a lot of analysis of the source code of the article, but in fact they are after repeated toss, to write that a shape like water is very abstract article. So we wrote so many source code analysis articles, recorded so many live classes, we just spent a lot of time behind the scenes. I can do it, you can do it if you want.
Of course, AT the beginning I do not recommend you to look at the source code, I remember the first time I looked at the source code, click in is a mask B. So just at the beginning we need to use the power of predecessors, follow the ideas of Daniel to see the source code, which is usually referred to as the old driver with me.
There are a few points need to remind you that some articles may be long, to have the patience to repeatedly read several times. Of course, sometimes you don’t have to be stuck in one tree, but you can also give other trees a few more tries. Secondly, we find some slightly reliable, read more articles, typesetting a little better, illustrated. Finally, no matter how well written or clear or brilliant someone else’s writing is, it is not our own and there may be bugs. That’s why I recommend reading a little bit more, because if there’s a problem, people will comment on it and it will go through a lot of corrections. We had better go to practice, take notes or write articles, make it really into their own knowledge, so that the speed of improvement is very fast, time and again.
Three. Idiomatic routines
Everyone reading source ideas will be some different, posture will be different, the following I only on behalf of the personal point of view about my customary routine, if we want to analyze glide this open source library, assuming that the information on the Internet now can not meet us:
The first step will be to draw the UML sequence diagram, I believe that we in the analysis of Glide source, at the beginning may not even access the network dock. Not only does it keep us from sinking into the ocean, but it also makes the loading and display process very clear.
The second step will be to draw the UML class diagram, each third party library in its architectural design, there will be something worth learning. Retrofit, an open source library, doesn’t have a lot of class files, but the idea of encapsulation and decoupling can be used in real development.
The third step is to grasp the details, such as how Glide compression fit pictures, how to handle the cache, how to load GIF pictures, how to parse the video cover. Many of the problems we encountered in the development process, source code will give us a lot of better solutions.
The fourth step is to refer to imitation, not to suggest that we repeat the wheel. For example, if we know the realization principle of IOC, we can perfectly solve the situation of multiple Prsenters in MVP. Once we know how RxPermission is implemented, we can easily write things like RxPay and RxShare.
The fifth step is to reflect on the existing architecture design. When we write a project, due to time constraints, we only consider how to achieve it. It is good if we can achieve it and launch it on time. As the business continues to modify the increase, there may have been no high protein low fat. Of course, with the growth of experience, I will consider more, but still need to constantly reflect and learn.
Four. Follow your heart
When we read a lot of source code and third-party open source libraries, we can get through the two channels and learn faster and faster. When colleagues encounter some troublesome bugs, we can analyze and solve them from the perspective of source code; Some difficult requirements, we can quickly find solutions. At this time, we are either working in a big company, or working in a small company as the head, is really like to work, learn and toss.
Original author: Red Orange Darren
The original link: https://www.jianshu.com/p/8012d5d38b01
Welcome to follow my wechat public account “Code farming breakthrough”, share Python, Java, big data, machine learning, artificial intelligence and other technologies, pay attention to code farming technology improvement, career breakthrough, thinking transition, 200,000 + code farming growth charge first stop, accompany you have a dream to grow together.