【轉】Ubuntu10.04上編譯Android源碼(Build Android source in Ubuntu10.04 Platform)

來源:互聯網
上載者:User

標籤:

原文網址:http://blog.csdn.net/chenyafei617/article/details/6570928

一、Introduction

          今天我們就來談談如何在Ubuntu平台上面編譯android源碼,我的是ubuntu10.04版本,在進行編譯流程講解之前我想講一下 make 、make snod 、make kernel的作用,m、mm、mmm它們的作用以及區別。

- make:                 編譯源碼,產生相應的系統鏡像檔案。                                                            
- make snod:       重建一個system.img系統鏡像檔案
- make kernel:    編譯核心(可選)一般根據envsetup.sh檔案內容而論


- 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(編譯指定目錄下的所有模組)。

 

二、 編譯Android source以及SDK

2.1、完全編譯

使用make編譯並產生鏡像

~$: cd ~/android/src
~$: make

 

映像編譯成功後會在目錄 ~/android/src/out/target/product/generic 下產生一些image檔案

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

 

我們可以通過啟動模擬器來驗證我們是否編譯正確 ,注意,我們最好在system.img所在的目錄下進行如下動作

~$ 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

如果能正確啟動則說明完全編譯成功啦。

 

 

完全編譯完後我們就可以使用make sdk命令做一次SDK的編譯拉,步驟如下:

~$ cd ~/android/src
~$ make sdk 

注意:如果需要build SDK,隨著版本的不同,我們所需的環境也不同,編譯android2.2之前的版本需要安裝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.anjoy.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 的類檔案 

所以,如果jdk版本不同的話就去官網下載一個或者線上安裝一個吧 ,這裡我就不多說拉

 

 

sdk編譯成功後會在~/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是預設的產品名)下打包所有的映像檔案:

generic-img-eng.anjoy.zip

 

產生的SDK目錄結構為:

/home/anjoy/android/src/out/host/linux-x86/sdk/android-sdk_eng.anjoy_linux-x86:
總計 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檔案所在的路徑配置到你個人的bashrc檔案裡面,這樣你就不要每次都作source動作拉。

 

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.
- godir:   Go to the directory containing a file.
...  
.. 

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

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

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

mmm packages/apps/Launcher2/

 

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

cd ~/android/src
make Launcher2 


2.3、增量編譯的步驟

a、假如我們修改了某個模組下的代碼,那麼我們只需要從新編譯這個模組就可以拉,而不需要整個工程的編譯。

 

b、編譯所修改的代碼所在模組,例如:

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

c、在~/android/src中運行:

cd ~/android/src
make snod

d、該命令產生一個新的系統映像system.img,將這個系統映像拷貝至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系統 32位機上安裝編譯Android2.3源碼,其步驟和注意事項如下:

 

1.安裝JDK6 
對於Android2.3 系統,不要安裝JDK5 ,應該安裝最新的JDK6 。
如果安裝了JDK6,Android會自動按64位編譯,如果系統是32位的,會有編譯錯誤,後面會說如何修改這個錯誤。
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 
安裝完後,需要手動設定JAVA_HOME, JRE_HOME , CLASS_PATH為JDK6 的安裝路徑。

 

2.進行編譯android2.3

~$ cd ~/android2.3
~$ source build/envsetup.sh
~$ make

 

在make的時候會提示出錯:

    ************************************************************
    You are attempting to build on a 32-bit system.
    Only 64-bit build environments are supported beyond froyo/2.2.
    ************************************************************

因為Android2.3預設是64位的系統上編譯,需要手動修改build/core/main.mk,把這個判斷部分注釋掉:

#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版本,會又報錯:

    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

 

我們只需要修改這幾個檔案,該回到32位編譯環境即可:

# external/clearsilver/cgi/Android.mk
# external/clearsilver/java-jni/Android.mk
# external/clearsilver/util/Android.mk
# external/clearsilver/cs/Android.mk

 

把上面這些檔案內容的編譯選項-m64 改成-m32 即可
重新make,大概不到一個小時就make完了。

 

OK,大工告成

 

Android常用的一些make命令

http://blog.chinaunix.net/uid-26926660-id-3216956.html

1.make -jXX
  XX表示數字,這個命令將編譯Android系統並產生鏡像,XX表示可以使用到的CPU核心數,這在配置好的電腦上特別有用,公司的16核ubuntu伺服器執行make -j16隻要不到20分鐘,而雙核PC上需要4個小時!

2.make snod
  
這條命令將重建鏡像,消耗的時間很少,類似WinCE的makeimg過程,如果你修改了一些資料檔案(如音樂、視頻)等檔案時比較有用。

3.make cts
  這條命令將編譯CTS套機,編譯出的結果放在out目錄對應版的data/app目錄下面。CTS測試時有用

4.make installclean
  
這條命令清除out目錄下對應板檔案夾中的內容,也就是相當於make clean,通常如果改變了一些資料檔案(如去掉)、最好執行以下make installclean,否則殘留在out目錄下的還會被打包進去。

5.mm/mm -B
  
開發調試中最喜歡這條命令了,在修改了的目錄下執行這條命令,就能智能地進行編譯,輸出的檔案在通過adb推送到目標機,可以很方便地調試。

6.make sdk
  
這條命令可以產生可發布的SDK,目前還沒試過,據說需要JDK1.5

7.make Setting
  
可以單獨編譯setting這個模組,目前還沒試,猜想是不是可以單獨編譯Email、Music這些模組

8.make bootimage
  
用這條命令可以產生boot.img,這個鏡像檔案中包含Linux Kernel,Ram disk,產生的boot.img只能通過fastboot進行燒寫,這在只修改了Linux核心的時候有用

【轉】Ubuntu10.04上編譯Android源碼(Build Android source in Ubuntu10.04 Platform)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.