Android 4.2源码的下载和编译环境的安装及源码的编译(跟着做能成功)

时间:2021-12-16 19:30:47

Google2012.11.14发布了Android4.2的源码,下面把本人在ubuntu12.04 64位系统上下载源码,和编译

源代码下载、编译环境安装、源代码编译和运行模拟器的可能出现的一些错误在本文最后一章节,若出现类似错误,可以参考。

  1. 源码的下载

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

1.1安装curl

在终端中执行:sudo apt-get install curl


1.2安装git-core

在终端中执行:sudo apt-get install git-core


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

在终端中执行:mkdir ~/bin

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

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


1.2.2下载repo脚本

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

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


1.3 下载源代码

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

如我的源代码保存在:/home/zhoulc/work/android4.2/

在终端中执行:/home/zhoulc/work/android4.2/进入到src目录中


1.3.2 获取repo最新的配置

初始化repo仓库:

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


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

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

如果报错且错误信息如下:

Traceback (most recent call last):
  File "/home/zhoulc/work/android4.2/.repo/repo/main.py", line 408, in <module>
    _Main(sys.argv[1:])
  File "/home/zhoulc/work/android4.2/.repo/repo/main.py", line 388, in _Main
    result = repo._Run(argv) or 0
  File "/home/zhoulc/work/android4.2/.repo/repo/main.py", line 138, in _Run
    result = cmd.Execute(copts, cargs)
  File "/home/zhoulc/work/android4.2/.repo/repo/subcmds/init.py", line 322, in Execute
    self._SyncManifest(opt)
  File "/home/zhoulc/work/android4.2/.repo/repo/subcmds/init.py", line 142, in _SyncManifest
    m.PreSync()
  File "/home/zhoulc/work/android4.2/.repo/repo/project.py", line 2137, in PreSync
    cb = self.CurrentBranch
  File "/home/zhoulc/work/android4.2/.repo/repo/project.py", line 539, in CurrentBranch
    b = self.work_git.GetHead()
  File "/home/zhoulc/work/android4.2/.repo/repo/project.py", line 1896, in GetHead
    fd = open(path, 'rb')
IOError: [Errno 2] No such file or directory: '/home/zhoulc/work/android4.2/.repo/manifests/.git/HEAD'

说明:访问android.googlesource.com下载源码时,因为网络和后续Android源码维护升级等因素,有时效性,并不能保证以后一定还起作用。之前Android的代码是在Linux kernel(android.git.kernel.org)管理,被Kernel踢出来之后,Android源码现在直接由Google(android.googlesource.com)管理。

