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

在Ubuntu上下载、编译和安装Android最新源代码(实践后修改)

2012-07-02 18:16 507 查看
1. 安装Git工具。Android源代码采用Git工具来管理,与SVN相比,这是一种分布式的源代码管理工具,而SVN是集中式的源代码管理工具。要安装Git工具,在Ubuntu上执行以下命令即可:
USER-NAME@MACHINE-NAME:~$ sudo apt-get install git-core gnupg

2. 安装Java SDK。在Ubuntu上执行以下命令:

USER-NAME@MACHINE-NAME:~$
sudo add-apt-repository ppa:ferramroberto/java

USER-NAME@MACHINE-NAME:~$

sudo apt-get update

USER-NAME@MACHINE-NAME:~$
sudo apt-get install sun-java6-jre sun-java6-plugin

USER-NAME@MACHINE-NAME:~$
sudo apt-get install sun-java6-jdk

3. 依赖的其它包。在Ubuntu上执行以下命令:


USER-NAME@MACHINE-NAME:~$ sudo apt-get install flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl


4. 调试工具。在Ubuntu上执行以下命令:


USER-NAME@MACHINE-NAME:~$ sudo apt-get install valgrind


二. 下载Android源代码工程。

1. 下载repo工具。在Ubuntu上执行以下命令:

USER-NAME@MACHINE-NAME:~$ curl http://git-repo.googlecode.com/files/repo-1.12 > ~/bin/repo

USER-NAME@MACHINE-NAME:~$ chmod 777 repo
USER-NAME@MACHINE-NAME:~$ cp repo /bin/

2. 下载Android最新版本源代码。在Ubuntu上执行以下命令:

USER-NAME@MACHINE-NAME:~$ mkdir Android

USER-NAME@MACHINE-NAME:~$ cd Android
[b]USER-NAME@MACHINE-NAME:~/Android$ repo init -u
[/b]https://android.googlesource.com/platform/manifest

(若链接许久无反应:修改repo文件将第五行
REPO_URL='https://code.google.com/p/git-repo/'
改为

REPO_URL='http://code.google.com/p/git-repo/')

[b]USER-NAME@MACHINE-NAME:~/Android$ repo sync[/b]
经过漫长的等待后,就可以把Android源代码下载下来了。其间可能还有经历下载中断的情况,这时只要重新执行repo sync就可以了。

若出现以下连接错误:

error: Failed connect to android.googlesource.com:443;Connection refused while accessinghttps://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文件

$ sudo 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.173cache.pack.google.com

三. 编译Android源代码。
1. 编译。在Android目录下执行以下命令:
USER-NAME@MACHINE-NAME:~/Android$ make
第一次编译要等待比较久的时间,编译成功后,可以看到下面的输出:
Target system fs image: out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img

Install system fs image: out/target/product/generic/system.img
Target ram disk: out/target/product/generic/ramdisk.img
Target userdata fs image: out/target/product/generic/userdata.img
Installed file list: out/target/product/generic/installed-files.txt
2. 编译过程中可能会遇到的问题。
问题一:You are attempting to build on a 32-bit system.
两个地方需要个修改:
1)修改build/core目录下的main.mk文件:
ifeq ($(BUILD_OS),linux)

build_arch := $(shell uname -m)
#Change the following line for building on a 32-bit system.
#ifneq (64,$(findstring 64,$(build_arch)))
ifneq (i686,$(findstring i686,$(build_arch)))
$(warning ************************************************************)
$(warning You are attempting to build on a 32-bit system.)
$(warning Only 64-bit build environments are supported beyond froyo/2.2.)
2)找到下列文件:
/external/clearsilver/cgi/Android.mk
/external/clearsilver/cs/Android.mk
/external/clearsilver/java-jni/Android.mk
/external/clearsilver/util/Android.mk
修改LOCAL_CFLAGS和LOCAL_LDFLAGS变量:
# This forces a 64-bit build for Java6

# Change the following two lines for building on a 32-bit system.
# LOCAL_CFLAGS += -m64
# LOCAL_LDFLAGS += -m64
LOCAL_CFLAGS += -m32
LOCAL_LDFLAGS += -m32
问题二:Undefined reference to `__dso_handle'
external/stlport/src/monetary.cpp:39: undefined reference to `__dso_handle'
out/target/product/vm/obj/SHARED_LIBRARIES/libstlport_intermediates/src/locale.o: In function `__static_initialization_and_destruction_0':
external/stlport/src/locale.cpp:29: undefined reference to `__dso_handle'
out/target/product/vm/obj/SHARED_LIBRARIES/libstlport_intermediates/src/locale_impl.o: In function `__static_initialization_and_destruction_0':
external/stlport/src/locale_impl.cpp:31: undefined reference to `__dso_handle'
out/target/product/vm/obj/SHARED_LIBRARIES/libstlport_intermediates/src/locale_impl.o: In function `std::_Locale_impl::make_classic_locale()':
external/stlport/src/locale_impl.cpp:670: undefined reference to `__dso_handle'
external/stlport/src/locale_impl.cpp:667: undefined reference to `__dso_handle'
out/target/product/vm/obj/SHARED_LIBRARIES/libstlport_intermediates/src/locale_impl.o:external/stlport/src/locale_impl.cpp:604: more undefined
references to `__dso_handle' follow
collect2: ld returned 1 exit status
修改external/stlport/dll_main.cpp,加入以下声明:
extern "C" {
void * __dso_handle = 0;
}
四. 编译SDK,这一步是可选的。
1. 编译。执行以下命令:
USER-NAME@MACHINE-NAME:~/Android$ make sdk
2. 编译过程中可能会遇到的问题。
问题一:找不到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$ 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/out/host/linux-x86/framework这个目录下,可以找到以下几个文件common.jar、ddmlib.jar、ninepatch.jar、sdkuilib.jar这四个文件,然后将它们分别复制一份,并重命名,命名的原则很简单,就是在原有的名字后面跟上-tests即可。
五. 安装编译好的Android镜像到模拟器上。
1. 设置环境变量:
USER-NAME@MACHINE-NAME:~/Android$ export PATH=$PATH:~/Android/out/host/linux-x86/bin
USER-NAME@MACHINE-NAME:~/Android$ export ANDROID_PRODUCT_OUT=~/Android/out/target/product/generic
其中,~/Android/out/host/linux-x86/bin有我们要执行的emulator命令,而~/Android/out/target/product/generic是Android镜像存放目录,下面执行emulator命令时会用到。
2. 运行模拟器。
USER-NAME@MACHINE-NAME:~/Android$ 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镜像文件了,是不是很酷呢?但是注意,这里说的Android镜像文件,只是包括system.img、userdata.img和ramdisk.img这三个文件,而Linux Kernel镜像用的是Android为我们预编译好的kernel-qemu镜像。那么,有没有办法使用我们自己编译的Linux Kernel镜像呢?答案上肯定的,这样我们就可以完全DIY自己的Android系统了!我将在下一篇文章描述如果编译自己的Linux
Kernel镜像,敬请期待~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: