天天看點

Android 源碼編譯 make sdk ADT

安裝虛拟機時建議設定空間為16g左右,否則後面編譯源代碼可能會導緻空間不夠

編譯環境:ubuntu8.10

1、安裝一些環境

sudo apt-get install build-essential make gcc g++ libc6-dev patch texinfo libncurses-dev git-core gnupg flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl ncurses-dev zlib1g-dev valgrind python2.5 sun-java5-jdk

不要用 sun-java6-jdk,否則會有問題

2、設定環境變量

vim ~/.bashrc

在.bashrc中新增或整合path變量,如下

#java 程式開發/運作的一些環境變量

在home/你的使用者名/.bashrc中加入下面一些,有些是後面運作編譯後的模拟器而用的

export java_home=usr/lib/jvm/java-1.5.0-sun

export jre_home=usr/lib/jvm/java-1.5.0-sun/jre

export path=$path:$java_home/bin

export path=$path:~/mydroid/out/host/linux-x86/bin

export android_product_out=~/mydroid/out/target/product/generic

export classpath=.:${java_home}/lib:$jre_home/lib:$classpath

export java_path=${java_home}/bin:${jre_home}/bin

home_bin=~/bin/

export path=${path}:${java_path}:${jre_path}:${home_bin};

3、安裝repo(用來更新android源碼)

建立~/bin目錄,用來存放repo程式,如下:

$ cd ~

$ mkdir bin

注意:下載下傳後的.repo也許并不在這個檔案夾中,請傳回上一級目錄找到後拷貝至此

下載下傳repo腳本并使其可執行:

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

$ chmod a+x ~/bin/repo

4、下載下傳 android源碼并更新之

$ mkdir mydroid

$ cd mydroid

方式一

建 議不要用repo來下載下傳(android源碼超過1g,非常慢),直接在網上下載下傳http: //www.androidin.com/bbs/pub/cupcake.tar.gz。而且解壓出來的 cupcake下也有.repo檔案夾,可以通過repo sync來更新cupcake代碼:

tar -xvf cupcake.tar.gz

repo sync(更新很慢,用了3個小時)

方式二

$ repo init -u git://android.git.kernel.org/platform/manifest.git

運作到這裡将出現 repo initialized in /home/<your_home>/mydroid 資訊,這時可以認為環境設定已完成

下載下傳android代碼 ,運作如下指令

$ repo sync

我是用的方式二,當然如果之前有下載下傳源碼的話,直接将源碼拷貝到mydroid檔案夾就可以了

5、編譯android源碼,并得到~/mydroid/out 目錄

進入mydroid源碼目錄:

make

這一過程很久(2個多小時)

6、在模拟器上運作編譯好android

android sdk的emulator程式在android-sdk-linux_x86-1.0_r2/tools/下,emulator是需要加載一些image 的,預設加載android-sdk-linux_x86-1.0_r2/tools/lib/images下的kernel-qemu(核心) ramdisk.img system.img userdata.img

編譯好android之後,emulator在 ~/project/android/cupcake/out/host/linux-x86/bin下, ramdisk.img system.img userdata.img則在~/project/android/cupcake/out/target/product/generic下

cd ~/project/android/cupcake/out/host/linux-x86/bin

增加環境變量

在.bashrc中新增環境變量,如下

export android_product_out=~/project/android/cupcake2/out/target/product/generic

android_product_out_bin=~/project/android/cupcake2/out/host/linux-x86/bin

export path=${path}:${android_product_out_bin};

最後,同步這些變化:

source ~/.bashrc

emulator -image system.img -data userdata.img -ramdisk ramdisk.img

最後進入android桌面,就說明成功了。

out/host/linux-x86/bin下生成許多有用工具(包括android sdk/tools的所有工具),是以,可以把eclipse中android sdk的路徑指定到out/host/linux-x86/bin進行開發

7、編譯linux kernel

