您的位置:首页 > 移动开发 > Android开发

Ubuntu下android-4.0.3_r1源码下载,阅读工具安装配置,源码编译详解

2013-05-29 17:02 1226 查看
备注:

android源码动辄6、7G,新版本的则10G左右,所以要有足够大的硬盘空间。

android应用开发环境搭建:http://www.cnblogs.com/pharen/archive/2011/09/13/2174592.html

一、安装Ubuntu 12.04

网上已经有很多图文教程,这里就不细说里,进给出链接:

http://teliute.org/linux/Ubsetup/lesson21/lesson21.html

http://www.dedecms.com/knowledge/servers/linux-bsd/2012/0819/8387.html

http://oss.lzu.edu.cn/artical.php?id=5

二、下载Anroid源码:

方法一:直接下载

下载链接:http://garth.im/136.htmlhttp://d.zhu.im/ (建议用迅雷下载)

选择需要的版本下载,比如 latest-android-4.0.3_r1.tar.bz2,将其解压到某个路径下。

tar xvIf latest-android-4.0.3_r1.tar.bz2

或 bzip2 -dc latest-android-4.0.3_r1.tar.bz2 | xvf -

附:*.tar.bz2等文件如何解压

如果tar不支持j这个参数就先用

bzip2 -d xxx.tar.bz2

把它解压成.tar文件,然后再用

tar xvf xxx.tar

解压常用命令:http://www.douban.com/note/57861194/

方法二:官网方法。

参考:/article/2469294.html

或:http://www.cnblogs.com/qianxudetianxia/archive/2011/05/29/2060636.html

或:官网http://source.android.com/source/initializing.html

源代码的下载,首先需要安装以下软件:curl 和 git-core

2.1安装git和curl

在终端中执行命令:

$sudo apt-get install git-core curl




说明:

curl是一个向服务器或从服务器传输数据的工具,它支持HTTP 、HTTPS、FTP 、 FTPS 、 SCP、SFTP、TFTP、DICT、TELNET、LDAP或FILE等协议。对于web开发人员来说,它对 HTTP POST的支持为我们提供了很大的帮助。

git是分布式的版本控制系统,在这里负责把远程版本库的源代码clone下来。

2.2在用户目录下新建bin文件

在终端中执行:mkdir ~/bin

将~/bin添加环境变量,作用是:要将git软件下载安装在此目录,在别的目录运行可能会找不到此软件,所以要添加到环境变量中

在终端中执行:PATH=~/bin:$PATH

2.3下载repo脚本

在终端中执行:curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo§ > ~/bin/repo

给repo执行权限:chmod
a+x ~/bin/repo

说明:

repo是一个基于git的版本库管理工具,可以简化android开发中使git的使用。在这里,使用repo和官方提供的android project列表自动批量下载android整个项目,免去了大量下载各个项目的手动工作量。

2.4 下载源代码

2.4.1进入要源代码将要保存的目录

如我的源代码保存在:~/Android/android-source

在终端中执行:cd ~/Android/android-source
进入到src目录中

2.4.2 获取repo最新的配置

初始化repo仓库:

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

$ ~/bin/repo init -u https://android.googlesource.com/platform/manifest
初始化完成后会显示repo initialized in /home/http://www.cnblogs.com/xx,如下图:




一般情况下都会出错,所以建议继续往下看再操作


Using authentication

By default, access to the Android source code is anonymous. To protect the servers against excessive usage, each IP address is associated with a quota.

When sharing an IP address with other users (e.g. when accessing the source repositories from beyond a NAT firewall), the quotas can trigger even for regular usage patterns (e.g. if many users sync new clients from the same IP address within a short period).

In that case, it is possible to use authenticated access, which then uses a separate quota for each user, regardless of the IP address.

The first step is to create a password from the password generator and to save it in ~/.netrc according to the instructions on that
page.

The second step is to force authenticated access, by using the following manifest URI:de>https://android.googlesource.com/a/platform/manifestde>. Notice how the de>/a/de> directory prefix triggers mandatory authentication.
You can convert an existing client to use mandatory authentication with the following command:
$ repo init -u https://android.googlesource.com/a/platform/manifest 
下面是具体操作:


1. 浏览器登录https://android.googlesource.com/new-password,并用gmail帐号登录;

2. 点击网页上的“允许访问”,得到类似:





------涂鸦部分为个人gmail帐号-------

3.把得到授权后的username和password粘到~/.netrc文件里,如果没此文件,新建。

