Build Android Platform —zhuan http://mmmyddd.freeshell.net/wiki/android/build.html

來源:互聯網
上載者:User

TOP

Website

android
的最新原始碼下載的官方網站是:http://source.android.com

原始碼的下載說明頁面是:http://source.android.com/download
(現在貌似用還用不了)
現在的網站是http://git.android.com


TOP

Create Directory

在HOME(/home/justin)目錄下建立如下目錄結構:

  /home/justin/android
:
  總計 36
  drwxr-xr-x 7 justin justin 4096 01-15 10:34 .
  drwxr-xr-x 58 justin justin 12288 01-15 10:39 ..
  drwxr-xr-x 2 justin justin 4096 01-15 10:34 applications
  drwxr-xr-x 2 justin justin 4096 01-15 10:33 downloads
  drwxr-xr-x 2 justin justin 4096 01-15 12:34 bin
  drwxr-xr-x 2 justin justin 4096 01-15 10:33 sdk
  drwxr-xr-x 2 justin justin 4096 01-15 10:33 src
  drwxr-xr-x 2 justin justin 4096 01-15 10:34 tools

將該目錄添加到PATH環境變數:

export PATH=$HOME/android/bin:$PATH

如果需要反覆使用,需要將這一行加入~/.bashrc啟動指令碼中


TOP

Donwload Tool ChainTOP

Git

Git的版本在1.5.4之上

sudo apt-get install git-core
TOP

Proxy

安裝代理軟體:

apt-get install connect-proxy



TOP

Repo

repo是封裝了git命令的python指令碼:

curl http://android.git.kernel.org/repo > ~/android/bin/repo

網關內部需要設定curl代理:

假設網段的(http)代理為:wwwgate.freeshell.net:8080

curl --proxy wwwgate.freeshell.net:8080 http://android.git.kernel.org/repo > ~/android/bin/repo

加上可執行許可權

chmod a+x ~/android/bin/repo
TOP

Python

Python的版本在2.4之上

sudo apt-get install python
TOP

JDK 1.6

在下列地址下載:
http://java.sun.com/javase/downloads
解壓得到如下檔案:
~/android
/downloads/jdk-6u11-linux-i586.bin

cd ~/android/downloads
chmode a+x jdk-6u11-linux-i586.bin

運行這個可執行檔, 將產生的目錄jdk1.6.0_11拷貝到/usr/local/下

將下列環境變數加入~/.bashrc:

export PATH=/usr/local/jdk1.6.0_11/bin:$PATH
export JAVA_HOME=/usr/local/jdk1.6.0_11
export ANDROID_JAVA_HOME=$JAVA_HOME
TOP

Other tools
sudo apt-get install gcc g++
sudo apt-get install flex bison gperf libsdl1.2-dev libesd0-dev libwxgtk2.6-dev /
build-essential zip curl libncurses5-dev zlib1g-dev valgrind libreadline5-dev

如果因為缺少X11/Xatom.h和X11/Xlib.h導致的build失敗,
可以安裝如下包:

sudo apt-get install x11proto-core-dev  # provides Xatom.h
sudo apt-get install libx11-dev # provides Xlib.h
TOP

交叉編譯環境

android
emulator 預設的目標代碼是arm7的代碼。如果需要將目標代碼移植到其他
版本的arm平台上去,需要重新設定相應的交叉編譯環境。


TOP

下載Android平台原始碼

1. 初始化要下載的檔案清單:

cd ~/android/src
repo init -u git://android.git.kernel.org/platform/manifest.git

如果想檢出除master外其他分支上的代碼可以用-b選項:

cd ~/android/src
repo init -u git://android.git.kernel.org/platform/manifest.git -b cupcake

2. 配置git帳戶

git config --global user.email "xxxxx@xxxxxxx"
git config --global user.name "xxxxxx"

3. 同步檔案清單:

repo sync

在第一次下載全部程式碼完成後,可以按模組更新子項目的代碼:

repo sync project-path