直接make android源碼時,并沒有make linux kernel。是以是在運作模拟器,是以不用編譯 linux kernel。如果要移值android,或增删驅動,則需要編譯 linux kernel

linux kernel的編譯将在以後的文章中介紹。

8、編譯子產品

android中的一個應用程式可以單獨編譯,編譯後要重新生成system.img

在源碼目錄下執行

. build/envsetup.sh (.後面有空格)

就多出一些指令:

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

可以加—help檢視用法

我們可以使用mmm來編譯指定目錄的子產品,如編譯聯系人:

mmm packages/apps/contacts/

編完之後生成兩個檔案:

out/target/product/generic/data/app/contactstests.apk

out/target/product/generic/system/app/ contacts.apk

可以使用make snod重新生成system.img

再運作模拟器

9、編譯sdk

直接執行make是不包括make sdk的。make sdk用來生成sdk,這樣,我們就可以用與源碼同步的sdk來開發 android了。

1)修改/frameworks/base/include/utils/asset.h

‘uncompress_data_max = 1 * 1024 * 1024’ 改為 ‘uncompress_data_max = 2 * 1024 * 1024’

原因是eclipse編譯工程需要大于1.3m的buffer

2)編譯adt。

注意,我們是先執行2),再執行3)。因為在執行./build_server.sh時,會把生成的sdk清除了。

用上了新的源碼,adt這個調試工具也得自己來生成,步驟如下:

若下載下傳的是cupcake源碼需進入:development/tools/eclipse/scripts目錄,如果下載下傳的是主線代碼,需要進入:sdk/eclipse/scripts目錄(才會有build_server.sh腳本指令),執行如下指令

$export eclipse_home=你的eclipse路徑

$./build_server.sh 你想放adt的路徑

eclipse的安裝注意問題:

下載下傳eclipcse,如果不使用jee版本eclipse,在編譯adt時會出現如下錯誤

需要下載下傳開發完全版eclipse, 在ubuntu 10.04上直接apt-get install eclipse安裝的為簡化版

可以這樣操作:

1. sudo apt-get install eclipse

2. 下載下傳jee版本的eclipse

http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/helios/sr1/eclipse-jee-

helios-sr1-linux-gtk.tar.gz

3. sudo rm -rf /usr/lib/eclipse/

4,解壓

5,sudo mv eclipse /usr/lib

6,配置環境變量

在最後添加:

eclipse_home=/usr/lib/eclipse

export eclipse_home;

export path=${path}:${eclipse_home}

如果沒有添加會出現以下錯誤:

installation error: eclipse plugin org.eclipse.equinox.launcher...jar not detected. found ''. aborting.

8,在使用者目錄下建立一個adt檔案夾

7,development/tools/eclipse/scripts/build_server.sh ~/adt

如果沒有下載下傳完全版的eclipse就會出現以下錯誤:

build failed

/usr/lib/eclipse/plugins/org.eclipse.pde.build_3.6.0.v20100603/scripts/build.xml:35: the following error occurred while executing this line:

/usr/lib/eclipse/plugins/org.eclipse.pde.build_3.6.0.v20100603/scripts/build.xml:91: the following error occurred while executing this line:

/home/luther.gliethttp/sdk/eclipse/buildconfig/customtargets.xml:14: the following error occurred while executing this line:

/home/luther.gliethttp/sdk/eclipse/buildconfig/allelements.xml:27: the following error occurred while executing this line:

/usr/lib/eclipse/plugins/org.eclipse.pde.build_3.6.0.v20100603/scripts/generictargets.xml:107: a cycle was detected when generating the classpath com.android.ide.eclipse.adt_0.9.8.v201009041615, org.eclipse.jdt_3.6.0.v201006080911, org.eclipse.ui.intro_3.4.0.v20100427,