即加入下面部分:



4.强制访问googlesource,执行初始化命令

注意由原来的

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

$ repo init -u https://android.googlesource.com/a/platform/manifest
对于下载不同版本的android代码,指定一个manifest分支(即只指明android对应的版本),使用-b参数(即下载某个版本的源代码),

如:下载基于android4.0的分支为android-4.0.1_r1的代码

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

这里以下载android4.2的分支为de>android-4.2_r1的de>代码为例:

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

也可尝试一下不加“/a”,自己试吧:

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

执行以上命令后,会在/home/zhoulc/work/android4.2/目录中生成一个.repo的目录,此目录保存了将要下载的源代码的相关信息。

2.5.同步代码即可:

$ repo sync

或开启多个线程: $ repo sync -j4

-----------------------------------------------------------------

repo sync时如果出现错误信息(没有出现此错误,请跳过):

$ repo sync

Initializing project platform/abi/cpp ...

fatal: '../platform/abi/cpp.git' does not appear to be a git repository

fatal: The remote end hung up unexpectedly

error: Cannot fetch platform/abi/cpp

解决方案:

可以查看根目录下有个.repo文件夹,在.repo目录下的manifest.xml里找到fetch属性

改成

fetch= "git://Android.git.linaro.org/ "



$ vi .repo/manifest.xml

<?xml version="1.0" encoding="UTF-8"?>

<manifest>

<remote name="aosp"

fetch=".."

review="https://android-review.googlesource.com/" />

<default revision="refs/tags/android-4.2_r1"

remote="aosp"

sync-j="4" />

将fetch=".."修改成fetch= "git://Android.git.linaro.org/ ",再sync就OK了

--------------------------------------------------------------

如果中间出现中断,这个很正常,你可以再次输入 ~/bin/repo sync 命令来进行下载。

根据网速的不同,下载时间也有快慢,最好在睡觉前或下班后下载代码,由于源文件比较大,一般国内都要5小时左右甚至更久。

若是出现以下信息,表示代码以下载完成:

........

........

........
From /home/zhoulc/work/android4.2/.repo/projects/tools/motodev.git/clone.bundle

* [new branch] tools_r21 -> aosp/tools_r21

* [new branch] master -> aosp/master

* [new branch] jb-mr1-release -> aosp/jb-mr1-release

* [new branch] jb-mr1-dev -> aosp/jb-mr1-dev

remote: Counting objects: 2506, done

remote: Finding sources: 100% (4/4)

remote: Total 4 (delta 0), reused 4 (delta 0)

Unpacking objects: 100% (4/4), done.

From https://android.googlesource.com/a/platform/tools/motodev
* [new tag] android-4.2.1_r1 -> android-4.2.1_r1

* [new tag] android-4.2_r1 -> android-4.2_r1

* [new tag] android-cts-4.2_r1 -> android-cts-4.2_r1

* [new tag] android-sdk-support_r11 -> android-sdk-support_r11

Fetching projects: 100% (329/329), done.

Checking out files: 100% (9390/9390), done.out files: 44% (4162/9390)

Checking out files: 100% (6764/6764), done.out files: 0% (2/6764)

Checking out files: 100% (24607/24607), done.ut files: 40% (10053/24607)

Checking out files: 100% (18696/18696), done.ut files: 1% (357/18696)

Checking out files: 100% (361/361), done.ng out files: 48% (175/361)

Checking out files: 100% (175/175), done.ng out files: 43% (76/175)

Checking out files: 100% (2407/2407), done. out files: 1% (39/2407)

Checking out files: 100% (137/137), done.ng out files: 44% (61/137)

Checking out files: 100% (40775/40775), done.ut files: 14% (5947/40775)

Checking out files: 100% (93/93), done.

Checking out files: 100% (450/450), done.

Checking out files: 100% (5265/5265), done. out files: 35% (1853/5265)

Checking out files: 100% (13/13), done.king out files: 15% (2/13)

Syncing work tree: 100% (329/329), done.

==========================下载单个项目的源码===================================

1.下载单个项目的源码

前面我们把android整个项目都下载下来了 ,包括虚拟机,SDK,NDK等,但是对于相当多的应用开发者来说,只需要下载android sdk源代码就足够了,没必要下载3个多G的全部源码。

所以,我们以SDK源码为例子,演示如何使用git下载单个项目工程(下载单个项目不需要repo工具)。