其中的project-path可以在src/.repo/manifests/default.xml中找到:

<?xml version="1.0" encoding="UTF-8"?>
<manifest>
<remote name="korg"
fetch="git://android.git.kernel.org/"
review="review.source.android.com" />
<default revision="master"
remote="korg" />

<project path="build" name="platform/build">
<copyfile src="core/root.mk" dest="Makefile" />
</project>

<project path="kernel"
name="kernel/common"
revision="refs/heads/android-2.6.27" />

<project path="bionic" name="platform/bionic" />

<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" />
... ...

在網關內部使用需要設定代理:

建立如下檔案:

#!/bin/sh
# /home/justin/android/bin/socks-gw.sh
# This script connects to a HTTP proxy using connect.c
connect -H wwwgate.freeshell.net:8080 $@
#!/bin/sh
# /home/justin/android/bin/socks-ssh.sh
ssh -o ProxyCommand="/home/justin/android/bin/socks-gw.sh %h %p" $@

增加這兩個檔案的可執行許可權:

chmod a+x /home/justin/android/bin/socks-ssh.sh
chmod a+x /home/justin/android/bin/socks-gw.sh

可以將所有代理的環境變數放在:

#!/bin/sh
# /home/justin/proxy.sh

# http proxy setting
export HTTP_PROXY=http://wwwgate.freeshell.net:8080
export http_proxy=$HTTP_PROXY

# set git to use ssh over http proxy
export GIT_SSH="/home/justin/android/bin/socks-ssh.sh"
export GIT_PROXY_COMMAND="/home/justin/android/bin/socks-gw.sh"

需要的時候運行:

. ~/proxy.sh

TOP

編譯Android平台以及SDK
TOP

完全編譯TOP

編譯映像
cd ~/android/src
make

映像編譯成功後會在目錄

~/android/src/out/target/product/generic

下產生一些image檔案

ramdisk.img
system.img
userdata.img
android
-info.txt

驗證,運行這些模組:

export ANDROID_PRODUCT_OUT=/home/justin/android/src/out/target/product/generic
cd out/host/linux-x86/bin
./emulator
TOP

SDK編譯


在做完一次完全編譯後,就可以build SDK了。

make sdk

注意:如果需要build SDK,需要安裝sun-java5-jdk, 而不是sun-java6-jdk,否則會出現如下錯誤:

build/core/product_config.mk:207: WARNING: adding test OTA key
============================================
TARGET_PRODUCT=generic
TARGET_BUILD_VARIANT=eng
TARGET_SIMULATOR=
TARGET_BUILD_TYPE=release
TARGET_ARCH=arm
HOST_ARCH=x86
HOST_OS=linux
HOST_BUILD_TYPE=release
BUILD_ID=
============================================
Combining NOTICE files: out/target/product/generic/obj/NOTICE.txt
Finding NOTICE files: out/host/linux-x86/obj/NOTICE_FILES/hash-timestamp
Combining NOTICE files: out/host/linux-x86/obj/NOTICE.txt
Package: out/target/product/generic/generic-img-eng.justin.zip
SDK buildinfo: out/target/product/generic/sdk/sdk-build.prop
Docs droiddoc: out/target/common/docs/dx
javadoc: 錯誤 - 在 doclet 類 DroidDoc 中,方法 start 已拋出異常 java.lang.reflect.InvocationTargetException
com.sun.tools.javac.code.Symbol$CompletionFailure: 未找到 sun.util.resources.OpenListResourceBundle 的類檔案

此時,可以考慮重新安裝sun jdk5, 或者直接從http://java.sun.com/javase/downloads
下載到~/android
/downloads/jdk-1_5_0_17-linux-i586.bin

sudo apt-get install sun-java5-jdk

並設定相應的.bashrc命令。

sdk編譯成功後會在~/android
/src/out/host/linux-x86/sdk/
產生sdk的檔案目錄和壓縮包:

android
-sdk_eng.justin_linux-x86
android
-sdk_eng.justin_linux-x86.zip

