版權(quán)聲明:本文為作者原創(chuàng),轉(zhuǎn)載必須注明出處。
轉(zhuǎn)載請(qǐng)注明出處:http://www.lxweimin.com/p/5576c2759a1c
build for android
這篇文章主要介紹如何在Ubuntu 14.04版本上下載和編譯Android源碼。
以下步驟都是作者親自實(shí)踐,廢話不多說,我們開始吧!
一、準(zhǔn)備
安裝編譯Android source code的相關(guān)包以及JDK
-
安裝JDK, 要注意的是不同Android版本是需要不同的JDK版本, Android K之前的是JDK 1.6, L, M是JDK 1.7, N以上是JDK 1.8
$ sudo apt-get update $ sudo apt-get install openjdk-8-jdk $ java -version (安裝完后可以執(zhí)行此命令確認(rèn)是否安裝成功以及版本正確)
-
安裝相關(guān)包 (以Ubuntu 14.04版本為例)
$ sudo apt-get install git-core gnupg flex bison gperf build-essential \ zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 \ lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache \ libgl1-mesa-dev libxml2-utils xsltproc unzip
-
未來若需要切換JDK, 可以用以下方法
$ sudo update-alternatives --config java $ sudo update-alternatives --config javac
其他版本的JDK安裝請(qǐng)參考
http://source.android.com/source/initializing.html 抓source code 源碼
- 設(shè)置repo, 由Google發(fā)明的一個(gè)管理多個(gè)git的腳本 https://source.android.com/source/using-repo.html
1.準(zhǔn)備repo環(huán)境變量和環(huán)境目錄$ mkdir ~/bin $ PATH=~/bin:$PATH
- 下載repo
$ curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > ~/bin/repo (需要翻墻) or 鏈接: [https://pan.baidu.com/s/1bpLwXCb](https://pan.baidu.com/s/1bpLwXCb) 密碼: hjsp (下載放到~/bin底下)
- 修改執(zhí)行權(quán)限
$ chmod a+x ~/bin/repo
- 抓Code
- 運(yùn)行 repo init 以獲取最新版本的 Repo 及其最近的所有錯(cuò)誤更正內(nèi)容。您必須為清單指定一個(gè)網(wǎng)址,該網(wǎng)址用于指定 Android 源代碼中包含的各個(gè)代碼庫將位于工作目錄中的什么位置。
要檢出“master”以外的分支,請(qǐng)使用repo init -u https://android.googlesource.com/platform/manifest
-b
指定相應(yīng)分支。要查看分支列表,請(qǐng)參閱源代碼標(biāo)記和編譯版本。
初始化成功后,系統(tǒng)將顯示一條消息,告訴您 Repo 已在工作目錄中完成初始化??蛻舳四夸浿鞋F(xiàn)在應(yīng)包含一個(gè) .repo 目錄,清單等文件將保存在該目錄下。repo init -u https://android.googlesource.com/platform/manifest -b android- 4.0.1_r1
- 同步source code, 大約3小時(shí), 這步非常久建議去吃個(gè)飯
(我本人沒遇到問題, 教學(xué)文件有說可能會(huì)遇到"exited sync due to fetch errors"錯(cuò)誤, 可以加上"-f", 其余的只能Google或是問人)$ repo sync -j4
- 運(yùn)行 repo init 以獲取最新版本的 Repo 及其最近的所有錯(cuò)誤更正內(nèi)容。您必須為清單指定一個(gè)網(wǎng)址,該網(wǎng)址用于指定 Android 源代碼中包含的各個(gè)代碼庫將位于工作目錄中的什么位置。
二、正式編譯源碼
- 添加Android編譯functions到當(dāng)前環(huán)境, ex: lunch, mm, (***此步驟在每次打開新的Terminal時(shí), 都必須執(zhí)行)
$ source ./build/envsetup.sh 以下是對(duì)這個(gè)指令的基本功能介紹, 更多介紹可以參考 http://gityuan.com/2016/03/19/android-build/ (很實(shí)用) Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment: - lunch: lunch <product_name>-<build_variant> - tapas: tapas [<App1> <App2> ...] [arm|x86|mips|armv5|arm64|x86_64|mips64] [eng|userdebug|user] - 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, but not their dependencies. - mmm: Builds all of the modules in the supplied directories, but not their dependencies. To limit the modules being built use the syntax: mmm dir/:target1,target2. - mma: Builds all of the modules in the current directory, and their dependencies. - mmma: Builds all of the modules in the supplied directories, and their dependencies. - cgrep: Greps on all local C/C++ files. - ggrep: Greps on all local Gradle files. - jgrep: Greps on all local Java files. - resgrep: Greps on all local res/*.xml files. - mangrep: Greps on all local AndroidManifest.xml files. - mgrep: Greps on all local Makefiles files. - sepgrep: Greps on all local sepolicy files. - sgrep: Greps on all local source files. - godir: Go to the directory containing a file.
- 根據(jù)手機(jī)型號(hào)設(shè)定環(huán)境變量 (***此步驟在每次打開新的Terminal時(shí), 都必須執(zhí)行)
$ lunch <product name> or $ lunch (會(huì)出現(xiàn)一個(gè)數(shù)字選單來選擇) 有分eng版(debug版本), user版(release版本), userdebug(能夠root的release版本) 也可以用 $ print_lunch_menu 指令看一下該repo底下有哪些product可以build
- 廢話不多說, 一言不合就編譯, 全新build很慢, 大約要1.5小時(shí)
但如果很不幸的遇到build break, 你會(huì)發(fā)現(xiàn)log洗滿了整個(gè)屏幕, 你也找不到錯(cuò)誤訊息. 所以你該做的是把log存下來, 如此才能分析問題。$ make -j4 target-files-package (數(shù)字4代表多線程數(shù)量一般來說建議是CPU核心數(shù)x2, 我有用過-j16但是并沒有快多少) 關(guān)于target-files-package參數(shù)說明 make target-files-package 比make精簡了一些操作,可以更快速打包。有時(shí)發(fā)現(xiàn)直接make -j4會(huì)報(bào)錯(cuò), 但是make -j4 target-files-package 不會(huì)報(bào)錯(cuò),原因可能就是因?yàn)閙ake多出來的編譯命令導(dǎo)致的錯(cuò)誤。
make -j4 target-files-package 1>out.txt 2>err.txt & tail -F out.txt (紅色的部份表示把stdout 存到out.txt, stderr存到err.txt, 最后用tail 去follow out.txt, 才能在屏幕上看到編譯狀況) 又或者你可以用tee make -j4 2>&1 | tee out.txt (粉色的部份表示把stderr也導(dǎo)向stdout, 最后用tee同時(shí)輸出到屏幕以及檔案)
三、總結(jié):
如果一切順利,相信通過上面的操作你已經(jīng)完成了源碼的編譯??雌饋硎遣皇呛芎唵?。如果沒有成功的同學(xué),也不要沮喪。大多數(shù)的原因,應(yīng)該是你的系統(tǒng)中缺少相關(guān)的依賴庫導(dǎo)致的。當(dāng)然特定問題特定分析,下面我會(huì)列出一些之前其他同學(xué)遇到過的問題,來幫助大家來檢查。
- 在安裝編譯Android source code的相關(guān)包以及JDK環(huán)節(jié):
如果出現(xiàn)【無法修正錯(cuò)誤,因?yàn)槟竽承┸浖3脂F(xiàn)狀,就是它們破壞了軟件包間的依賴關(guān)系】,詳情解法可參見這篇文章:
http://www.cnblogs.com/LeoGodfrey/p/3316834.html - 如果出現(xiàn) “Try increasing heap size with java option '-Xmx<size>'” 這種error
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g" // 調(diào)大heap size
./prebuilts/sdk/tools/jack-admin kill-server // 重啟jack-admin
./prebuilts/sdk/tools/jack-admin start-server
參考文章:
http://stackoverflow.com/questions/35579646/android-source-code-compile-error-try-increasing-heap-size-with-java-option - error: subcommand無法執(zhí)行或者執(zhí)行失敗 或者 /bin/bash: xmllint: 未找到命令
原因:缺少特定的庫
解決方法:把能下的庫都下完,一般差不多就能過了。
差不多有如下這些庫會(huì)用到
sudo apt install git-core bison build-essential curl flex git gnupg gperf libesd0-dev liblz4-tool libncurses5-dev libsdl-dev
libwxgtk3.0-dev libxml2 libxml2-utils lzop maven openjdk-7-jdk pngcrush schedtool squashfs-tools xsltproc zip g++-multilib
gcc-multilib lib32ncurses5-dev lib32readline6-dev libc6-dev-i386 x11proto-core-dev libx11-dev ccache libgl1-mesa-dev unzip
python-imaging lib32z1-dev
相關(guān)參考文章:
- https://source.android.com/setup/build/downloading.html(Android官方下載源碼介紹)
- http://gityuan.com/2016/03/19/android-build/ (這篇講解Android編譯系統(tǒng), 講得非常清楚)
- http://elinux.org/Android_Build_System (英文版的也不錯(cuò), 可以對(duì)照看看)
- http://source.android.com/source/requirements.html (Android官方下載源碼系統(tǒng)環(huán)境介紹)