打开网页 http://android.git.kernel.org/,包含大量的android相关的工程。

android SDK的代码包括在android核心框架中。工程路径为 platform/frameworks/base.git。

命令:

?
等待一会儿,大概200M,下载完成后,如下图。




类似的,你可以下载任何其他的单个项目到本地。

2.获取各个版本SDK源码

关于获取各个版本的SDK源码,网上还是身边经常有人急需阿,跪求阿什么的,今天我在这里抛砖引玉,演示一下方法,但是不提供下载,毕竟文件还是有一定的大小的。

首先我们现获取SDK的各版本的标志,有两种方式:1.通过分支branch;2.通过标签tag。

我们先看看结果:

?
分别如下面二图:




图A git branch -a




图B git tag

从上面两张图中我们可以看到各个分支号和标签号,这是我们切换分支和检出标签的基础。关于两者的区别, 如果你只是下载的话,大家暂时不需要知道,branch是可以提交修改的,tag则只是作一个重要的标记,但是不能再修改标签对应的代码了。

大家都知道android在2.3中开始添加了NFC,所以源代码中也会增加NFC目录,我们以此为基础,切换到2.3之前和之后的分支,比较一下。

我们使用git checkout (branch)命令来检出分支,我们分别检出2.2和2.3版本的分支(同一目录下切换):




得到的结果分别是:



图A froyo版本(android2.2)



图B gingerbread版本(2.3版本,姜饼)

对比图A和图B,我们发现图B多了一个nfc目录,说明我们切换到了2.3版本。

检出标签也是一模一样的,比如git check android-sdk-2.1_r1

通过切换不同的分支,我们可以获取所有不同版本的SDK源代码,切换的速度还是很快的。

3.链接android.jar源码

一般我们都喜欢下载各个版本的sdk源码,分别存放在各个版本的sources目录下,以便eclipse方法中android.jar对应链接。这种方法我就不多介绍。

今天,我介绍另外一种方法让我们只需要一个简单的命令就能方便的切换源代码,呵呵,你是不是想到了,没错,就是使用前面的checkout命令切换。

在工程的android.jar右键attach source,选择前面的android-sdk-source文件夹,



链接完来后,就可查看SDK源代码了,如果版本不一致,我们只需要在android-sdk-source目录下执行git checkout branchname切换到相应的版本就OK了,再也不用找各个版本的源代码了。

当然,两种方法各有长短,觉得哪种方便就用哪种好了,本人是linux环境,所以我选择后一种会更好。

============================================================================

三、源码阅读工具安装

3.1安装SourceInsight

3.1.1安装最新版 wine

ubuntu 官方自带了 wine ,但是推荐用 winehq 官方提供的最新版本 wine ,新版本解决了很多以前显得麻烦的问题。

PPA地址:https://launchpad.net/~ubuntu-wine/+archive/ppa
sudo add-apt-repository ppa:ubuntu-wine/ppa

如果命令方式添加软件源失败可以用图形方式:

Adding the WineHQ PPA Repository:

Open the Software Sources menu by launching the Ubuntu Software Center and selecting Edit->Software Sources. Choose the Other Software tab and click Add. Then, copy and paste the line below.ppa:ubuntu-wine/ppa

sudo apt-get update
 sudo apt-get install wine




配置

winecfg

中文问题:
~/ .wine/drive_c/windows/Fonts/   #只留一个你想要显示的中文字体在这

进入Si3572Setup.exe所在目录,运行"wine Si3572Setup.exe" 一直下一步,安装完后输入序列号:SI3US-465643-84290。安装完毕

4.安装编译环境

4.1 Ubuntu12.04安装JDK6

[code]$ sudo add-apt-repository "deb http://us.archive.ubuntu.com/ubuntu/ hardy multiverse"
$ sudo apt-get update
$ sudo apt-get install sun-java6-jdk
$ sudo update-alternatives --config java
$ sudo update-java-alternatives -s java-6-sun
4.2 安装其它一些编译所需软件

在终端中执行:sudo apt-get install git-core gnupg flex bison gperf build-essential \zip curl zlib1g-dev libc6-dev lib32ncurses5-dev ia32-libs \x11proto-core-dev libx11-dev lib32readline5-dev lib32z-dev \libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown \libxml2-utils xsltproc
基本只要安装以上软件即可,官方网站上说的其它一些软件或者设置可以不用理会。注:此步可能会出现错误,请查看本文最后部分的说明

Trouble Shooting:

