《深入解析Android 虚拟机》——第1章,第1.1节获取Android源码

本节书摘来自异步社区《深入解析Android 虚拟机》一书中的第1章,第1.1节获取Android源码,作者 钟世礼,更多章节内容可以访问云栖社区“异步社区”公众号查看

第1章 获取并编译Android源码
深入解析Android 虚拟机
在本章中,将详细讲解获取并编译Android源码的基本知识,介绍各个目录中主要文件的功能,为读者步入本书后面知识的学习打下基础。

1.1 获取Android源码
要想研究Android系统的源码,需要先获取其源码。目前市面上主流的操作系统有Windows、Linux、Mac OS的操作系统,由于Mac OS源自于Linux系统,因此本书将讲解分别在Windows系统和Linux系统中获取Android源码的知识。

1.1.1 在Linux系统获取Android源码
在Linux系统中,通常使用Ubuntu来下载和编译Android源码。由于Android的源码内容很多,Google采用了Git的版本控制工具,并对不同的模块设置不同的Git服务器,可以用repo自动化脚本来下载Android源码,下面介绍获取Android源码的过程。

(1)下载repo。

在用户目录下创建存放repo的bin文件夹,并把该路径设置到环境变量中去,具体命令如下所示:

$ mkdir ~/bin
$ PATH=~/bin:$PATH

下载用于执行repo的repo的脚本,具体命令如下所示:

$ curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo
 > ~/bin/repo

设置可执行权限,命令如下所示:

$ chmod a+x ~/bin/repo

(2)初始化一个repo的客户端。
在用户目录下创建一个空目录,用于存放Android源码,命令如下所示:

$ mkdir AndroidCode
$ cd AndroidCode

进入到AndroidCode目录,并运行repo下载源码,下载主线分支的代码,主线分支包括最新修改的bug,以及并未正式发布版本的最新源码,命令如下所示:

$ repo init -u https://android.googlesource.com/platform/manifest

下载其他分支,建议下载正式发布的版本,可以通过添加-b参数来下载,例如下载Android 4.3正式版的命令如下所示:

$ repo init -u https://android.googlesource.com/platform/manifest -b
android-4.3_r1

在下载过程中会需要填写Name和E-mail,填写完毕之后,选择Y进行确认。最后提示repo初始化完成,这时可以开始同步Android源码了。同步过程非常漫长,需要大家耐心等待。执行下面命令开始同步代码:

$ repo sync

经过上述步骤后,便开始下载并同步Android源码了,界面效果如图1-1所示。


0e20e1824f332c60c2f46c5b6540fe95d7d1e9c4

1.1.2 在Windows平台获取Android源码
在Windows平台获取源码与在Linux上原理相同,但是需要预先在Windows平台上搭建一个Linux环境,此处需要用到Cygwin工具。Cygwin的作用是构建一套在Windows上的Linux模拟环境,下载Cygwin工具的地址如下所示:

http://cygwin.com/install.html
下载成功后会得到一个名为“setup.exe”的可执行文件,通过此文件可以更新和下载最新的工具版本,具体流程如下所示。

(1)启动Cygwin,如图1-2所示。


3ef5d790bce9689d7aa4a8df2b5f93010701b100

(2)单击“下一步”按钮,选择第一个选项:从网络下载安装,如图1-3所示。


ba28da202e871ad8077915320be575def02943ff

(3)单击“下一步”按钮,选择安装根目录,如图1-4所示。


4e8581f0d9e8d8de57073a2c130051d2d624542d

(4)单击“下一步”按钮,选择临时文件目录,如图1-5所示。


40e7a3d4d7457c3e92a4137f6ef0cd296486f075

(5)单击“下一步”按钮,设置网络代理。如果所在网络需要代理,则在这一步进行设置,如果不用代理,则选择直接下载,如图1-6所示。


d87378c63490220240a810064d28d204a5affb5b

(6)单击“下一步”按钮,选择下载站点。一般选择离得比较近的站点,速度会比较快,如图1-7所示。


69db075f051bbd4f0b79f01aa2f287453b98eb5a

(7)单击“下一步”按钮,开始更新工具列表,如图1-8所示。


3d618d9abae29489a5c61007e5720c98debf2fcd

(8)单击“下一步”按钮,选择需要下载的工具包。在此需要依次下载curl、git、python这些工具,如图1-9所示。


4e4d4088c246405519f1e4cf3edefcb081e59e36

为了确保能够安装上述工具,一定要用鼠标双击这些图标使之变为Install形式,如图1-10所示。


10e2ffed91e94b2536b706a72e2b6de25588c608

(9)单击“下一步”按钮,需要经过漫长的等待过程,如图1-11所示。


40c6485a6cef77eb6ee841cfdcfade9790c9a11b