並在~/android
/src/out/target/product/generic(generic是預設的產品名)下打包所有的映像檔案:

generic-img-eng.justin.zip

產生的SDK目錄結構為:

  /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86:
總計 32
drwxrwx--- 6 justin justin 4096 02-13 17:06 .
drwxr-x--- 3 justin justin 4096 02-13 17:09 ..
drwxrwx--- 2 justin justin 4096 02-13 17:06 add-ons
drwxrwx--- 14 justin justin 4096 02-13 17:06 docs
-rw-rw---- 1 justin justin 172 02-13 17:08 documentation.html
drwxrwx--- 3 justin justin 4096 02-13 17:06 platforms
-rw-rw---- 1 justin justin 225 02-13 17:08 RELEASE_NOTES.txt
drwxrwx--- 3 justin justin 4096 02-13 17:08 tools

安裝產生的SDK只需要在.bashrc中增加:

export PATH=$PATH:/home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools

為了使用方便,將產生的SDK目錄鏈結至~/android
/sdk:

ln -sf /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools /
~/android/sdk

TOP

模組編譯

在src目錄執行:

cd ~/android/src
. build/envsetup.sh

envsetup.sh 提供了一些的bash函數定義,當運行了envsetup.sh後就可以使用help
命令來查看:

help

得到這些命令的協助資訊:

Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
- croot: Changes directory to the top of the tree.
- m: Makes from the top of the tree.
- mm: Builds all of the modules in the current directory.
- mmm: Builds all of the modules in the supplied directories.
- cgrep: Greps on all local C/C++ files.
- jgrep: Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.

Look at the source to view more functions. The complete list is:
add_lunch_combo cgrep check_product check_variant choosecombo chooseproduct choosetype choosevariant croot findmakefile gd
bclient get_abs_build_var getbugreports get_build_var getprebuilt gettop help isviewserverstarted jgrep lunch m mm mmm pid
printconfig print_lunch_menu resgrep runhat runtest setpaths set_sequence_number set_stuff_for_environment settitle smoke
test startviewserver stopviewserver tapas tracedmdump

其中對模組的編譯有協助的是tapas、m、mm、mmm這幾個命令。

1.tapas - 以互動方式設定build環境,以下是運行效果:

tapas

第一步,選擇目標裝置:

Build for the simulator or the device?
1. Device
2. Simulator

Which would you like? [1]

第二步,選擇目標代碼格式:

Build type choices are:
1. release
2. debug

Which would you like? [1]

第三步,選擇產品平台:

Product choices are:
1. emulator
2. generic
3. sim
You can also type the name of a product if you know it.
Which would you like? [generic]

第四步,在選用參數下構建平台。

2. 獨立模組的構件命令

  • m: Makes from the top of the tree.
  • mm: Builds all of the modules in the current directory.
  • mmm: Builds all of the modules in the supplied directories.

其中mmm後面要跟模組的根目錄,不是所有的目錄下都有子模組,那些含有Android.mk檔案目錄才是
模組的根目錄,模組名可以從Android.mk的LOCAL_MODULE或者LOCAL_PACKAGE_NAME變數中得到。

單獨編譯某模組,需要在mmm後面指定模組路徑,例如編譯application中的Contacts:

mmm packages/apps/Contacts/

或者在src目錄下直接運行make module name

cd ~/android/src
make Contacts

TOP

增量編譯的步驟

1. 修改代碼

2. 編譯所修改的代碼所在模組,例如:

cd ~/android/src
mmm packages/apps/Contacts

3. 在~/android
/src中運行:

cd ~/android/src
make snod

該命令產生一個新的系統映像system.img

4.將這個系統映像拷貝至sdk下:

cd ~/android/src
cp out/target/product/generic/system.img /
out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/lib/images/

5. 刪除程式遺留的資料:

out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/emulator -wipe-data

 



Contact:

mmmyddd@freeshell.org

2009 CopyLeft Contributed



[ Top
| Up
| Dir
| Home
]


Last updated 2009-02-13

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.