<command-line>:0:0: warning: "_FORTIFY_SOURCE" redefined [enabled by default]<built-in>:0:0: note: this is the location of the previous definition cc1plus: all warnings being treated as errorsmake: *** [out/host/linux-x86/obj/EXECUTABLES/obbtool_intermediates/Main.o] Error 1后来发现了,原来是Ubuntu11.10里的gcc和g++版本太高了,于是执行下面的操作:sudo apt-get install gcc-4.4sudo apt-get install g++-4.4sudo rm -rf /usr/bin/gcc /usr/bin/g++sudo ln -s /usr/bin/gcc-4.4 /usr/bin/gccsudo ln -s /usr/bin/g++-4.4 /usr/bin/g++把默认的4.6版本换为了4.4,继续编译源码,又出现了另一个错误,大致提示为:g++ selected multilib '32' not installed继续奋战吧,安装相应的工具吧:sudo apt-get install g++-4.4-multilib,现在正在make -j8在我的i5/4G机子里跑着。

[b]5. 编译SDK(可选)。     1. 编译。执行以下命令:     USER-NAME@MACHINE-NAME:~/Android/android-4.0.3_r1$ make sdk2. 编译过程中可能会遇到的问题。     问题一:找不到bios.bin和vgabios-cirrus.bin文件     couldn't locate source file: usr/share/pc-bios/bios.bin     couldn't locate source file: usr/share/pc-bios/vgabios-cirrus.bin     注意,这里的usr/share目录指的是~/Android/out/host/linux-x86目录下的usr/share目录,修改办法是复制~/Android/prebuilt/common下的pc-bios文件夹到~/Android/out/host/linux-x86/usr/share即可:     USER-NAME@MACHINE-NAME:~/Android/android-4.0.3_r1$ cp ~/Android/prebuilt/common/pc-bios ~/Android/out/host/linux-x86/usr/share    问题二:找不到ddmlib-tests.jar、 ninepath-tests.jar 、common-tests.jar 和sdkuilib-tests.jar文件    在~/Android/android-4.0.3_r1/out/host/linux-x86/framework这个目录下,可以找到以下几个文件common.jar、ddmlib.jar、ninepatch.jar、sdkuilib.jar这四个文件,然后将它们分别复制一份,并重命名,命名的原则很简单,就是在原有的名字后面跟上-tests即可。

6. 安装编译好的Android镜像到模拟器上

6.1. 设置环境变量:

我是直接修改/etc/profile文件:sudo gedit /etc/profile &

在文件末尾添加:

export PATH=~/Android/android-4.0.3_r1/out/host/linux-x86/bin:$PATH
ANDROID_PRODUCT_OUT=~/Android/android-4.0.3_r1/out/target/product/generic

修改完后在终端输入source /etc/profile,使得修改的环境变量有效
~/Android/android-4.0.3_r1/out/host/linux-x86/bin有要执行的emulator命令,
~/Android/android-4.0.3_r1/out/target/product/generic是Android镜像存放目录,下面执行emulator命令时会用到。

6.2. 运行模拟器    USER-NAME@MACHINE-NAME:~/Android/android-4.0.3_r1/$ emulator    模拟器运行需要四个文件,分别是Linux Kernel镜像zImage和Android镜像文件system.img、userdata.img和ramdisk.img。执行emulator命令时,如果不带任何参数,则Linux Kernel镜像默认使用~/Android/prebuilt/android-arm/kernel目录下的kernel-qemu文件,而Android镜像文件则默认使用ANDROID_PRODUCT_OUT目录下的system.img、userdata.img和ramdisk.img,也就是我们刚刚编译出来的镜像问题。    当然,我们也可以以指定的镜像文件来运行模拟器,即运行emulator时,即:    USER-NAME@MACHINE-NAME:~/Android$ emulator -kernel ./prebuilt/android-arm/kernel/kernel-qemu -sysdir ./out/target/product/generic -system system.img -data userdata.img -ramdisk ramdisk.img模拟器运行截图如下:注:这里说的Android镜像文件,只是包括system.img、userdata.img和ramdisk.img这三个文件,而Linux Kernel镜像用的是Android为我们预编译好的kernel-qemu镜像。那么,有没有办法使用我们自己编译的Linux Kernel镜像呢?答案上肯定的,这样我们就可以完全DIY自己的Android系统了!/article/1363577.html该文章描述如何编译自己的Linux Kernel镜像。

参考:/article/1363578.html



[/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: