Android 源碼編譯 make sdk ADT
安裝虛擬機時建議設置空間為16G左右,否則後麵編譯源代碼可能會導致空間不夠
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變量,如下
在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 https://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個多小時)
cd ~/project/android/cupcake/out/host/linux-x86/bin
vim ~/.bashrc
在.bashrc中新增環境變量,如下
#java 程序開發/運行的一些環境變量
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
最後進入android桌麵,就說明成功了。
out/host/linux-x86/bin下生成許多有用工具(包括android SDK/tools的所有工具),因此,可以把eclipse中android SDK的路徑指定到out/host/linux-x86/bin進行開發
直接make android源碼時,並沒有make linux kernel。因此是在運行模擬器,所以不用編譯 linux kernel。如果要移值android,或增刪驅動,則需要編譯 linux kernel
linux kernel的編譯將在以後的文章中介紹。
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
再運行模擬器
直接執行make是不包括make sdk的。make sdk用來生成SDK,這樣,我們就可以用與源碼同步的SDK來開發 android了。
‘UNCOMPRESS_DATA_MAX = 1 * 1024 * 1024’ 改為 ‘UNCOMPRESS_DATA_MAX = 2 * 1024 * 1024’
原因是Eclipse編譯工程需要大於1.3M的buffer
注意,我們是先執行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的路徑
下載eclipcse,如果不使用jee版本eclipse,在編譯ADT時會出現如下錯誤
需要下載開發完全版eclipse, 在ubuntu 10.04上直接apt-get install eclipse安裝的為簡化版
可以這樣操作:
1. sudo apt-get install eclipse
2. 下載jee版本的eclipse
https://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,配置環境變量
vim ~/.bashrc
在最後添加:
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.
BUILD FAILED
/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:
/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.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.
注意,這裏需要的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移出來
目前的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,原因還不清楚。
安裝、配置ADT請參考官方文檔
編譯出來的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。
最後更新:2017-04-02 22:16:32