如果下载安装成功会出现提示信息,单击“完成”按钮即完成安装。打开安装好的Cygwin后,会模拟出一个Linux的工作环境,然后按照Linux平台的源码下载方法就可以下载Android源码了。

建议读者在下载Android源码时,严格按照官方提供的步骤进行,地址是:http://source. android.com/source/downloading.html,这一点对初学者来说尤为重要。另外,整个下载过程比较漫长,需要大家耐心等待。图1-12是笔者机器的命令截图。


a6840881e353360d056731efabd22ad9f35ac4fa

1.1.3 Windows获取Android L源码
在作者撰写本书时,Android系统的最新版本是Android L,此版本在Google官方网站的代号为“l-preview”。在Windows系统中获取Android L源码的具体流程如下。

(1)下载Git工具,其官方下载地址是http://www.git-scm.com/downloads ,如图1-13所示。


f5025264f8b0df9d18bedd1a5f4355d565dc6d5f

下载后双击可执行文件进行安装,在安装过程按照默认选项安装即可。

(2)下载并安装TortoiseGit工具,下载地址是https://code.google.com/p/tortoisegit/ ,如图1-14所示。


51450506a94f6b117cc38ab9c591a3b5279c8664

下载后双击下载后的可执行文件进行安装,安装过程按照默认选项安装即可。如果读者对英文不敢兴趣,可以下载TortoiseGit的中文版本,笔者安装的就是TortoiseGit中文版。

(3)新建一个保存源码的文件夹,例如“cc”,在文件夹上单击右键,然后选择“Git克隆”命令。

(4)在弹出的“Git克隆”对话框界面中,在“URL”后面的文本框中输入Android L项目下载路径:https://android.googlesource.com/platform/ manifest.git,如图1-15所示。


795a6816f701d0c9b16e3c1fd03ed740011cac20

(5)单击图1-15中的“确定”按钮开始下载分支信息文件,下载后的文件被保存在“cc”文件夹中,具体目录结构如图1-16所示。

!


d8be0ebf4269ea7917bf247a5178c2a4cfd84c50

(6)打开文件default.xml,在里面列出了Android L源码的各个文件夹中子文件夹目录的分支信息,具体格式如下所示:

<project path="abi/cpp" name="platform/abi/cpp" groups="pdk"/><project path="art" name="platform/ art"/>
<project path="bionic" name="platform/bionic" groups="pdk"/>
<project path="bootable/bootloader/legacy" name="platform/bootable/bootloader/legacy"/>
<project path="bootable/diskinstaller" name="platform/bootable/diskinstaller"/>
<project path="bootable/recovery" name="platform/bootable/recovery" groups="pdk"/>
<project path="cts" name="platform/cts" groups="cts"/>
<project path="dalvik" name="platform/dalvik"/>
<project path="developers/build" name="platform/developers/build"/>
<project path="developers/demos" name="platform/developers/demos"/>
<project path="developers/docs" name="platform/developers/docs"/>
<project path="developers/samples/android" name="platform/developers/samples/android"/>

例如“”表示在Android L的源码中,存在了一个名为“bootable”的根目录文件夹,而在“bootable”文件夹中又包含了一个名为bootloader”的子文件夹,而在“bootloader”文件夹下又包含了一个名为“legacy”的文件夹。

(7)开始下载Android L源码,在文件夹“cc”上单击右键,然后选择“Git克隆”命令。在弹出界面中输入Android L某个分支的下载路径,例如path=“art”表示“art”,此文件夹的下载地址是:https://android.googlesource.com/ a/art.git。然后勾选“分支”复选框,并在后面填写“l-prevew”分支,如图1-17所示。


28f31785629e602108ff963a0a4a6e2c75143f54

单击“确定”按钮后将开始下载Android L源码中的“art”文件夹的内容。同理,可以根据default.xml文件提供的路径信息继续下载其他文件夹的内容。

Android虚拟机Dalvik完整源码,宝贵资源,欢迎下载! This directory contains the Dalvik virtual machine and core class library, as well as related tools, libraries, and tests. A note about the licenses and header comments --------------------------------------------- Much of the code under this directory originally came from the Apache Harmony project, and as such contains the standard Apache header comment. Some of the code was written originally for the Android project, and as such contains the standard Android header comment. Some files contain code from both projects. In these cases, the header comment is a combination of the other two, and the portions of the code from Harmony are identified as indicated in the comment. Here is the combined header comment: /* * Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ---------- * * Portions of the code surrounded by "// BEGIN Harmony code" and * "// END Harmony code" are copyrighted and licensed separately, as * follows: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Native SH call bridge --------------------- Native SH call bridge is written by Shin-ichiro KAWASAKI and Contributed to Android by Hitachi, Ltd. and Renesas Solutions Corp.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值