preface
If you’ve been looking for a job or looking for jobs in the Android industry, you’ll notice that many companies, especially large ones, now list familiarity with framework principles or source code as one of their hiring requirements when looking for a senior engineer or architect. Is it necessary to learn the framework as an Android developer? And how to learn? We’re going to talk about Framework development today.
Why do YOU need to learn the Framework
System application development, now speaking, has begun to separate from the system, out of development, system custom interface, has been provided to the application call, used to enhance the function. Native desktop, dial, setting, has been unable to make differentiation advantage, so all the efforts, to carry out in-depth application development for the previous maintenance system application module for people, tinkering, really no growth. For an Android developer, if there is no deep technical barrier in every day’s work, it is easy to be attacked by others, and finally “optimized” by the company and the market.
It is good, such as Settings, contacts, and then small changes, modify the fault, there is no very high requirements, have done application development, can come and a foot, change the problem. And let an application developer to modify the system interface, estimated change heart tired, and hesitant. So you need a layer of technical barriers to enhance your core competencies. As long as you have core competitiveness and keep learning to expand your depth and breadth, your position will be unshakeable and will not be easily replaced in any market environment. This is why I think Android developers need to learn the framework.
How the Framework learns
Core technologies of the Framework
Next, the blogger will talk about system application development from his own point of view, how to enter the framework, learn, and master more core technologies.
Here some people will say, I do application wind fresh water, but also proficiently, not worse than you do system Framework, why the system application development of worthless. Here’s the thing. I don’t think it’s meant to be. Your app is doing well and making a lot of money. For example, its performance, its interface is gorgeous, the theme is changing. Or it has intelligent recognition, and so on. These are called differentiation product, have its bright spot, characteristic, ability Deus out of encircling.
As for the system application development, I prefer the OEM manufacturers in the development of the whole mobile phone, mainly making the system, which can ensure the normal function without spending a lot of manpower and energy to do the application in-depth development and system reconstruction. Here, we mainly pursue speed and simultaneously meet the hardware requirements of customers, such as multiple Hall devices, multiple temperature sensors, multiple holster functions, etc., but rarely change the application architecture in large quantities, so as not to affect the final mass production.
Above, is specifically refers to this application developer. Then, what does the Framework need to know?
The operating system
Those who are familiar with me know that I particularly like to put this in the first place, for the simple reason that it does support all my subsequent knowledge base, allowing me to take my time and dismantle systems one by one.
Let’s take Android for example, the startup process
Android startup process framework
It’s very similar to Linux, so it’s easy to migrate, and at the same time, a differentiated init process, to do the initial task, to create an incubator in the Android world, and since this is going into the Android world, and the basic language of the Android world is Java, you need a Java virtual machine, so the incubator is going to build a virtual machine, Used to parse the byte stream that executes Java compilation. The incubator itself is implemented by C CPP language, so the Java virtual machine is written by C CPP language, Linux operating system is written by C (and some assembly), so the Java language running the Java virtual machine needs to deal with C CPP, so there is JNI.
Incubators do several things:
-
The Java VM is constructed
-
Bridge JNI to Java and C
-
Load a common shared library
-
Wait for someone to send it a message and create a new process
In the meantime, one thing the incubator does is create system_server. What does this process do? When we create a Virtual machine that can run Java, we need to provide a bunch of system interfaces to assist application development, such as network request, such as interface creation, such as location, memory card acquisition, and so on, so as to facilitate user development functions. A platform is often judged by how powerful it is, how well it has technical documentation, and how well it develops debugging tools.
So there’s a bunch of threads like AMS, WMS, PMS, BT, WIFI, all of which can be found in /proc/{system_server_pid}/ tast.
With these threads, we can easily implement many functions. At this time we need the supporting development tools, such AS AS editor, can edit, compile APK, installed to the mobile phone to run.
About the operating system, you need to know the following:
-
Process, thread concept
-
Mutual exclusion, deadlock mechanism and principle, how to avoid deadlock
-
Memory management mechanism, virtual memory
-
The difference between static and dynamic libraries
-
Memory barriers between processes, how to communicate (IPC)
-
Communication advantages of Binder
I want to keep that in mind, but it’s not about understanding all the mechanics, it’s about understanding the whole thing. Please think about the following questions and see if you can answer them.
If I write an application called com.codegg. Home and inside the main activity, I load a layout, layout_main.xml, and inside the layout I write a TextView, and I want to debug that TextView, Do YOU want to break in com.codegg. Home or system_server? If you want to debug ActivityThread.java? Which process should I break on?
Com.codegg. Home breakpoints com.codegg. Home breakpoints com.codegg. Home breakpoints com.codegg. Home breakpoints com.codegg.
So let’s think about it a little bit more, now I’m going to go after the start of the application, the start Activity, where do I want to break?
We know that the core of the process is in the Activity Manager Server, and it is in the System Server process, as a thread, so we need to debug, we need to set a breakpoint on the System Server.
After we understand system Server, we will take AMS WMS PMS as an example. These service threads complete the application’s request tasks and return the results to the application. For example, to view all the activities that are currently running, the application initiates a request, and from the operating system, it goes to the Server Manager, which has a bunch of handles, or tokens, for the service, and that’s the only way you can find it.
When the Server Manager sees that you have the permission, it will give you the corresponding AMS handle, which the operating system will also recognize, corresponding to the AMS reference of the System Server, that is, you call its method through this handle, the operating system will send your request, To the System Server, the operating system knows that the handle is number 20 of AMS (which represents the method to query all activities currently running), and wakes up the System Server from the binder thread pool, which, like AMS, is a thread. Pull one out of the thread pool and call AMS’s method 20.
After the call is complete, data is delivered to the calling application from the operating system level to realize data transmission. The delivery format defined here is the package, or serialized data.
Understand process communication
Ok, so that’s it for process communication and the simple logic of binder communication. Here’s why we write.
Because the operating system is designed to manage software units as processes, processes themselves do not contact each other, invisible to each other. One process wants to talk to the other, and they both know the operating system. Because they are managed by the operating system. The operating system loads programs from the hard disk into memory and assigns each one a process number, so they are labeled in the system, and each one is given a name, one I love to eat and one I love to drink. Like to drink a person alone, he does not know like to eat, he asked the operating system, there is no one like to eat, the operating system a check, said ah, and then like to eat the process number to him, he can find like to eat.
Then the operating system gave him a car and told him to load it with whatever he wanted to eat. The car is provided by the operating system, and the car is the means of communication. Like cars, planes, walking.
So the way that processes communicate is, you find the target from the operating system, you get the communication mode, and you use the communication tools that the operating system gives you to communicate.
Data structures and algorithms
So with that done, let’s do one more thing:
File = file header + file content
We found that this MP3, OGG formats, belong to a file format statement, this tool we can use a HEX open an MP3 file, you can see the first position, behind the belongs to describe how to parse the contents of, such as file name, file size, file format, system according to the description, try to use the corresponding decoder decoding, Play after decoding.
Here how the decoder decodes is the algorithm. The file header is the data structure that describes the file.
So, we know
Program = data structure + algorithm
If I write a snake, how do I describe the state of the snake, the length of the snake, the current trajectory, all of that has to be represented, and then operate around that description, that’s an algorithm.
The finished program, when run, is the process. So there is a process in the memory, the operating system with a table records process data, such as process, the parent process, process opens the file handle, the process of the current state, the process’s context (context is to save the current CPU registers, save the scene, because the register is only a, when a process is interrupted, the other process is running, So before the current register to save it, to prevent being covered by others, until the next time they run, and then restore back, to ensure that their normal operation), the program is a hard disk or other storage devices, power will not be lost, and the process is memory, so power off is no longer.
How does the program load and how does the system recognize it? This brings us back to where we started, data structure plus algorithm, also known as file header and file content. The source code is compiled and linked into a file we affectionately call an executable. So let’s talk about it.
Two common executable files, called PE for Windows and ELF for Linux, are derived from the COFF format. For more information, see “Linkers and loaders”.
How is the program executed on the CPU?
So the format description, operating system, you will know how to parse it, and then the corresponding code, data, stack area is configured, the code loaded into memory, then the next executive position, namely PC registers, pointing to the executable file configuration text entry, this is the entry point of the program, So this main method that we’re going to write, we can just kind of think of it as a portal, but what’s really going to happen is that there’s some code in front of it, in preparation for running this program, and then it’s ready to actually call main, and this code is called creating the environment for this process, like parameters, stack initialization.
Now that we’re talking about it, let’s look at it from another latitude.
Digital circuit and or non – logical circuit, opened the door to a new world. We use breakpoints, power-on, to represent two states. We can’t say, it’s like there’s electricity, it’s like there’s no electricity, so the world of computers defines binary because right and wrong can be defined, and what’s right and what’s wrong is hard to define.
Therefore, on our circuit board, it is stipulated that 0-0.6V represents no power, 4.4-5V represents power, and the middle value represents device error and instability.
So zero is when there’s no power, one is when there’s power, and the real world is two intervals of voltage.
The CPU in the quartz crystal drive, the execution of a command. What are the instructions? It’s a series of numbers, each representing a specific meaning.
So, the number of instructions a CPU can execute is an important parameter to determine whether it is powerful, and the time it takes to execute instructions in a day, also known as the instruction cycle, is as short as possible. That is, two cpus do an addition at the same time, the one with the shortest time is stronger.
The CPU gets an instruction, will be in the quartz crystal under the impetus, the execution of this instruction, and then the PC register plus 1, read the next instruction.
We often encounter illegal instructions, is because the CPU got an unknown data string, resulting in an exception. For example, its instruction set, there are addition, subtraction, you said to him that you give me a somersault (illegal instructions), he scolded a screw you, Lao Tze will not (abnormal error). In general, the instruction is not recognized. For example, if you use a new ARM instruction and run this program on the old ARM board, it will hang up and prompt illegal instruction.
The beginning of the operating system, embedded operating system, there is no memory protection, can jump to perform data segment is the procedures section, are now in the memory management unit, of course, will be described data segment memory can be read write unenforceable, if the PC (register) points to the place, to read the execution, will be submitted to the illegal access.
If you don’t have protection, and you’re reading data from a data segment, executing it as an instruction, it’s pretty normal to have an instruction exception.
Well, due to space reasons, today I will introduce you so much, to sum up, I hope to let friends realize the importance of learning framework as an Android developer, if you want to learn framework more systematically, please continue to read.
The last
Want a more systematic learning framework friends, bloggers I recently found a Tencent T4 big guy on GitHub organized, has been a star 8K Android Framework development notes, today out to share with you, need partners trouble like + any comments, in my GitHub access!
This note focuses on the main modules of the Framework:
Binder the Binder mechanism as a means of interprocess communication permeates almost all of the Andorid framework layer. The basic communication mechanism of Android Binder must be understood first. Knowledge in this chapter
- Binder series – Introduction
- Binder Driver a preliminary
- Binder Driver revisited
- Binder start ServiceManager
- Get ServiceManager
- Registration Service (addService)
- GetService (getService)
- The Framework layer analysis
- How to use Binder
- How do I use AIDL
- Binder to summarize
- Binder interview questions
There are so many articles on the web analyzing the Handler mechanism, why do we need to write this note? It is not that the articles written by the former people are not good, I just feel that they are not fine, some points are not clear, logic is difficult to flow smoothly, every time I learn something when this situation is sad.
This chapter first macro theoretical analysis and Message source code analysis, and then to MessageQueue source code analysis, Looper source code analysis, handler source code analysis, handler mechanism implementation principle summary. Finally, we also sorted out and handled all the interview questions.
Handler is a long, step-by-step chapter that I’m sure won’t disappoint you if you stick with it.
Chapter 3: Dalvik VM process system
Andorid system startup, init process, Zygote, SystemServer startup process, application creation and use, Activity creation, destruction Handler and Looper.
Chapter 4 analyzes WMS in depth
Window management frame system animation frame View working principle.
The fifth block PackagerManagerService
Package management services. Resource management related classes