org.eclipse.help.base_3.5.0.v201006080911, org.eclipse.equinox.http.jetty_2.0.0.v20100503, org.mortbay.jetty.util_6.1.23.v201004211559, org.slf4j.api_1.5.11.v20100519-1910, ch.qos.logback.classic_0.9.19.v20100519-1505, ch.qos.logback.core_0.9.19.v20100419-1216,

org.slf4j.api_1.5.11.v20100519-1910.

/usr/lib/eclipse/plugins/org.eclipse.pde.build_3.5.2.r35x_20100114/scripts/build.xml:35: the following error occurred while executing this line:

/usr/lib/eclipse/plugins/org.eclipse.pde.build_3.5.2.r35x_20100114/scripts/build.xml:91: the following error occurred while executing this line:

/usr/lib/eclipse/plugins/org.eclipse.pde.build_3.5.2.r35x_20100114/scripts/generictargets.xml:106: bundle com.android.ide.eclipse.adt_0.0.0 failed to resolve.:

missing required plug-in org.eclipse.gef_0.0.0.

missing required plug-in org.eclipse.wst.sse.core_0.0.0.

missing required plug-in org.eclipse.wst.sse.ui_0.0.0.

missing required plug-in org.eclipse.wst.xml.core_0.0.0.

missing required plug-in org.eclipse.wst.xml.ui_0.0.0.

3)執行make sdk。

注意,這裡需要的javadoc版本為1.5,是以你需要在步驟1中同時安裝sun-java5-jdk

make sdk

編 譯很慢。編譯後生成的sdk存放在out/host/linux-x86/sdk/,此目錄下有android-sdk_eng.xxx_linux- x86.zip和android-sdk_eng.xxx_linux-x86目錄。android-sdk_eng.xxx_linux-x86就是 sdk目錄

實際上,當用mmm指令編譯子產品時,一樣會把sdk的輸出檔案清除,是以,最好把android-sdk_eng.xxx_linux-x86移出來

4)關于環境變量、android工具的選擇

目前的android工具有:

a、我們從網上下載下傳的sdk( tools下有許多android工具,lib/images下有img映像)

b、我們用make sdk編譯出來的sdk( tools下也有許多android工具,lib/images下有img映像)

c、我們用make編譯出來的out目錄( tools下也有許多android工具,lib/images下有img映像)

那麼我們應該用那些工具和img呢?

首先,我們不會用a選項的工具和img,因為一般來說它比較舊,也源碼不同步。測試發現,如果使用b選項的工具和img,android模拟器視窗變小(可能是skin加載不了),而用c選項的工具和img則不會有此問題。

有 些android工具依賴android.jar(比如android),是以,我們在eclipse中使用b選項的工具(sdk),使用c選項的 img。其實,從emulator -help-build-images也可以看出,android_product_out是指向c選項的img目錄的

不過,除了用a選項的工具和img,用b或c的模拟器都不能加載sdcard,原因還不清楚。

5)安裝、配置adt

安裝、配置adt請參考官方文檔

6)建立android virtual device

編譯出來的sdk是沒有avd(android virtual device)的,我們可以通過android工具檢視:

android list

輸出為:

available android targets:

[1] android 1.5

api level: 3

skins: hvga-p, qvga-l, hvga-l, hvga (default), qvga-p

available android virtual devices:

表明沒有avd。如果沒有avd,eclipse編譯工程時會出錯(failed to find a avd compatible with target 'android 1.5'. launch aborted.)

建立avd:

android create avd -t 1 -c ~/sdcard.img -n myavd

可以android –help來檢視上面指令選項的用法。建立中有一些選項,預設就行了

再執行android list,可以看到avd存放的位置

以後每次運作emulator都要加-avd myavd或@myavd選項,這裡eclipse才會在你打開的emulator中調試程式

注意:

這樣,sdk和adt就生成了,就按照官方文檔把他們整合到eclipse,這裡不再細說了。

建個android的新工程,試試你自己編譯的sdk。