This article has authorized the official wechat account Guolin_blog (Guo Lin) to publish my own Xiaonan, an aspiring Android developer.

Personal official account:

### Android Source Code Analysis Tour directory

The full contents of the Android Source Analysis Tour will be arranged as follows:

  1. Chapter 1 – This article introduces some basic concepts, methods, and tools.
  2. Chapter 2 — focuses on the Binder IPC mechanism. Because the Android Framework is full of processes that communicate between processes, learn in advance here.
  3. Chapter 3 — Introduces some common Android source code analysis.
  4. Chapter 4 – focuses on the HAL layer.
  5. Follow up slowly…

### Why learn Android source code?

Many readers, especially beginners, are particularly resistant to looking at source code, so here’s why source code analysis is necessary. These include the following benefits:

  1. Learning Android source code helps us learn the design patterns, ideas, architecture.
  2. Familiar with the architecture of the whole source code, help us to call the SDK provided by Android more correctly, write efficient and correct code.
  3. Learning source code helps us in interviews, because big companies like to ask about it.
  4. Learning source code helps us learn some black technology, such as plug-in learning from time we need to learn Hook mechanism, but learning Hook mechanism when we need to master the Activity start process, message mechanism and so on.

Personally, I think it is the difference between a coder and a senior engineer to only know how to call the API and master the implementation of the underlying API. Will only use the API can only do a lot of repetitive work every day, but learning the source code, we can do a lot of native API could not do, this is what we call black science and technology, it can let our knowledge more widely, because, even if a personal talent again good, if he didn’t have enough knowledge noodles wide, A lot of things (such as hot updates, plug-ins, NDK) that are not touched will always be stuck at the bottleneck they have reached. Source code may be important for doing system APP, system Framework layer development and customization like I do, but that doesn’t mean it’s not important when doing third-party APP. Learning source need to have patience, of course, may also need you to in the process of analysis to draw some more intuitive (image), and spend extra time studying the source code of design patterns, and so on, to study the source code is a pain, because you will find that the master source doesn’t mean you can expect success. But as you slowly master the entire Android architecture, you won’t regret your efforts. Because I always believe that you will get what you give.

As an aside, I just mentioned NDK. I think NDK is also an important module. It can use C/C++ to realize things that Java cannot or are inefficient when Java is used, such as QQ voice change function, audio mixing of national karaoke, video processing, live broadcasting and so on. So I’ll write about the NDK when I have time.

# # # introduction

Recently, I have been maintaining the system camera APP and intend to study and supplement the relevant knowledge of HAL layer. Taking this opportunity, I plan to reconstruct the collection of Android Source Analysis Tour, and then add some knowledge of overall system architecture, UML (class diagrams, sequence diagrams), and other aspects of source analysis.

###Android system architecture

When it comes to Android architecture, many people’s first reaction is probably this most classic picture:

However, the image above is out of date because of the HAL layer added to the Android architecture, which can be seen at source.android.com:

According to the figure above, the Android architecture is as follows from top to bottom:

  1. Application frameworks: Application frameworks are most commonly used by application developers. As hardware developers, we should know a lot about developer apis, because many of these apis map directly to the underlying HAL interface and provide useful information about implementing drivers.
  2. Binder IPC: The Binder Interprocess Communication (IPC) mechanism allows application frameworks to cross process boundaries and call Android system service code, enabling high-level framework apis to interact with Android system services. At the application framework level, developers can’t see this kind of communication going on, but everything seems to be “working as planned.”
  3. System services: Functionality provided by application framework apis communicates with system services to access the underlying hardware. Services are centralized modular components, such as window managers, search services, or notification managers. Android contains two sets of services: “Systems” (services such as window managers and notification managers) and “media” (services related to playing and recording media).
  4. Hardware Abstraction Layer (HAL) : The hardware abstraction layer (HAL) defines a standard interface for hardware vendors to implement and allows Android to ignore lower-level driver implementations. HAL allowed us to implement functionality without affecting or changing higher-level systems. HAL implementations are packaged into module (so) files and loaded in due course by the Android system.
  5. Linux kernel: Developing device drivers is similar to developing a typical Linux device driver. The Version of the Linux kernel used by Android contains special complementary features such as wakelock (a memory management system that protects memory more aggressively), Binder IPC drivers, and other features important for mobile embedded platforms. These additional features are mainly used to enhance system functionality and do not affect driver development. We can use any version of the kernel as long as it supports the required functionality (such as Binder drivers). However, the latest version of the Android kernel is recommended.

Common modules of HAL layer are:

# # # # # # details

  1. Android Framework: Contains all layers above HAL, which refers to the Framework of the entire system. Corresponds to the Framework folder in AOSP.
  2. Application Framework: More apI-related, application-side frameworks. Such as activities, ActivityThread, and so on, but not AMS.
  3. The Android Framework includes Java implementation and C/C++ (native) implementation. Later in the analysis of Camera source code will be reflected.

### source code download and view, tool introduction

Work to be prepared:

  1. On Ubuntu or Mac, you are advised to use Android Studio.
  2. In Windows, you are advised to use Source Insight.
  3. UML modeling tool: Start UML is recommended for drawing class diagrams and sequence diagrams.
  4. Download the source code, recommend the following article in the use of network disk download.

References are as follows:

Download and import the source code to view the reference article:

www.cnblogs.com/Napoleon-Wa…

About using Source Insight:

Blog.csdn.net/shulianghan…

About UML Modeling:

www.jianshu.com/p/a0704aa2b…

### source code analysis methods and experience

For an introduction to the source architecture, see the following article:

Blog.csdn.net/itachi85/ar…

From top to bottom, Android source code is very large and complex. Here are some lessons from source code analysis:

  1. First combine the official website and other materials to make an overall grasp of the Android system, and then carry out specific learning, and then continue the iterative learning process.
  2. Use efficient tools such as Source Insight/Android Studio to view the Source code, track the code, and Debug it when necessary.
  3. Combined with UML modeling, draw class diagram/sequence diagram to avoid confusion.
  4. Recommend “hulunganzao” type of learning, first rough learning, and then in-depth details. Hulking things down here is not to say that they are not well understood. Hulking things down here is to know how to “swallow a few” and replenish them in the future when you need them. The combination of jump learning and progressive learning.
  5. Start with the top layer and then the bottom layer, starting with the commonly used system level API or plug-in level API.

If you feel that my words are helpful to you, welcome to pay attention to my public number:

My group welcomes everyone to come in and discuss all kinds of technical and non-technical topics. If you are interested, please add my wechat huannan88 and I will take you into our group.