Android Bottom has a certain understanding, studied the relevant Android source code

Source: Internet
Author: User

First, the system architecture:

A), System layering: (from bottom up) ""

1, the Android system is divided into four layers, namely, Linux kernel layer, libraries layer, framework layer, and applications layer;

The Linux kernel layer contains the Linux kernel and various drivers;

The libraries layer contains various class libraries (dynamic libraries (also called shared libraries), Android runtime libraries, Dalvik virtual machines), and the programming language is mainly C or C + +

Most of the framework layer is written in the Java language and is the cornerstone of the Java world on the Android platform

The applications layer is the application layer, which we develop on this layer, using Java speech writing

2. The difference between a Dalvik vm and a traditional JVM:

Traditional JVM: Write a. java file à compile to a. class file à package into a. jar file

Dalvik VM: Write a. java file à compile the. class file into a. dex file à package into an. apk file (via the DX tool)

It improves efficiency by consolidating all classes into one file. More suitable to run on the phone

1. Linux kernel layer [Linux KERNEL]:

Contains the Linux kernel and driver modules (such as USB, Camera, Bluetooth, etc.).

Android2.2 (code Froyo) is based on the Linux kernel version 2.6.

2. LIBRARIES layer [LIBRARIES]:

This layer provides a dynamic library (also called a shared library), an Android runtime library, a Dalvik virtual machine, and so on.

The programming language is mainly C or C + +, so it can be simply regarded as native layer.

3. Framework layer [application framework]:

Most of this layer is written in the Java language, which is the cornerstone of the Java world on the Android platform.

4. Applications layer [application]: Application layer

The whole diagram of the system hierarchy is simplified to the following diagram, corresponding to the following:

Framework Layer--------Àjava World

Libraries Layer--------Ànative World

Linux kernel layer--------àlinux OS

Communication between the Java world and the native world is through the JNI layer

The JNI layer and the native world can directly invoke the system's underlying

II), System compilation:

1. Main steps: Prepare the system environment, download the source code, compile the source code, and output the result:

Currently the system's compilation environment only supports Ubuntu and MAC OS two operating systems, the disk control is large enough

When downloading the source code, because the Android source code uses the GIT to manage, need to download some tools, such as Apt-get install Git-core curl

Download the source code, compile: First build the environment, deploy the JDK (different source code needs to compile different JDK version, such as 2.2 need jdk5,2.3 need 1.6),

Then set up the compilation environment: Use . build/envsetup.sh script; Choose a compilation target (can be different with the version you need)

Finally, the MAKE–J4 command is compiled. (Make is a compiled function that is a command, J4 refers to the number of cores of the CPU processor: J4 x i; dual-core is J8)

Finally, the compiled results are output: All the compilation artifacts are in the/out directory

2. Compiling flowchart

Second, the system start:

Start the first process init of the user space in the Linux system through the Linux kernel, which is the first process to be launched in the Android world;

The init.rc configuration file is then loaded in Init and the daemon (media loading) and incubator Zygote (the opening of the Java world) are turned on, in fact, the daemon of the Debug bridge is also opened up.

The zygote incubator (Zygote is the foundation of the entire Java world, and the entire Android world (including the framework and app APK) is started by the incubator), which is then handled by some action execution: App_main.cpp.

In App_main, the Appruntime start method is called to open appruntime, in fact, the Start method of the parent class Androidruntime is invoked, and zygote is called. At this point the upper right corner of the native layer has an area that is Android runtime started up;

At the same time, Appruntime calls Zygoteinit's Main method to start the Zygoteinit (the entire application and the framework will be brought up by Zygoteinit, and JNI will be started):

In Zygoteinit, the Systemserver class is called, the Init1 () method is started in the main method of Systemserver, the System_init.cpp is opened, and in the Init1 () method, The entire native world (i.e. the libraries layer) is opened up.

The system_init.cpp will then call Systemserver's Init2 () method to open the Serverthread, opening the framework layer through the serverthread (all of them open up), That is, the Java World (Application FRAMEWORK) is started; At this time Activitymanager,windowmanager,packagemanager (the main, All the manifest files and apk have it managed) and so on and so on the framework layer all open up

A) The overall boot sequence of the Android system:

1. Start the init process with the Linux kernel (the first process of user space in a Linux system).

2, the Android runtime this piece of content to start the complete

3, divided into two steps to start libraries (i.e. native world) and application FRAMEWORK (i.e. the Java World)

1) Start libraries first (ie native world)

2) After starting the application FRAMEWORK (i.e. Java World) "Activitymanager,windowmanager, power management, etc."

II) specific start-up process

A), start the process:

1. Init process:--the first process to be launched in Android world

Load a bunch of configuration files, core loaded init.rc profile, which contains the incubator and daemon are all turned on

1), start the service: Open Servermanager

Daemon Start (Daemon process):/system/bin/servicemanager

The Guardian is:

①, the opening of the Java World: Onrestart restart Zygote

②, multimedia loading: onrestart Restart Media

@, ADBD's Guardian has also been opened up, that is, the debugging of the bridge daemon has been opened up

2), start incubator zygote

Starting incubator zygote in App_main, the entire Android world (including the framework and app APK) is started by the incubator

"The virtual machine is not turned on at this time, just configure some VM parameters"

3, app_main:--open incubator

App_main, call Appruntime's Start method, the upper-right corner of the native layer has an area, that is, the Android runtime starts up

The Start method is actually the Start method of the parent class Androidruntime.

"The VM virtual machine is now turned on, enabled by the Start method, and the default memory size of 16M is set in Androidruntime"

"Register JNI and start incubator zygote"

4, Zygoteinit Open

After the Appruntime is started, the Zygoteinit Main method is called, and the Zygoteinit is started;

Then, the whole application and the framework will be brought up by Zygoteinit.

5, Systemserver start:

Zygoteinit calls the Systemserver class, starts the Init1 () method in the main method of Systemserver, and opens the System_init.cpp.

In the Init1 () method, the whole native world is opened up.

6, Serverthread Start (open the framework layer)

Call Systemserver's Init2 () method to open the Serverthread and open the framework layer through the serverthread (all of them open up)

At this time Activitymanager,windowmanager,packagemanager (the most important, all the manifest files and apk have IT management) and so on, and so on the framework layer all open up

ii), Specific introduction:

1. Boot entry: Init process

@, source location :/system/core/init/init.c

@, Process entry: Main method

1) Create folder, Mount Device "created by mkdir command, mount some system devices after"

2) redirect input output, such as error message output "Set some input and output processing"

3) Set the log output "log of some systems"

4) INIT.RC System-initiated configuration file "loaded with relevant information, different versions of the phone's unique configuration information"

①, file location:/system/core/rootdir

②, daemon start (Daemon process):/system/bin/servicemanager

The Guardian is

Opening of the Java World: Onrestart restart Zygote

Loading of multimedia: Onrestart Restart Media

The ADBD daemon was also opened up, i.e. the daemon (adbd[android Debug Bridge Daemon]) of the Debug Bridges (Adb[android) was also opened.

③, start Zygote--app_main.cpp "Zygote is the foundation of the entire Java world"

After compiling, there will be an incubator start-up under System/bin/app_process xzygote

After the daemon is opened, Zygote is also started.

5) parsing and current device-related configuration information (/init.%s.rc)

Tips:

When you finish parsing the init.rc and device configuration information, you get a series of action

Init divides the execution of the action into four stages (priority from large to small):

Early-init: Initial

Init: Initialization phase

Early-boot: Initial stage of system startup

Boot: System boot

6) Processing action execution: This stage zygote will be started

7) Infinite cycle phase, waiting for something to happen

2, Zygote Introduction:

@ , Zygote Start: App_main.cpp

1) zygote Introduction:

①, an application that itself is native

②, loaded by the Init process via init.rc

2) function Analysis:

①, Main method Appruntime.start (), work done by the parent class Androidruntime

②, in androidruntime , opens the following content :

@startVM--Turn on the virtual machine (view heap memory settings): Default 16M ""

@ Register JNI function "At this point in the native layer, the bridge connecting Java and C (i.e. JNI) needs to be built."

@ Start the Main method of "Com.android.internal.os.ZygoteInit"

"System-level package (this package is opened by the runtime's Start method)"

The Start method is actually the androidruntime of its parent class.

@ Enter the Java World entrance

3. Introduction to Servicethread: (What the Java World has done)

1) preloadclasses ();: Pre-loaded class

Read a preloaded-classes configuration file

This file has a lot of content, which is one of the reasons why the Android system is slow to start

At this point there will be a garbage collection operation GC (), which will be useless to recycle

2) Zygoteinit using JNI to open com.android.server.SystemServer in the main method

3) Start System_init.cpp to handle native layer service

4) Then call Systemserver's Init2 ()

5) Start the Servicethread and start the Android service

6) launcher start

Third, the time consumption of the boot:

1, Zygoteinit.main () The class will be preloaded

Catalog: Framework/base/preload-class

Zygoteinit.main () will load a lot of classes, nearly 1800 (Android 2.3)

2. The system will scan all apk during boot.

All applications need to be presented to the user, it is necessary to scan the APK to scan all the packages

There is an APK package in the data directory

A package with APK in the system directory

There are also related packages under the framework directory

3. Those service Systemserver created

Android Bottom has a certain understanding, studied the relevant Android source code

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.