一、Introduction
今天我們就來談?wù)勅绾卧赨buntu平臺(tái)上面編譯android源碼,我的是ubuntu10.04版本,在進(jìn)行編譯流程講解之前我想講一下 make 、make snod 、make kernel的作用,m、mm、mmm它們的作用以及區(qū)別。
- make: 編譯源碼,生成相應(yīng)的系統(tǒng)鏡像文件。
- make snod: 重新生成一個(gè)system.img系統(tǒng)鏡像文件
- make kernel: 編譯內(nèi)核(可選)一般根據(jù)envsetup.sh文件內(nèi)容而論
- m: Makes from the top of the tree(編譯全部模塊)。
- mm: Builds all of the modules in the current directory(編譯當(dāng)前目錄下的所有模塊)。
- mmm: Builds all of the modules in the supplied directories(編譯指定目錄下的所有模塊)。
二、 編譯Android source以及SDK
2.1、完全編譯
使用make編譯并生成鏡像
~$: cd ~/android/src
~$: make
映像編譯成功后會(huì)在目錄 ~/android/src/out/target/product/generic
下產(chǎn)生一些image文件
ramdisk.img system.img userdata.img android -info.txt
我們可以通過啟動(dòng)模擬器來驗(yàn)證我們是否編譯正確 ,注意,我們最好在system.img所在的目錄下進(jìn)行如下動(dòng)作
~$ emulator -kernel ~/android2.2/prebuilt/android-arm/kernel/kernel-qemu -ramdisk ramdisk.img -debug all -data userdata-qemu.img -system system.img -sysdir . -show-kernel -skin 800x480
如果能正確啟動(dòng)則說明完全編譯成功啦。
完全編譯完后我們就可以使用make sdk命令做一次SDK的編譯拉,步驟如下:
~$ cd ~/android/src
~$ make sdk
注意:如果需要build SDK,隨著版本的不同,我們所需的環(huán)境也不同,編譯android2.2之前的版本需要安裝sun-java5-jdk, 而不是sun-java6-jdk,否則會(huì)出現(xiàn)如下錯(cuò)誤:
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.anjoy.zip
SDK buildinfo: out/target/product/generic/sdk/sdk-build.prop
Docs droiddoc: out/target/common/docs/dx
javadoc: 錯(cuò)誤 - 在 doclet 類 DroidDoc 中,方法 start 已拋出異常 java.lang.reflect.InvocationTargetException
com.sun.tools.javac.code.Symbol$CompletionFailure: 未找到 sun.util.resources.OpenListResourceBundle 的類文件
所以,如果jdk版本不同的話就去官網(wǎng)下載一個(gè)或者在線安裝一個(gè)吧 ,這里我就不多說拉
sdk編譯成功后會(huì)在~/android /src/out/host/linux-x86/sdk/ 生成sdk的文件目錄和壓縮包:
android-sdk_eng.anjoy_linux-x86
android-sdk_eng.anjoy_linux-x86.zip
并在~/android /src/out/target/product/generic(generic是默認(rèn)的產(chǎn)品名)下打包所有的映像文件:
generic-img-eng.anjoy.zip
生成的SDK目錄結(jié)構(gòu)為:
/home/anjoy/android/src/out/host/linux-x86/sdk/android-sdk_eng.anjoy_linux-x86:
總計(jì) 32
drwxrwx--- 6 anjoy anjoy 4096 2011-06-27 17:48 .
drwxr-x--- 3 anjoy anjoy 4096 2011-06-27 17:48 ..
drwxrwx--- 2 anjoy anjoy 4096 2011-06-27 17:48 add-ons
drwxrwx--- 14 anjoy anjoy 4096 2011-06-27 17:48 docs
-rw-rw---- 1 anjoy anjoy 172 2011-06-27 17:50 documentation.html
drwxrwx--- 3 anjoy anjoy 4096 2011-06-27 17:48 platforms
-rw-rw---- 1 anjoy anjoy 225 2011-06-27 17:50 RELEASE_NOTES.txt
drwxrwx--- 3 anjoy anjoy 4096 2011-06-27 17:50 tools
想很方便的使用生成的SDK只需要在.bashrc中增加:
export PATH=$PATH:/home/anjoy/android/src/out/host/linux-x86/sdk/android-sdk_eng.anjoy_linux-x86/tools
2.2、模塊化編譯
注意:在模塊化編譯之前我們一定要把envsetup.sh 腳本source 一下,或者你直接把envsetup.sh文件所在的路徑配置到你個(gè)人的bashrc文件里面,這樣你就不要每次都作source動(dòng)作拉。
envsetup.sh 提供了一些的bash函數(shù)定義,當(dāng)運(yùn)行了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.
- godir: Go to the directory containing a file.
...
..
其中對(duì)模塊的編譯有幫助的是tapas、m、mm、mmm這幾個(gè)命令。
其中mmm 后面要跟模塊的根目錄,不是所有的目錄下都有子模塊,那些含有Android.mk 文件目錄才是模塊的根目錄,模塊名可以從Android.mk 的LOCAL_MODULE 或者LOCAL_PACKAGE_NAME 變量中得到。
單獨(dú)編譯某模塊,需要在mmm后面指定模塊路徑,例如編譯application中的Launcher2:
mmm packages/apps/Launcher2/
或者在src目錄下直接運(yùn)行make module name:
cd ~/android/src
make Launcher2
2.3、增量編譯的步驟
a、假如我們修改了某個(gè)模塊下的代碼,那么我們只需要從新編譯這個(gè)模塊就可以拉,而不需要整個(gè)工程的編譯。
b、編譯所修改的代碼所在模塊,例如:
cd ~/android/src
mmm packages/apps/Launcher2
c、在~/android/src中運(yùn)行:
cd ~/android/src
make snod
d、該命令生成一個(gè)新的系統(tǒng)映像system.img,將這個(gè)系統(tǒng)映像拷貝至sdk下:
cd ~/android/src
cp out/target/product/generic/system.img /
out/host/linux-x86/sdk/android-sdk_eng.anjoy_linux-x86/tools/lib/images/
OK,這樣就完成了Android源碼的編譯以及SDK的生成拉
提醒:如果你是Ubuntu10.04系統(tǒng) 32位機(jī)上安裝編譯Android2.3源碼,其步驟和注意事項(xiàng)如下:
1.安裝JDK6
對(duì)于Android2.3 系統(tǒng),不要安裝JDK5 ,應(yīng)該安裝最新的JDK6 。
如果安裝了JDK6,Android會(huì)自動(dòng)按64位編譯,如果系統(tǒng)是32位的,會(huì)有編譯錯(cuò)誤,后面會(huì)說如何修改這個(gè)錯(cuò)誤。
sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner"
sudo add-apt-repository "deb-src http://archive.canonical.com/ubuntu lucid partner"
sudo apt-get update
sudo apt-get install sun-java6-jdk
sudo update-java-alternatives -s java-6-sun
安裝完后,需要手動(dòng)設(shè)置JAVA_HOME, JRE_HOME , CLASS_PATH為JDK6 的安裝路徑。
2.進(jìn)行編譯android2.3
~$ cd ~/android2.3
~$ source build/envsetup.sh
~$ make
在make的時(shí)候會(huì)提示出錯(cuò):
************************************************************
You are attempting to build on a 32-bit system.
Only 64-bit build environments are supported beyond froyo/2.2.
************************************************************
因?yàn)锳ndroid2.3默認(rèn)是64位的系統(tǒng)上編譯,需要手動(dòng)修改build/core/main.mk,把這個(gè)判斷部分注釋掉:
#ifneq (64,$(findstring 64,$(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.)
#$(warning ************************************************************)
#$(error stop)
#endif
重新make,如果是安裝了JDK6版本,會(huì)又報(bào)錯(cuò):
Docs droiddoc: out/target/common/docs/api-stubs
Could not load ‘clearsilver-jni’
java.library.path = out/host/linux-x86/lib
make: *** [out/target/common/docs/api-stubs-timestamp] Error 45
make: *** Waiting for unfinished jobs….
Could not load ‘clearsilver-jni’
java.library.path = out/host/linux-x86/lib
make: *** [out/target/common/docs/doc-comment-check-timestamp] Error 45
我們只需要修改這幾個(gè)文件,該回到32位編譯環(huán)境即可:
# external/clearsilver/cgi/Android.mk
# external/clearsilver/java-jni/Android.mk
# external/clearsilver/util/Android.mk
# external/clearsilver/cs/Android.mk
把上面這些文件內(nèi)容的編譯選項(xiàng)-m64 改成-m32 即可
重新make,大概不到一個(gè)小時(shí)就make完了。
OK,大工告成
聯(lián)系客服