2010年10月11日 星期一

[Android] Lession 2 編譯並在模擬器上跑Android

閱讀本文請參考先前的介紹

編譯前設定
依照官方網頁說明,編譯之前需要執行 build/envsetup.sh,這個動作會加入一些常用指令,請在終端機執行help參閱。
jimmy@ubuntu:~/mydroid$. build/envsetup.sh
including device/htc/dream/vendorsetup.sh
including device/htc/passion/vendorsetup.sh
including device/htc/sapphire/vendorsetup.sh
jimmy@ubuntu:~/mydroid$ help
Invoke ". build/envsetup.sh" from your shell to add the following functions to your nvironment:
- 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.

Look at the source to view more functions. The complete list is:
add_lunch_combo cgrep check_product check_variant choosecombo chooseproduct choosetype choosevariant cproj croot findmakefile gdbclient get_abs_build_var getbugreports get_build_var getprebuilt gettop godir help isviewserverstarted jgrep lunch m mm mmm pid printconfig print_lunch_menu resgrep runhat runtest setpaths set_sequence_number set_stuff_for_environment settitle smoketest startviewserver stopviewserver systemstack tapas tracedmdump
通常執行完 build/envsetup.sh 之後,預設的設定是 generic-eng ,也就是模擬器可以跑的版本,如果你是要給自己的手機用的,可以執行lunch或是tapas來挑選你自己想要的設定。
jimmy@ubuntu:~/mydroid$ lunch

You're building on Linux

Lunch menu... pick a combo:
1. generic-eng
2. simulator

Which would you like? [generic-eng] 1
當你執行過lunch,會自動設定一些環境變數,列出幾個重要的如下(這裡的是我的例子):
# 預定要編譯的版本
TARGET_PRODUCT=generic
TARGET_BUILD_TYPE=release
TARGET_BUILD_VARIANT=eng
# 編譯輸出檔案和映像檔的預設位置
ANDROID_PRODUCT_OUT=/home/jimmy/mydroid/out/target/product/generic
開始編譯
設定好之後,直接make編譯,如果想要自己編譯sdk就執行make sdk編譯sdk。我們會編譯出來的東西都在out資料夾裡面,但是也可以自訂位置。
# Normal build
. build/envsetup.sh
# 編譯 Android source code 加上 SDK
make -j2 sdk
# 也可以只編譯 Android source code
make -j2
編譯出來的映像檔案 system.img、ramdisk.img、userdata.img 就擺在環境變數 ANDROID_PRODUCT_OUT 指定的位置。
#ANDROID_PRODUCT_OUT 的預設位置
mydroid/out/target/product/generic
編譯出來的sdk資料夾跟壓縮檔則擺在這裡。
mydroid/out/host/linux-x86/sdk/android-{Your_Build}_linux-x86
mydroid/out/host/linux-x86/sdk/android-{Your_Build}_linux-x86.zip
如果是只針對framework需要修改,那麼sdk不用每次都編譯,因為每次make都會把out/host資料夾都刪除。所以第一次編譯出sdk的時候就可以先把sdk資料夾或是壓縮檔複製下來,擺到其他地方使用(例如家目錄)。
cd ~/
#複製SDK到其他位置
cp mydroid/out/host/linux-x86/sdk/android-{Your_Build}_linux-x86.zip .
unzip android-{Your_Build}_linux-x86.zip
#方便起見建一個link
ln -s android-{Your_Build}_linux-x86 mysdk
新增/設定Android Virtual Machine (AVD)
接下來利用sdk的工具,有幾個命令需要先熟悉,android、emulator和adb,可以參考官方網頁說明
先新增一個AVD,我們需要用到SDK的工具,將下載來的SDK或是自己編譯好的SDK複製到自己選定的位置,這邊的例子是mysdk。
cd ~/mysdk/tools
# 列出所有 AVDs
./android list avd
# 新增一個AVD,名字myavd,Target ID為1,設置128M bytes SD Card。
# Target ID 可以打 ./android list targets 來查詢,一般有成功編譯應該會有。
./android create avd -n myavd -t 1 -c 128M
執行模擬器
終於我們可以開始跑模擬器了,直接進入先前我們複製好的SDK資料夾,執行emulator。
cd ~/mysdk/tools/
#方便我們指定編譯出來的system.img等檔案,
#再建一個link到環境變數ANDROID_PRODUCT_OUT指定的位置。
ln -s mydroid/out/target/product/generic imgs
#不使用AVD建立時指定的映像檔,改用我們指定的
# system.img ramdisk.img userdata.img
./emulator -system imgs/system.img -ramdisk imgs/ramdisk.img -data imgs/userdata.img

更多有用的資料,請參考emulator的說明

沒有留言:

張貼留言