天天看點

在Ubuntu上下載下傳、編譯和安裝Android最新源代碼

看完了前面說的幾本書之後,對Linux Kernel和Android有一定的認識了,是不是心裡蠢蠢欲動,想小試牛刀自己編譯一把Android源代碼了呢?一直習慣使用Windows系統,而Android源代碼是不支援在Windows上編譯上,于是決定使用虛拟機安裝Ubuntu,然後下載下傳、編譯和安裝Android源代碼。

     一. 環境準備。

     1. 磁盤空間預留20G左右,記憶體3G,因為一邊要跑主機,一邊要跑虛拟機,記憶體要求還是比較高的,這樣才會比較流暢。

     2. 安裝VMWare 7.1.4。我的作業系統是Win7,VMWare的版本要新一點的,舊版本的VMWare在網絡支援上比較差,由于要在虛拟機上下載下傳Android源代碼,沒有網絡是萬萬不行的。

     3. 安裝好VMWare後,接下來就安裝Ubuntu系統了。我選擇目前最新的版本ubuntu-11.04-alternate-i386,從網上查到的資料說,要編譯Android源代碼,Ubuntu的最低版本是8.04。下載下傳好後,安裝時采用一直預設安裝即可。

     4. 安裝Git工具。Android源代碼采用Git工具來管理,與SVN相比,這是一種分布式的源代碼管理工具,而SVN是集中式的源代碼管理工具。要安裝Git工具,在Ubuntu上執行以下指令即可:

     USER-NAME@MACHINE-NAME:~$ sudo apt-get install git-core gnupg

     5. 安裝Java SDK。在Ubuntu上執行以下指令:

     USER-NAME@MACHINE-NAME:~$ 

sudo add-apt-repository ppa:ferramroberto/java

USER-NAME@MACHINE-NAME:~$ 

sudo apt-get update

sudo apt-get install sun-java6-jre sun-java6-plugin

sudo apt-get install sun-java6-jdk

     6. 依賴的其它包。在Ubuntu上執行以下指令:

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

     7. 調試工具。在Ubuntu上執行以下指令:

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

    二. 下載下傳Android源代碼工程。

     1. 下載下傳repo工具。在Ubuntu上執行以下指令:

     USER-NAME@MACHINE-NAME:~$ wget https://dl-ssl.google.com/dl/googlesource/git-repo/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

     USER-NAME@MACHINE-NAME:~/Android$ repo sync

     經過漫長的等待(我下載下傳了兩三天)後,就可以把Android源代碼下載下傳下來了。其間可能還有經曆下載下傳中斷的情況,這時隻要重新執行repo sync就可以了。

     三. 編譯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鏡像,敬請期待~

在Ubuntu上下載下傳、編譯和安裝Android最新源代碼

PS:主線上最新源代碼是不穩定版本,使用過程可能會有問題

另外,如果從官方下載下傳不到源代碼(大家懂的),可以從這裡下:http://zhu.im/Android/

本文轉載于http://blog.csdn.net/luoshengyang/article/details/6559955