Android Source Learning Preliminary

Source: Internet
Author: User

At present, the Internet industry is moving towards the mobile internet in a strong direction, and the development of mobile Internet is inseparable from the support of the mobile platform behind. As we all know, Apple's iOS, Google's Android and Microsoft's Windows Phone system have formed the three pillars situation in the mobile platform market, and the market share of Android system is the highest. The Android system is the first place in the market because it relies on Google's moral and technical prowess, and because it is open, anyone can get its source code and be free to use it. Since the Android system is open, as a mobile platform developer, of course, want to be able to deeply analyze and study its source code, however, the source code of the Android system is very large, we need to learn in a gradual manner.

工欲善其事, its prerequisite. In order to fully and deeply understand the source code of the Android system, we need to have some reference and experimental environment in hand before entering the world of Android system source code, in addition, we need to understand the architecture knowledge of Android system.

Resources

Android system source code is very large and complex, we can not rush into, otherwise it is easy to get into the direction of the inside, and then lose the confidence to study it. We should learn some theoretical knowledge before analyzing its source code, and introduce some information related to Android system.

We know that the Android system is based on the Linux kernel, and when it comes to analyzing its source code at runtime, we often encounter knowledge such as pipes, sockets, and Virtual file systems (VFS). In addition, the Android system has added some special drivers to the Linux kernel, such as the logger driver for the log system, the binder driver for interprocess communication, and the anonymous shared memory Ashmem driver for secondary memory management. When analyzing these Android-specific drivers, the data structures associated with processes and memory management in the Linux kernel are also encountered. Therefore, we need to master some of the basic knowledge of the Linux kernel, the following describes the four classic Linux kernel books.

    1. Linux Kernel Development.

      The author of this book is Robert Love, and the latest version is the 3rd edition. This book provides a general overview of the design and implementation of the Linux kernel, conceptually describing the design goals and implementation ideas of each subsystem of the Linux kernel, which is ideal for beginners to read. From a software engineering point of view, this book is equivalent to the Linux kernel's outline design documentation.

    2. Understanding the Linux Kernel.

      The author of this book is Daniel P. Bovet and Marco Cesati, the latest version is the 3rd edition. This book provides more details on the implementation of the Linux kernel, describes in detail the important data structures, algorithms, and programming techniques used in kernel development, and is ideal for middle-level readers to read. From a software engineering standpoint, this book is the equivalent of a detailed design document for the Linux kernel.

    3. Linux Device Drivers.

      The author of this book is Jonathan Corbet, Alessandro Rubini and Greg Kroah-hartman, the latest version is the 3rd edition. The book is more practical, and it explains in detail how the Linux kernel driver is implemented and how it is implemented, and the reader can follow it to actually write its own Linux driver. After reading this book, it is very helpful for us to follow up and analyze the special driver for Android.

    4. Linux Kernel source code scenario analysis

      The author of this book is Maudeca and Hu Himing, a classic Linux kernel book written by the Chinese themselves. The most important feature of this book is from the use of the situation, the source code of the Linux kernel to make a detailed analysis to help readers to the boring source code to straighten out.

After mastering the basic knowledge of the Linux kernel, it is not appropriate to immediately analyze the source code of the Android system, because this is aimless, we should take the problem or target to analyze the source code of the Android system. The best way to dig out a problem or target is to write your own application on the Android platform. By writing an application, we can know what features the Android platform provides, and we will want to understand how these features are implemented so that we can analyze the source code of the Android system with a problem or goal. Here are some books on two Android app development tutorials:

    1. Professional Android 2 application development.
    2. Google Android SDK Development Example Encyclopedia.

Both of these books use a number of examples to illustrate how to use the Android SDK to develop Android apps. The reader can practice according to the actual situation, the main master of Android application four components (Activity, Service, broadcast receiver and content Provider) usage, Because the entire architecture and implementation of the Android system is designed to provide developers with these four components to implement a wide variety of applications. In the course of learning, if you encounter other problems, you can also refer to the official documents, the site is: http://developer.android.com/index.html

Environment construction

Android applications can be developed in two environments, one in the Android SDK environment, typically integrated in eclipse development, and the other in the Android source code engineering environment, The benefit of developing in this environment is that you can use some interfaces that are not exposed in the SDK. But if we want to modify the source code of the Android system, or add a new feature interface to the Android system, it can only be done in the Android source code engineering environment. Since our aim is to analyze the source code of the Android system, we are also in the Android source code environment when developing Android applications. In this way, we need to build a set of Android source code engineering environment.

Currently, the Android source engineering environment can only be used on Linux platforms, and the distribution of Linux systems is much more, here we recommend Ubuntu System. Ubuntu system is free, and very easy to use, install and update the application is also very convenient, its official: http://www.ubuntu.com/

After installing the Ubuntu system, we can download, compile and install the Android source code, the specific methods and procedures can refer to the following article: download, compile and install Android latest source code on Ubuntu

Android system source code works by default does not contain the Linux kernel source, if we need to modify the Android kernel or add new modules inside, then the Android kernel source code to download, compile and install, Specific methods and procedures can refer to the following article: download, compile and install Android latest kernel source code (Linux Kernel) on Ubuntu

After the Android source code engineering environment is set up, we can develop new applications or modify the system code in it. Add a new application or modify the system's code, do not need to recompile the entire source code project, as long as the separate compilation of the modified module can be, the specific method can refer to the following article: How to compile the module in the Android source code separately

For an already developed application or system function, if you want to show the demo to the customer experience, we can either run on the real machine, or in the emulator (Android source code engineering environment or the Android SDK environment is integrated emulator) above the run. When we do not have a real machine on hand, and we do not want to bring the entire Android source engineering environment or the Android SDK environment to show our demo, we can consider the two environments to separate the simulator, The specific method can be referred to the following article: production of independent distribution of the Android simulator

System architecture

The Android system is designed according to the hierarchy and sub-modules. Before we proceed to the analysis of the source code of the Android system, we need to have a general understanding of the architecture of the Android system, so that we can quickly know which code is at which level of which module, save time to search the code, and more energy into the analysis of the source code.

The whole system is divided into two parts: kernel space and user space. Kernel space includes process management, memory management, device driver modules, and so on, with Android dedicated driver binders, logger, and Ashmem implemented in kernel space. The user space contains the hardware abstraction layer (HAL), the external library, and the Runtime Library layer (External Libraries & Android Runtime), the application framework layer (application Framework) and an application layer (applications) at four levels. What should we do to master this hierarchical structure? The best way is to learn from the hardware abstraction layer of Android as a starting point.

Perhaps readers will find it strange, why should the hardware abstraction layer of Android system as a starting point to learn the architecture of Android system? This level, because of the hardware involved, seems to be a more complex and esoteric point of knowledge. In fact, the hardware abstraction layer of Android system is very clear in implementation and use, it covers the user space and kernel space of Android system from top to bottom. Kernel space is primarily about hardware drivers, and user space involves knowledge of the Android application layer, the application framework layer, and the System Runtime Library layer. Therefore, learning the hardware abstraction layer of the Android system can enable readers to quickly understand the entire Android system, so that the Android system to get a perceptual understanding of the back of the deep analysis of the Android system source code to lay a good foundation.

The learning of the Android hardware abstraction layer can be referenced in a series of articles below:

    • Android hardware Abstraction Layer (HAL) Overview and Learning Plan
    • Write Linux kernel driver for Android system on Ubuntu
    • Test Linux kernel drivers for Android built-in C executable program on Ubuntu
    • Add Hardware abstraction Layer (HAL) module to Android to access Linux kernel drivers on Ubuntu
    • Java Access Hardware Service interface is provided in Ubuntu authoring Jni method for Android hardware abstraction Layer (HAL) module
    • Added hardware access to the Android application frameworks layer on Ubuntu
    • Test application frameworks Layer hardware services for Android built-in Java applications on Ubuntu

Having learned to write basic Android applications and have a general understanding of the overall architecture of the Android system, we can analyze the source code of the Android system.

In the process of analyzing the Android source code, we often go to the application framework layer to analyze its source code, and in the application framework layer, a part of the code is implemented using C + +, this time will often touch the smart pointer, therefore, We also learn the smart pointers in Android as a basic point of knowledge. It is believed that readers who have used the C + + language for development do not feel unfamiliar with smart pointers. In C + + to write code the most error-prone place is the pointer, once the use of improper, light causes memory leaks, heavy caused the system crashes, therefore, the system provides us with smart pointers to avoid the above problems.

In Android, there are three types of smart pointers, lightweight pointers, strong pointers, and weak pointers, all based on the object reference counting technique. The counting technique for lightweight pointers is simple, as long as the object's reference count value is 0, it is freed. The counting technique for strong pointers and weak pointers is relatively complex, and an object can be referenced by both strong pointers and weak pointers, but the object's life cycle is generally controlled by strong pointers, that is, when the object's strong reference count is 0, the object is freed, even if the object's weak reference count is not 0. The introduction of strong pointers and weak pointers this complex reference counting technique is proposed to address garbage collection (garbage Collection) issues. Consider a scenario in which there are two objects A and B in the system, object B is referenced inside object A, and object A is referenced inside object B. When two objects A and B are no longer in use, the garbage collection system will find that the memory occupied by the two objects cannot be reclaimed, because the system can only collect one object at a time, regardless of whether the system decides to retract object A or object B, it will find that the object is referenced by other objects, so it will not be recycled. This creates a memory leak. With strong pointers and weak pointer techniques, the problem is solved by using weak pointers to both A and B to refer to each other. Android Smart pointer Learning, you can refer to the following article: Android system smart pointer (lightweight pointers, strong pointers and weak pointers) Implementation principle analysis

Having mastered the basics of this article, we can formally begin to analyze the source code of the Android system.

Excerpt from: http://www.cnblogs.com/xiaoran1129/archive/2012/11/02/2751446.html

Android Source Learning Preliminary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.