1.環境
一.vmware 8.0下掛載ubuntu 11.04(64 bit)
二.main OS : win 7
1.1軟體需求
一般情況下,你將需要以下工具:
1.1.1.
Python 2.5 -- 2.7.
1.1.2.
GNU Make 3.81 -- 3.82.建議安裝3.81
1.1.3.
JDK 6 if you wish to build Gingerbread or newer; JDK 5 for Froyo or older.
1.1.4.
Git 1.7 or newer.
1.1.5 GCC and GCC++ : 4.4 -- 4.5
1.1.1.Python:
檢查版本,沒有就安裝
[nash@.....]# python --version
[nash@.....]# sudo apt-get install python
1.1.2.GNU Make 3.81 -- 3.82.
檢查版本,沒有就安裝
[nash@.....]# make --version
[nash@.....]# sudo apt-get install make
1.1.3.JDK 6
檢查版本,沒有就安裝
[nash@.....]# java -version
Installing the JDK
請見
[Linux] ubuntu 11.04(32 bit)建立開發環境
1.1.4.Git
檢查版本,沒有就安裝
[nash@.....]# git --version
[nash@.....]# sudo apt-get install git
1.1..編譯android source code需要的套件
64 bit
[nash@.....]# 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
1.1.7.設定 USB 存去(有ROOT全線可跳過)
在GNU/Linux系統(Ubuntu系統),默認情況下,普通用戶不能直接訪問USB設備。該系統需要進行配置,來允許此類訪問。
推薦的方法是創建一個文件/etc/udev/rules.d/51-android.rules(作為root用戶),內容如下
# adb protocol on passion (Nexus One)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e12", MODE="0600", OWNER="<username>"
# fastboot protocol on passion (Nexus One)
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", ATTR{idProduct}=="0fff", MODE="0600", OWNER="<username>"
# adb protocol on crespo/crespo4g (Nexus S)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e22", MODE="0600", OWNER="<username>"
# fastboot protocol on crespo/crespo4g (Nexus S)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e20", MODE="0600", OWNER="<username>"
# adb protocol on stingray/wingray (Xoom)
SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", ATTR{idProduct}=="70a9", MODE="0600", OWNER="<username>"
# fastboot protocol on stingray/wingray (Xoom)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="708c", MODE="0600", OWNER="<username>"
# adb protocol on maguro/toro (Galaxy Nexus)
SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", ATTR{idProduct}=="6860", MODE="0600", OWNER="<username>"
# fastboot protocol on maguro/toro (Galaxy Nexus)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e30", MODE="0600", OWNER="<username>"
# adb protocol on panda (PandaBoard)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d101", MODE="0600", OWNER="<username>"
# fastboot protocol on panda (PandaBoard)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d022", MODE="0600", OWNER="<username>"
# usbboot protocol on panda (PandaBoard)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d010", MODE="0600", OWNER="<username>"
這種方式在Ubuntu Hardy Heron (8.04.x LTS)和Lucid Lynx (10.04.x LTS)已經測試過可行。其他版本的GNU/Linux的Ubuntu或其他變種可能需要不同的配置。
1.1.8.Setting up ccache(“compiler cache”的縮寫)
該工具會高速緩存編譯生成的信息,並在編譯的特定部分使用高速緩存信息,比如header,這樣就節省了使用cpp解析這些信息所需的時間。假如foobar.h中包含對其他header的引用,ccache會用那文件的cpp-parsed版本来取代include。ccache只是將最後的文本拷貝到只文件中,讓他可以立即被編譯,而不是真的去讀取、理解並解釋其內容。
您可以選擇性地構建使用ccache的編譯工具。 ccache的作用,用於加快重新編譯。如果你時常"make clean",或者如果你經常切換不同的建置產品。
在/home/user/.bashrc中加入環境變數
export USE_CCACHE=1
默認情况下cache會保存在~/.ccache目錄下,如果主目錄位於NFS或其他非本地文件系統上,也將下面環境變數加到/home/user/.bashrc:
export CCACHE_DIR=<path-to-your-cache-directory> #修改為cache目錄位置。
推薦的cache目錄大小50-100GB
prebuilt/linux-x86/ccache/ccache -M 50G
該設置會保存到CCACHE_DIR中,該命令為長效的,不會因為系統重啟而失效。
1.1.8 gcc
Ubuntu11.04 gcc與g++版本為4.5,如果想修改系統default gcc or g++
[nash@.....]# ls -l gcc*
lrwxrwxrwx 1 root root 7 2012-04-02 13:36 gcc -> gcc-4.5
-rwxr-xr-x 1 root root 237012 2011-04-19 04:22 gcc-4.5
安裝比較舊的版本
[nash@.....]# sudo apt-get install gcc-4.4
[nash@.....]# sudo apt-get install gcc++-4.4
安裝完後進到/usr/bin目錄下,檢查結果
[nash@.....]# ls -l gcc*
lrwxrwxrwx 1 root root 7 2012-04-02 13:36 gcc -> gcc-4.5
-rwxr-xr-x 1 root root 224488 2011-04-01 03:31 gcc-4.4
-rwxr-xr-x 1 root root 237012 2011-04-19 04:22 gcc-4.5
發現gcc鏈接gcc-4.5,需將它改為鏈接到gcc-4.4,方法如下:
[nash@.....]# $sudo mv gcc gcc.bak
[nash@.....]# $sudo ln -s gcc-4.4 gcc
同理發現gcc++鏈接gcc++-4.5,需將它改為鏈接到gcc++-4.4,方法如下:
[nash@.....]# ls -l g++*
lrwxrwxrwx 1 root root 7 2012-04-02 13:36 g++ -> g++-4.5
-rwxr-xr-x 1 root root 228520 2011-04-01 03:29 g++-4.4
-rwxr-xr-x 1 root root 236948 2011-04-19 04:18 g++-4.5
[nash@.....]# $sudo mv g++ g++.bak
[nash@.....]# $sudo ln -s g++-4.4 g++
在查看gcc和g++版本:
[nash@.....]# ls -l gcc*
lrwxrwxrwx 1 root root 7 2012-04-03 20:18 gcc -> gcc-4.4
-rwxr-xr-x 1 root root 224488 2011-04-01 03:31 gcc-4.4
-rwxr-xr-x 1 root root 237012 2011-04-19 04:22 gcc-4.5
lrwxrwxrwx 1 root root 7 2012-04-02 13:36 gcc.bak -> gcc-4.5
[nash@.....]# ls -l g++*
lrwxrwxrwx 1 root root 7 2012-04-03 20:18 g++ -> g++-4.4
-rwxr-xr-x 1 root root 228520 2011-04-01 03:29 g++-4.4
-rwxr-xr-x 1 root root 236948 2011-04-19 04:18 g++-4.5
lrwxrwxrwx 1 root root 7 2012-04-02 13:36 g++.bak -> g++-4.5
均顯示gcc version 4.4.5 ,說明gcc 4.4安裝成功。
======================================================================
2:下載android source code
2.1.Installing Repo
Repo 是 Google 為 Android source tree 的管理而寫的一個 script工具,以方便處理 Android 源碼包含的上百個 git repositories,可以更容易地使用Git,更多關於 Repo 的資訊,請參考
Version Control。
請按照下列步驟:
2.1.1請確保在你的主目錄有一個 bin /目錄,並且它有設定在path路徑:
注意在安裝JDK時已經宣告PATH所以可能會讓repo失敗
[nash@.....]# mkdir ~/bin #在home/nash/下建立一個bin的資料夾
[nash@.....]# PATH=~/bin:$PATH
2.1.2下載 Repo script 並確保它是可執行文件:
[nash@.....]# curl https://android.git.kernel.org/repo > ~/bin/repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 19933 0 0 0 0 0 0 --:--:-- --:--:-- --:--:100 19933 100 19933 0 0 22306 0 --:--:-- --:--:-- --:--:100 19933 100 19933 0 0 22302 0 --:--:-- --:--:-- --:--:-- 117k
[nash@.....]# chmod a+x ~/bin/repo
2.2.Initializing a Repo client
安裝完成Repo後, 設置用戶端存取 android 原始碼庫:
2.2.1 建一個空目錄來保存你的文件:
[nash@.....]# mkdir Android
[nash@.....]# cd Android
2.2.2 執行 repo init 去獲取最新最新版本的Repo 與 所有最新的修正檔。 您必須指定manifest的URL, 指出包含在 Android source的各種資源庫 將被放置在您的目錄。
[nash@.....]# repo init -u https://android.googlesource.com/platform/manifest
Your Name [nash.lin]: nash.lin
Your Email [nash.lin@mantis-1.(none)]: xxxxx@gmail.com
Your identity is: nash.lin
is this correct [y/N]? y
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/nash.lin/Android
2.2.3 要開 branch 分支, 加上參數 -b:
[nash@.....]# repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1
2.3 Getting the files
[nash@.....]#repo sync
如果斷線就在重複上面指令即可
下載完成後修改權限
[nash@.....]# chmod -R 777 <PATH android source code>
======================================================================
3 編譯android source code
[nash@.....]# cd Android
3.1 envsetup.sh腳本初始化環境
[nash@.....]# source build/envsetup.sh
including device/moto/wingray/vendorsetup.sh
including device/samsung/crespo/vendorsetup.sh
including device/samsung/maguro/vendorsetup.sh
including device/ti/panda/vendorsetup.sh
including sdk/bash_completion/adb.bash
3.2 選擇target
[nash@.....]# lunch
You're building on Linux
Lunch menu... pick a combo:
1. full-eng
2. full_x86-eng
3. vbox_x86-eng
4. full_wingray-userdebug
5. full_crespo-userdebug
6. full_maguro-userdebug
7. full_panda-userdebug
Which would you like? [full-eng] 1
============================================
PLATFORM_VERSION_CODENAME=AOSP
PLATFORM_VERSION=4.0.4.0.4.0.4
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.38-8-generic-x86_64-with-Ubuntu-11.04-natty
HOST_BUILD_TYPE=release
BUILD_ID=OPENMASTER
OUT_DIR=out
============================================
3.4 開始編譯android source code
[nash@.....]# make
編譯完成後會在source code資料夾內會產生一個out資料夾,在/out/target/product/generic目錄下產生出一些*.img檔,例如 system.img / userdata.img /ramdisk.img等
======================================================================
4. 編譯kernel
在下載下來source code資料夾裡面建立kernel資料夾
[nash@.....]# mkdir kernel
4.1 下載kernel source code
[nash@.....]# git clone https://android.googlesource.com/device/ti/panda
Cloning into panda...
remote: Counting objects: 23, done
remote: Counting objects: 515, done
remote: Finding sources: 100% (85/85)
remote: Getting sizes: 100% (40/40)
remote: Compressing objects: 100% (18/18)
Receiving objects: 100% (492/492), 63.74 MiB | 275 KiB/s, done.
remote: Total 492 (delta 219), reused 489 (delta 216)
Resolving deltas: 100% (219/219), done.
[nash@.....]# ls
Panda
[nash@.....]# cd Panda
4.2 查看kernel目前版本資訊
[nash@.....]# git log kernel
commit 793defdaa84b0375d34757db4dfb8fe437bf4340
Author: Jean-Baptiste Queru
Date: Fri Feb 24 12:34:52 2012 -0800
PandaBoard prebuilt kernel
ebb6cb6 Panda: Support data usage statistic collection.
……………………..
4.3 選擇下載的檔案
[nash@.....]# git clone https://android.googlesource.com/kernel/common.git
[nash@.....]# git clone https://android.googlesource.com/kernel/goldfish.git
[nash@.....]# git clone https://android.googlesource.com/kernel/msm.git $ git clone
[nash@.....]# https://android.googlesource.com/kernel/omap.git $ git clone
[nash@.....]# https://android.googlesource.com/kernel/samsung.git $ git clone
[nash@.....]# https://android.googlesource.com/kernel/tegra.git
4.4 選擇一個版本check out
[nash@.....]# cd goldfish/
[nash@.....]# git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/android-goldfish-2.6.29
remotes/origin/master
[nash@.....]# git checkout -b android-goldfish-2.6.29 origin/android-goldfish-2.6.29
[nash@.....]# git branch -a
4.5 設定編譯參數
[nash@.....]# gedit Makefile
#LDFLAGS_BUILD_ID = $(patsubst -Wl$(comma)%,%,\
# $(call ld-option, -Wl$(comma)--build-id,))
LDFLAGS_BUILD_ID =
[nash@.....]# sudo gedit ~/.bashrc
把前面安裝JDK的PATH mark起來在加入以下參數
export PATH=$PATH:/home/nash/Android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin
export ARCH=arm
export SUBARCH=arm
export CROSS_COMPILE=arm-eabi-
[nash@.....]# source ~/.bashrc
4.6 開始編譯kernel
[nash@.....]# make goldfish_defconfig
#
# configuration written to .config
#
[nash@.....]# make
完成後會在/kernel/Panda/goldfish/arch/arm/boot/下產生一個zImage文件
======================================================================
5. 編譯SDK
在android source code目錄下, 下make PRODUCT-sdk-sdk即可
[nash@.....]# make PRODUCT-sdk-sdk
完成後所產生的SDK會存放在/out/host/linux-x86/sdk裡面,會有兩個檔案
1. android-sdk_eng.xxx_linux-x86.zip
2. android-sdk_eng.xxx_linux-x86
P.S : xxx為使用者帳號名稱
android-sdk_eng.xxx_linux-x86就是我們需要的SDK,建議大家備份起來,因為編譯單獨模組時,有可能會清除SDK目錄
參考資料:
http://source.android.com/source/initializing.html
http://www.2cto.com/os/201201/116762.html
http://www.cnblogs.com/dwayne/archive/2011/11/11/2245383.html
http://www.linuxidc.com/Linux/2011-12/49775.htm