打印的错误很奇怪,Python是正确安装了的,版本也是要求的,找不出哪里出错了呀。其实官网(http://source.android.com/source/downloading.html)有段说明,如果认真执行就可解决。

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. 点击网页上的“允许访问”,得到类似:

Android 4.2源码的下载和编译环境的安装及源码的编译(跟着做能成功)
  
------涂鸦部分为个人gmail帐号-------
3.把得到授权后的username和password粘到~/.netrc文件里,如果没此文件,新建。
即加入下面部分:
Android 4.2源码的下载和编译环境的安装及源码的编译(跟着做能成功)
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的目录,此目录保存了将要下载的源代码的相关信息。


---------------------------------------------------------------------------------------------------------
可能的错误信息(没有,则跳过红色部分):

如果执行$ repo init -u https://android.googlesource.com/a/platform/manifest -b android-4.2_r1
/home/zhoulc/bin/repo: line 1: syntax error near unexpected token `newline'
/home/zhoulc/bin/repo: line 1: `<!DOCTYPE html>

解决方案:
原来下载的repo不能用,得换一个:
执行:
$ curl "http://php.webtutor.pl/en/wp-content/uploads/2011/09/repo"> ~/bin/repo
$ chmod a+x ~/bin/repo

再运行就OK了:
$ repo init -u https://android.googlesource.com/a/platform/manifest -b android-4.2_r1
gpg: keyring `/home/zhoulc/.repoconfig/gnupg/secring.gpg' created
gpg: keyring `/home/zhoulc/.repoconfig/gnupg/pubring.gpg' created
gpg: /home/zhoulc/.repoconfig/gnupg/trustdb.gpg: trustdb created
gpg: key 920F5C65: public key "Repo Maintainer <repo@android.kernel.org>"表ed
。。。。。

Testing colorized output (for 'repo diff', 'repo status'):
  black    red      green    yellow   blue     magenta   cyan     white 
  bold     dim      ul       reverse 
Enable color display in this user account (y/n)? y

repo initialized in /home/zhoulc/work/android4.2
仓库初始化完毕了
------------------------------------------------------------------------------

执行之后的打印信息:
zhoulc@zhoulc-PC:~/work/android4.2$ repo init -u https://android.googlesource.com/a/platform/manifest
Get https://gerrit.googlesource.com/git-repo/clone.bundle
Get https://android.googlesource.com/a/platform/manifest
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100     1  100     1    0     0      3      0 --:--:-- --:--:-- --:--:--     4
100 96126  100 96126    0     0   120k      0 --:--:-- --:--:-- --:--:-- 1168k
Receiving objects: 100% (398/398), 85.13 KiB, done.
Resolving deltas: 100% (143/143), done.
From /home/zhoulc/work/android4.2/.repo/manifests.git/clone.bundle
 * [new branch]      tradefed   -> origin/tradefed
 * [new branch]      tradedef   -> origin/tradedef
 * [new branch]      tools_r20  -> origin/tools_r20
 * [new branch]      master-dalvik -> origin/master-dalvik
 * [new branch]      master     -> origin/master
 * [new branch]      ics-mr1    -> origin/ics-mr1
 * [new branch]      ics-mr0    -> origin/ics-mr0
 * [new branch]      gingerbread-release -> origin/gingerbread-release
 * [new branch]      gingerbread -> origin/gingerbread
 * [new branch]      froyo      -> origin/froyo
 * [new branch]      android-sdk-adt_r20 -> origin/android-sdk-adt_r20
 * [new branch]      android-sdk-adt_r16.0.1 -> origin/android-sdk-adt_r16.0.1
........
........
........
 * [new tag]         android-4.1.1_r1 -> android-4.1.1_r1
 * [new tag]         android-4.1.1_r1.1 -> android-4.1.1_r1.1
 * [new tag]         android-4.1.1_r1_ -> android-4.1.1_r1_
 * [new tag]         android-4.1.1_r2 -> android-4.1.1_r2
 * [new tag]         android-4.1.1_r3 -> android-4.1.1_r3
 * [new tag]         android-4.1.1_r4 -> android-4.1.1_r4
 * [new tag]         android-4.1.1_r5 -> android-4.1.1_r5
 * [new tag]         android-4.1.1_r6 -> android-4.1.1_r6
 * [new tag]         android-4.1.1_r6.1 -> android-4.1.1_r6.1
 * [new tag]         android-4.1.2_r1 -> android-4.1.2_r1
 * [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.1_r1 -> android-cts-4.1_r1
 * [new tag]         android-sdk-support_r11 -> android-sdk-support_r11

Your identity is: xxxx <xxxxx@xxxx.com>
If you want to change this, please re-run 'repo init' with --config-name

repo initialized in /home/zhoulc/work/android4.2
zhoulc@zhoulc-PC:~/work/android4.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.  

zhoulc@zhoulc-PC:~/work/android4.2$ 

运行~/bin/repo sync 还可以重新校对下载的源码,若是有问题或遗漏了一些文件会重新下载,并从.repo目录中导出到实际源码中

注:此步可能会出现下载错误,请查看本文最后部分的说明


2编译环境的安装

首先若是没有修改源,请修改为国内的源,从而提高软件的下载安装的速度

可以参考下面博客的内容添加一些速度快的源,如:*源、Ubuntu官方上海源网易、搜狐、

http://hi.baidu.com/houxn22/item/48f0f7d150657c16d80e4468


安装编译所需的软件

  1. 安装jdk

sudo apt-get install sun-java6-jdk


  1. 安装其它一些编译所需软件


在终端中执行: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


基本只要安装以上软件即可,官方网站上说的其它一些软件或者设置可以不用理会。

注:此步可能会出现错误,请查看本文最后部分的说明


3编译源代码

3.1编译初始化:

在终端中执行:

source build/envsetup.sh

或:

. build/envsetup.sh

执行后输出:

source build/envsetup.sh

including device/asus/grouper/vendorsetup.sh

including device/asus/tilapia/vendorsetup.sh

including device/generic/armv7-a-neon/vendorsetup.sh

including device/generic/armv7-a/vendorsetup.sh

including device/generic/mips/vendorsetup.sh

including device/generic/x86/vendorsetup.sh

including device/samsung/maguro/vendorsetup.sh

including device/samsung/manta/vendorsetup.sh

including device/samsung/toroplus/vendorsetup.sh

including device/samsung/toro/vendorsetup.sh

including device/ti/panda/vendorsetup.sh

including sdk/bash_completion/adb.bash



3.2选择编译目标:

$ lunch full-eng

$ lunch 1

执行后会又以下信息提示:

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

PLATFORM_VERSION_CODENAME=REL

PLATFORM_VERSION=4.2

TARGET_PRODUCT=full

TARGET_BUILD_VARIANT=eng

TARGET_BUILD_TYPE=release

TARGET_BUILD_APPS=

TARGET_ARCH=arm

TARGET_ARCH_VARIANT=armv7-a

HOST_ARCH=x86

HOST_OS=linux

HOST_OS_EXTRA=Linux-2.6.32-45-generic-x86_64-with-Ubuntu-10.04-lucid

HOST_BUILD_TYPE=release

BUILD_ID=JOP40C

OUT_DIR=out

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


3.3编译代码:

在终端中执行:make -j4

-j4:表示用四个线程来编译:


编译根据不同机器的配置不同所需时间也有很大差异,如本人电脑为intel i5-2300四核2.82G内存,晚上8点开始编译,将近凌晨2点才编译完成,所以编译源代码也最好是在睡觉时或下班后进行。


出现以下信息表示编译完成:

..............

Done!
Install: out/target/product/generic/system/app/Browser.odex
Install: out/target/product/generic/system/app/Browser.apk
build/tools/generate-notice-files.py  out/target/product/generic/obj/NOTICE.txt  out/target/product/generic/obj/NOTICE.html "Notices for files contained in the filesystem images in this directory:" out/target/product/generic/obj/NOTICE_FILES/src
Combining NOTICE files into HTML
Combining NOTICE files into text
Installed file list: out/target/product/generic/installed-files.txt
Target system fs image: out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img
Running:  mkyaffs2image -f out/target/product/generic/system out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img
Install system fs image: out/target/product/generic/system.img
zhoulc@zhoulc-PC:~/work/android4.2$ 

编译可能出现的错误,请查看本文最后部分的说明


4、运行模拟器

在终端中执行:emulator


运行模拟器可能出现的错误,请查看本文最后部分的说明


模拟器运行截图:


初始化:



Android 4.2源码的下载和编译环境的安装及源码的编译(跟着做能成功)

开机后的待机界面

Android 4.2源码的下载和编译环境的安装及源码的编译(跟着做能成功)
 

待机二:


Android 4.2源码的下载和编译环境的安装及源码的编译(跟着做能成功)

主菜单一:


Android 4.2源码的下载和编译环境的安装及源码的编译(跟着做能成功)

主菜单二:


Android 4.2源码的下载和编译环境的安装及源码的编译(跟着做能成功)

设置菜单中查看本版号:


Android 4.2源码的下载和编译环境的安装及源码的编译(跟着做能成功)

锁屏一:


Android 4.2源码的下载和编译环境的安装及源码的编译(跟着做能成功)

锁屏二:


Android 4.2源码的下载和编译环境的安装及源码的编译(跟着做能成功)

5、相关问题的解决办法

5.1 下载源代码可能会提示的错误

5.1.1 error: Exited sync due to fetch errors...

希望各位不要出现这个错误,出现这个错误就要折腾一会了

首先继续repo sync,若是一直提示这个错误,那么就按照下面的方法来做吧:

关于这个问题其实google是有说明的http://source.android.com/source/downloading.html,为了防止连接数过多,每个ip都需要认证。。。

第一步:从这里 the password generator  获取用户名和密码,前提是你在之前填写了你的真实姓名和邮箱 

第二步:将上面的页面上以machine开头的两行复制到  ~/.netrc文件中

第三步:repo init -u https://android.googlesource.com/a/platform/manifest  多了个/a”

然后就可以repo sync

特别注意.netrc文件是在用户的根目录下,root用户就是/目录下,如果没有的话就自己建一个,把权限改为 *00


5.1.2若是出现这个错误:Exited sync due to gc errors


表示代码基本下载完,需要从repo里面导出来,但是git的版本太低,不支持1.72以下版本。

添加这个源:deb http://ftp.de.debian.org/debian sid main

在终端中执行:gedit /etc/apt/source.list

在文件的最后添加deb http://ftp.de.debian.org/debian sid main,并保存退出

回到终端中执行:sudo apt-get update

更新完后,安装高版本的git工具

在终端中执行:sudo apt-get install git-core


5.1.3 代码下载过程中,经常提示以下错误:

error: Failed connect to android.googlesource.com:443;Connection refused while accessing https://android.googlesource.com/a/platform/frameworks/base/info/refs

fatal: HTTP request failed

error: Cannot fetch platform/tools/motodev

error: Cannot fetch platform/frameworks/base

error: Cannot fetch platform/prebuilts/sdk

error: Exited sync due to fetch errors

解决方法:编辑/etc/hosts文件

$ vim /etc/hosts

增加下面内容,保存(提前保存好):

74.125.31.82 www.googlesource.com

74.125.31.82 android.googlesource.com

203.208.46.172 cache.pack.google.com

59.24.3.173 cache.pack.google.com


5.2安装编译环境的软件可能会出现的问题

下列软件包有未满足的依赖关系: libasound2: 破坏: libasound2-plugins (< 1.0.24-0ubuntu3) 但是 1.0.22-0ubuntu6正要被安装 libglib2.0-0: 破坏: gnome-control-center (< 1:3) 但是 1:2.30.0-0ubuntu4 正要被安装 ppp: 破坏: network-manager (<= 0.8.0.999-1) 但是 0.8-0ubuntu3 正要被安装E: 破损的软件包


某些源有问题,为高版本 ubuntu的源 ,删除即可,如安装高版本的git所用的源就是ubuntu 10.4之后的源,用在10.04上就可以会有问题,引起此提示。

在终端中执行:gedit /etc/apt/source.list

把文件的最后 deb http://ftp.de.debian.org/debian sid main 删除,并保存退出

回到终端中执行:sudo apt-get update

再次重新安装编译环境的软件


5.3编译可能会遇到的问题:

5.3.1运行make命令后,提示以下错误:/bin/bash: java: command not found

首先检查是否确实安装了jdk,在终端中输入:java -version ,若是有安装jdk,就会显示jdk的版本信息,提示出错,说明没有安装jdk,那么就安装jdk

安装jdk后还是提示上面的错误信息,则按执行以下命令建立链接文件来解决问题:

sudo ln -s /jdk1.6.0_21/bin/jar ~/bin/jar

sudo ln -s /jdk1.6.0_21/bin/java ~/bin/java

sudo ln -s /jdk1.6.0_21/bin/javac ~/bin/javac

sudo ln -s /jdk1.6.0_21/bin/javah ~/bin/javah

sudo ln -s /jdk1.6.0_21/bin/javadoc ~/bin/javadoc


重新编译若还是提示一样的错误,再执行下面的命令:

sudo ln -s /jdk1.6.0_21/bin/jar /bin/jar

sudo ln -s /jdk1.6.0_21/bin/java /bin/java

sudo ln -s /jdk1.6.0_21/bin/javac /bin/javac

sudo ln -s /jdk1.6.0_21/bin/javah /bin/javah

sudo ln -s /jdk1.6.0_21/bin/javadoc /bin/javadoc

至此java的问题应该可以解决

5.3.2 编译出现以下错误提示:

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

Checking build tools versions...

************************************************************

You are attempting to build with the incorrect version

of java.

Your version is: java version "1.6.0_24".

The correct version is: Java SE 1.6.

Please follow the machine setup instructions at

https://source.android.com/source/download.html

************************************************************

build/core/main.mk:141: *** stop 停止。

切换从普通用户登,然后修改源码目录的存取权限:chmod 777 src

之后再运行编译命令再次编译

5.3.3 No command 'lunch' found, did you mean:

解决方法:

运行以下命令:

source build/envsetup.sh

之后再次运行编译命令lunch full-englunch full-user ……..



5.4运行模拟器可能会出现的问题:

No command 'emulator' found, did you mean:

编译成功之后,在终端运行emulator命令,却提示:

No command 'emulator' found, did you mean:

Command 'qemulator' from package 'qemulator' (universe)

emulator: command not found

解决方法:

重新运行以下命令:

source build/envsetup.sh

lunch full-eng

之后再次运行 emulator 命令





本文转载自:http://blog.163.com/zhou_411424/blog/static/1973621562012111782412775/