[deep understanding of the full text of Android Volume-chapter i] preparation before reading

Source: Internet
Author: User
<span id="Label3"></p><p style="text-align: left;"><p style="text-align: left;"><span style="font-size:18px;color:#ff6666;"><strong>As the "in-depth understanding of Android Volume one" and "deep understanding of Android volume two" is no longer published, and the dissemination of knowledge should not be due to the problem of paper media, so I will be in the CSDN blog full text forward the full contents of the two books.</strong></span></p></p><p style="text-align: left;"><p style="text-align: left;"><span style="font-size:18px;color:#ff6666;"><strong>(the Publishing House of a good PDF version is being applied to the publishing house, it will be published through CSDN download resources)</strong></span></p></p><p style="text-align: left;"><p style="text-align: left;"><br></p></p>Chapter I preparatory work before reading<p><p>Main contents of this chapter</p></p><p><p></p></p> <ul> <ul> <li>This chapter provides a brief introduction to the architecture of the Android system, the build environment, and the use of some tools.</li> </ul> </ul><p><p></p></p>1.1 System Architecture 1.1.1 Android System architecture<p><p>Android is Google's launch of a mobile phone development platform. The platform itself is based on the Linux kernel, and figure 1-1 shows the architecture of the System:</p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p style="text-align: center;"><p style="text-align: center;"><br></p></p><p style="text-align: center;"><p style="text-align: center;">Figure 1-1 Android System architecture</p></p><p><p>As you can see, the Android system can be broadly divided into four layers, from bottom to top:</p></p><p><p></p></p> <ul> <ul> <li>Linux kernel layer, currently Android2.2 (codenamed Froyo) is based on the Linux kernel version 2.6.</li> <li>Libraries layer, which provides a dynamic library (also called a shared library), an Android runtime library, a Dalvik virtual machine, and so On. From a programming language, most of this layer is written in C or C + +, so you can simply think of it as a native layer.</li> <li>Above the libraries layer is the framework layer, most of which is written in the Java Language. It is the cornerstone of the Java world on the Android Platform.</li> <li>The framework layer above is the applications layer, and the user directly interacts with these applications, they are all developed in Java.</li> </ul> </ul><p><p></p></p><p><p>From the above introduction, it can be seen that one of the biggest features of android, I am afraid is to build a vast Java developers clamor Java WORLD. But the world is not a castle in the sun, its operation depends on another Google hard to hide the native world. The interaction between the two worlds can be expressed in figure 1-2:</p></p><p style="text-align: center;"><p style="text-align: center;"><br></p></p><p><p></p></p><p style="text-align: center;"><p style="text-align: center;">Figure 1-2 Java World and native world interaction</p></p><p><p>From the Following:</p></p><p><p></p></p> <ul> <ul> <li>Java has platform-independent features, but the isolation between Java and specific platforms is done by the JNI Layer. Java is done by invoking system calls in the Linux OS through the JNI layer. For example, Create a file, create a socket, and so On.</li> <li>In addition to the Java world, there is a core native world, which provides strong support for the efficient and smooth operation of the entire System. Generally speaking, the Java world interacts with the native world through the JNI layer through the IPC. The most mysterious IPC method on the Android platform is Binder. Binder is described in detail in the sixth Chapter. In addition, sockets are commonly used in IPC Mode. These are all seen in the code analysis that Follows.</li> </ul> </ul><p><p></p></p>1.1.2 The structure of the book<p><p>The modules analyzed in this book will also follow the Android system architecture, as shown in 1-3:</p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p style="text-align: center;"><p style="text-align: center;">Figure 1-3 Architecture diagram of the book</p></p><p><p>From the know, the various modules analyzed in this book are not involved in the kernel, the other three layers have been involved, respectively, are:</p></p><p><p></p></p> <ul> <ul> <li>The native layer includes init, audio systems (including audiotrack, audioflinger, and audiopolicyservice), surface systems (including surface and surfaceflinger), Common classes (including refbase, sp, wp, etc.), vold, and Rild.</li> <li>The Java framework layer includes zygote, system_server, and common classes in Java (including handler, looper, and so on).</li> <li>Java application layer, including Mediaprovider and PHONE.</li> </ul> </ul><p><p></p></p><p><p></p></p>1.2 Building the development environment<p><p>This section describes how to build an Android source development Environment.</p></p><p><p>first, a Linux system is required, and I recommend installing Ubuntu10.04 (32-bit version). Readers can download this version of the system from the Internet. Windows users can use VMware or VirtualBox as a virtual machine to install Ubuntu10.04. I personally recommend VMware because it's too powerful!</p></p><p><p>If you are using vmware, be sure to install VMware Tools after you install Ubuntu because this tool provides many useful features. There is also a small suggestion that if the Linux system is for personal use, it is recommended to log into the system with the root Account. At work, many colleagues who have logged in with a Non-root account have been working with sudo all day, typing in a password, which wastes a lot of fragmented time slices.</p></p><p><p>Assuming that the reader has installed Ubuntu 10.04 (32-bit Version) and logged on to the system as the root account, the following work is Done:</p></p>1.2.1 Download Source code<p><p>Android Source uses Git① as the version management tool, This tool by the Linux Father Linustorvalds uses the pure C Development. The question of why git uses the C language has also sparked a big discussion about the pros and cons of C and C + +, but Linus Torvalds apparently did not have the image of "the height of the temple, the people who are worried about it". For ordinary yards, The most important thing is to use the most suitable tools and the most practical way to do good Work. So c, C + +, Java, python, and so on are just tools.</p></p><p><p>Here's How to download the source Code.</p></p>1. Set up the software source<p><p>Before downloading the android source code, some download tools need to be downloaded from Ubuntu software source. You can specify a software source for the Ubuntu System. Some software sources have these tools, some do not, and each software source download speed is also different, so you should first find a suitable source of software. Ubuntu software Source Setup Interface 1-3 shows:</p></p><p><p></p></p><p style="text-align: center;"><p style="text-align: center;"><br></p></p><p style="text-align: center;"><p style="text-align: center;">Figure 1-3 Ubuntu software source settings</p></p><p><p>You can find that the source address of the software is set to Http://mirror.bjtu.edu.cn/ubuntu. Everyone can choose the right software source according to their own Situation.</p></p>2. Download Android source code<p><p>Download the following Android source code, the process is relatively simple, can be one go.</p></p><p><p></p></p> <ul> <ul> <li>Apt-get Install Git-core Curl #先下载这两个工具</li> <li>Mkdir–p ~/android/froyo #在登录用户的目录下新建android和froyo两个目录</li> <li>CD ~/android/froyo #进入这个目录</li> <li>Curl Http://Android.git.kernel.org/repo >./repo #从源码网站下载repo脚本, This script is provided by Google for easy source Download. The script can download the entire source Code.</li> <li>chmod a+x Repo #设置该脚本为可执行</li> <li>./repo init-u Git://android.git.kernel.org/platform/manifest.git–bfroyo #初始化git库</li> <li>The./repo sync #下载源码 with a size of 2 gigabytes and a fast speed estimate of 2 Hours.</li> </ul> </ul><p><p></p></p><p><p>When you are finished downloading, the contents of this directory are shown in 1-4:</p></p><p><p></p></p><p style="text-align: center;"><p style="text-align: center;"><br></p></p><p style="text-align: center;"><p style="text-align: center;">Figure 1-4 Source Download results</p></p><p><p>Note that the kernel code must be downloaded separately and downloaded as Follows:</p></p><p><p>git clone Git://android.git.kernel.org/kernel/common.gitkernel</p></p>1.2.2 Compiled source code 1. Deploy JDK<p><p>Froyo compiler relies on JDK1.5, so the first thing to do is to download JDK1.5. The download URL is http://www.oracle.com/technetwork/java/javase/downloads/index-jdk5-jsp-142662.html. The downloaded files are jdk-1_5_0_22-linux-i586.bin. Put it in a directory, such as myself, put it in the/develop, and then execute it in this directory:</p></p><p><p>./jdk-1_5_0_22-linux-i586.bin #执行这个文件</p></p><p><p>This command is actually decompression, the result of decompression in the/develop/jdk1.5.0_22 directory. Now that you have the jdk, you can deploy it by following these steps:</p></p><p><p></p></p> <ul> <ul> <li>Add the following words at the end of the ~/.BASHRC file:</li> </ul> </ul><p><p></p></p><p><p>Exportjava_home=/develop/jdk1.5.0_22 #设置为刚才解压的目录</p></p><p><p>Exportjre_home=java_home/jre</p></p><p><p>Exportclasspath= $JAVA _home/lib: $JRE _home/lib: $CLASSPATH</p></p><p><p>Exportpath= $JAVA _home/bin: $JRE _home/bin: $PATH</p></p><p><p></p></p> <ul> <ul> <li>Re-login to the system so that the JDK resources can be found correctly.</li> </ul> </ul><p><p></p></p>2. Compiling the source code<p><p>Android's compilation has its own set of rules, the main use is the Mk File. There are too many explanations on the internet, which is not mentioned here, only a simple introduction of its compilation Process:</p></p><p><p>Enter the source directory (take my development environment as an example), that is Cd/develop/download_froyo</p></p><p><p></p></p> <ul> <ul> <li>Perform. build/envsetup.sh, This script is used to set the Android build Environment.</li> <li>Executes the Choosecombo command, which is used to select the compilation target (such as the target hardware platform, Eng or user, etc.). In general, mobile phone manufacturers will set their own unique compilation Options.</li> </ul> </ul><p><p></p></p><p><p>After performing the above steps, you can compile the System. The Android platform provides three commands for compiling, namely make, MMM and mm, The three commands are used and their pros and cons are as Follows:</p></p><p><p></p></p> <ul> <ul> <li>Make: without any parameters, it is used to compile the entire system for a long time, I do not recommend this practice, unless the reader wants to compile the entire system.</li> <li>Make mediaprovider: for example, The following examples are compiled Mediaprovider. This approach corresponds to a single module Compilation. The advantage is that the other modules that the module relies on are also compiled Together. Make libmedia, for example, will compile all libmedia-dependent libraries. Its disadvantage is also obvious, it needs to search the entire source code to define the Android.mk file used by the Mediaprovider module, and also to determine whether the module depends on the other modules are Modified. The overall compilation time is Longer.</li> <li>Mmm packages/providers/mediaprovider: This command compiles the target module under the specified directory without compiling the module on which it DEPENDS. So if the reader is the first time compiling, compiling a module in this way will often result in an Error. The reason for the error is because the module it relies on is not compiled.</li> <li>Mm: This method requires cdpackages/providers/mediaprovider the directory first, then Mm. This command compiles the modules in the current directory. Like mmm, it compiles only the target Module. mm and MMM commands are compiled quickly.</li> </ul> </ul><p><p></p></p><p><p>From the point of view of use, I have the following suggestions:</p></p><p><p></p></p> <ul> <ul> <li>If you only know the target module name, you should compile the target module by using the Make module Name. For example, If you compile libmedia, you can use make Libmedia directly. In addition, This method is also used for initial Compilation.</li> <li>If you do not know the target module name and know the directory where the target module is located, you can use the MMM or mm command to Compile. of course, The make command must also be used for initial Compilation. and later compilation can use MMM or mm, this will save a lot of Time.</li> </ul> </ul><p><p></p></p><p><p>The general compilation method uses incremental compilation, which is to compile only the changed target Files. But sometimes you need to recompile all of the target files, so you can use the-b option of the Make Command. For example, make–b module name, or mm–b, mmm–b. MM and MMM internal, also call make command, and the-b option will force all target files to be Compiled.</p></p><p><p>Android's compilation process is relatively simple, the difficulty is mainly in the Android.mk file Writing. Readers can search the Internet for relevant learning Materials.</p></p>3. The compilation target of each module in this book<p><p>The compilation target for each module in this book is shown in table 1-1, where only a few representative modules are listed:</p></p><p style="text-align: center;"><p style="text-align: center;">Table 1-1 This book each module compiles the target</p></p> <table border="0" cellspacing="0" cellpadding="0" width="709"> <tbody> <tr> <td valign="top"><p>Target Module</p></td> <td valign="top"><p>Make command</p></td> <td valign="top"><p>MMM command</p></td> </tr> <tr> <td valign="top"><p>Init</p></td> <td valign="top"><p>Make Init</p></td> <td valign="top"><p>Mmm system/core/init</p></td> </tr> <tr> <td valign="top"><p>Zygote</p></td> <td valign="top"><p>Make App_process</p></td> <td valign="top"><p>Mmm frameworks/base/cmds/app_process</p></td> </tr> <tr> <td valign="top"><p>System_server</p></td> <td valign="top"><p>Make services</p></td> <td valign="top"><p>Mmm Frameworks/base/services/java</p></td> </tr> <tr> <td valign="top"><p>refbase, etc.</p></td> <td valign="top"><p>Make Libutils</p></td> <td valign="top"><p>Mmm frameworks/base/libs/utils</p></td> </tr> <tr> <td valign="top"><p>looper, etc.</p></td> <td valign="top"><p>Make framework</p></td> <td valign="top"><p>Mmm frameworks/base</p></td> </tr> <tr> <td valign="top"><p>Audiotrack</p></td> <td valign="top"><p>Make Libmedia</p></td> <td valign="top"><p>Mmm Frameworks/base/media/libmedia</p></td> </tr> <tr> <td valign="top"><p>Audioflinger</p></td> <td valign="top"><p>Make Libaudioflinger</p></td> <td valign="top"><p>Mmm Frameworks/base/libs/audioflinger</p></td> </tr> <tr> <td valign="top"><p>Audiopolicyservice</p></td> <td valign="top"><p>Make Libaudiopolicy</p></td> <td valign="top"><p>MMM hardware/msm7k/libaudio-qsd8k (example)</p></td> </tr> <tr> <td valign="top"><p>Surfaceflinger</p></td> <td valign="top"><p>Make Libsurfaceflinger</p></td> <td valign="top"><p>Mmm Frameworks/base/libs/surfaceflinger</p></td> </tr> <tr> <td valign="top"><p>Vold</p></td> <td valign="top"><p>Make Vold</p></td> <td valign="top"><p>Mmm system/vold/</p></td> </tr> <tr> <td valign="top"><p>Rild</p></td> <td valign="top"><p>Make Rild</p></td> <td valign="top"><p>Mmm hardware/ril/rild/</p></td> </tr> <tr> <td valign="top"><p>Mediaprovider</p></td> <td valign="top"><p>Make Mediaprovider</p></td> <td valign="top"><p>Mmm Packages/providers/mediaprovider</p></td> </tr> <tr> <td valign="top"><p>Phone</p></td> <td valign="top"><p>Make Phone</p></td> <td valign="top"><p>Mmm packages/apps/phone/</p></td> </tr> </tbody> </table><p><p>Assuming the make framework, the compiled results are shown in 1-5:</p></p><p><p></p></p><p style="text-align: center;"><p style="text-align: center;"><br></p></p><p style="text-align: center;"><p style="text-align: center;">Figure 1-5 Results of the make framework</p></p><p><p>As you can see, the make command compiles framework-res.apk and Framework.jar two Modules. The results of their compilation are under Out/target/product/generic/system/framework. The reader uses the ADB command to push the two files to the System/framework directory of the phone to replace the old Files. If you want to test this new module, you will need to kill all the processes that use the module, and the process will reload the module when it restarts, so you can use the new File. For example, to test the newly modified Libaudioflinger module, the ADB push goes up, killing the mediaserver process first, because the Libaudioflinger library is currently only used by that Process. When the MediaServer is restarted, the Libaudioflinger library on the new push is Loaded.</p></p><p><p>After the system service is killed, it will usually be restarted automatically (controlled by init, as seen in chapter iii).</p></p>1.3 Tools Introduction<p><p>This section describes two more useful tools for Android development and source code Research.</p></p>1.3.1 Source Insight Introduction<p><p>Source insight is an essential tool for reading the source code and is a software under windows that can be installed on the Linux platform via Wine. here, do not tell how to install the source insight, I believe the reader Will. Here are some tips on using the source Insight.</p></p>1. Source Insight Work Burden<p><p>When using source insight, you need to create a new source project, project→new project from the menu item to specify the source Directory. Found at work, many colleagues often a brain to the Android all the source code is added to the project, resulting in the source insight run very slow. In fact, you only need to add the current analysis of the source directory to the Project. For example, after creating a new source Insight project, only the Source/framework/base directory is Added. In addition, when the source code in a directory is finished, you can remove the catalog from the project without further analysis by Project→add and remove project files Options. 1-6 is shown below:</p></p><p><p></p></p><p style="text-align: center;"><p style="text-align: center;"><br></p></p><p style="text-align: center;"><p style="text-align: center;">Figure 1-6 Adding or removing catalogs in a project</p></p><p><p>The box we can find:</p></p><p><p></p></p> <ul> <ul> <li>Source Insight supports dynamically adding or removing Directories. In this way, the workload of the source insight can be greatly reduced.</li> </ul> </ul><p><p></p></p><p><p>Generally first put Framework/base under the catalogue to the project, later if necessary, and then add the other directories in.</p></p>2. Adjust the font<p><p>Source Insight The default font is small, looking very cost-eye. What to do?</p></p><p><p>Select the Options→document Options menu on the toolbar, pop up the Document Options dialog box, where the Upper-left section has a screen Fonts, and a font dialog box pops up, where you can select large fonts, such as number fourth, font fifth, and so On. 1-7 is shown below:</p></p><p><p></p></p><p style="text-align: center;"><p style="text-align: center;"><br></p></p><p style="text-align: center;"><p style="text-align: center;">Figure 1-7 Font Adjustment</p></p>3. Quickly locate files<p><p>After the project is established, the source code must be parsed by Project→rebuild project Options. In addition, in the study of source code often usually only remember the source file name, and do not remember which directory. It doesn't matter, source insight supports quickly locating files in the source Code. Use method 1-8 as Shown:</p></p><p><p></p></p><p style="text-align: center;"><p style="text-align: center;"><br></p></p><p style="text-align: center;"><p style="text-align: center;">Figure 1-8 Quick Location files</p></p><p><p>Use this method:</p></p><p><p></p></p> <ul> <ul> <li>First select the button in the lower left corner of Figure 1-8.</li> <li>Then enter the source file name in the upper left-hand corner of the input box, for example App_process. The corresponding file is then listed in the result bar.</li> </ul> </ul><p><p></p></p><p><p></p></p>Use of 1.3.3 BusyBox<p><p>Busybox, known as the "swiss Army knife" on the Linux platform, offers a number of common tools, such as grep, find, and More. These tools are available on standard linux, but the Android system has removed most of the Tools. This has led to the difficulty of debugging the program and studying the Android system, so we need to install BusyBox on the PHONE.</p></p>1. Download BusyBox<p><p>We can download the compiled busybox,1-9 from this web site as shown here:</p></p><p><p>http://www.busybox.net/downloads/binaries/1.18.4/</p></p><p><p></p></p><p style="text-align: center;"><p style="text-align: center;"><br></p></p><p style="text-align: center;"><p style="text-align: center;">Figure 1-9 BusyBox Download</p></p><p><p>Note that the site has been compiled according to the different platform of the corresponding busybox, we can download the corresponding files according to the situation of their mobile phone. For example, HTC G7 's CPU supports armv7l, so I downloaded the closest busybox-armv6l.</p></p><p><p>Tip: Arm V7 represents the arm instruction set as v7, which is currently supported by the ARM CORTEX-A8/A9 series of Cpus.</p></p>2. Installation and use of BusyBox<p><p>After downloading busybox, you need to push it to your PHONE. Such as:</p></p><p><p>ADB push Busybox/system/xbin #为了避免冲突, I push to The/system/xbin directory.</p></p><p><p>Cd/system/xbin #进入对应目录</p></p><p><p>chmod 755 BusyBox #更改busybox权限为可执行</p></p><p><p>Busybox–-install #安装busybox</p></p><p><p>grep #执行busybox提供的grep命令, or busybox xxx execute XXX command also line</p></p><p><p>BusyBox is installed, if the BusyBox command is executed, 1-10 of the output will be Printed.</p></p><p><p></p></p><p style="text-align: center;"><p style="text-align: center;"><br></p></p><p style="text-align: center;"><p style="text-align: center;">Figure 1-10 Tools provided by BusyBox</p></p><p><p>As you can see, BusyBox offers a number of tools so that we can learn more about Android Systems.</p></p><p><p>To the mobile phone installation busybox must have root authority, in order to learn android, we'd better buy that can be cracked mobile phone bar.</p></p>1.4 Summary of this chapter<p><p>This chapter on the Android system, source building, research tools, and so do a part of the introduction, I believe the reader is now impatient, eager to do it? Start our source code journey now!</p></p><br clear="all">

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.