前言:
一直想要編譯一下Android 源碼,之前去google 看,下載要下載repo。
當時很懵逼,repo 是個什么?(repo 是一個python 腳本,因為Android 源碼git 倉庫太多,幫助管理git 倉庫的,你不需要知道里面什么實現。會用就行。)
然后往下看,看到網上說,編譯Android AOSP 只能用Ubuntu,當時自己的心也哇哇涼啊。
自己只有一個工作的電腦,自己的電腦配置太低。 看網上說,還要200G 的空間。(有一個攔路虎)
之前自己給自己的windos 安裝過雙系統,但是好像一直啟動不了。現在想想,應該是因為當時沒有設置引導盤的順序吧。現在我用一個usb 連接了一個1T的硬盤,里面安裝了Ubentu . 如果我插了這個硬盤,啟動的就是ubuntu, 如果沒有,就是工作的windows 。工作學習兩不誤。
不知道什么動力讓我突破了所有上面的攔路虎,依然決然去編譯源碼。面試的時候,別人提到過,我說我想,但是沒有做到,別人說,那么你為了編譯源碼,你第一步應該怎么做? 我說首先下載源碼,安裝Ubuntu。終於一步一步走過來了。感謝面試的那個人給我指點,讓我思考。
一路走來,少些艱辛。下載代碼用了兩天,還是下載的清華鏡像的,公司的網太慢了,43G 下載了兩天,然后正好周末,編譯成功花費了兩天周末時間,但是還是很值得的。里面有很多坑,給大家分享下。
下載AOSP:
google 的AOSP 的話,因為翻牆和數據量太大,考慮國內的鏡像。
https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/ 這個網址是清華大學的鏡像站。
由於首次同步需要下載約 30GB 數據,過程中任何網絡故障都可能造成同步失敗,我們強烈建議您使用初始化包進行初始化。也就是我們直接下載一個git倉庫。這樣的話,不會說,文件太大,git clone 的時候老是中斷。
下載 https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar,
下載完成后記得根據 checksum.txt 的內容校驗一下。
當時我是用Windows 迅雷下載的文件。
當然如果你直接是Ubuntu 系統,也可以用下面的方法:
wget -c https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下載初始化包
tar xf aosp-latest.tar
cd AOSP # 解壓得到的 AOSP 工程目錄
# 這時 ls 的話什么也看不到,因為只有一個隱藏的 .repo 目錄
repo sync # 正常同步一遍即可得到完整目錄
# 或 repo sync -l 僅checkout代碼
安裝Ubentu
一定要安裝Ubentu ,這個可以讓你少走一些坑。
之前我用迅雷下載了清華大學的AOSP,是tar 文件,我用Windows 解壓之后,當時的想法很簡單,就是只想簡單的看下源碼先。當時解壓之后,執行python repo sync
,報了亂七八糟的錯。我還調試了repo 的源碼。花了不少時間。
后來發現config 文件,解壓之后和壓縮包里面的不一樣,我就知道了,可能tar 文件,Windows 解壓之后,會有問題。
果斷去安裝Ubuntu。
安裝之后,把下載的文件copy 到 Ubentu 的/usr/lib/下面。(當然你可以隨便copy 到任何地方)
由於太大,該過程大約十多分鍾。
copy 完成之后,解壓縮:
tar -xvf ***.tar -C /usr/lib/AOSP
這也是一個漫長的過程,大概半個多小時。畢竟40G的代碼。
安裝Python 2.7,git(如果有,請忽略)
因為repo 是python 腳本工具,你的Ubuntu需要Python 運行環境。Python 3.5 可能會有問題,最好安裝python 2.7
sudo apt-install python
sudo add-apt-repository ppa:git-core/ppa //添加源
sudo apt-get update //更新
sudo apt-get install git //自動安裝git
git --version //確認git版本
//第一次使用前
git config --global user.name "your name" //設置用戶名,注意git config命令的--global參數,用了這個參數,表示你這台機器上所有的Git倉庫都會使用這個配置,當然也可以對某個倉庫指定不同的用戶名和Email地址。
git config --global user.email "your email" //設置電子郵箱
git config user.name //查看用戶名
git config user.email //查看電子郵箱
初始化倉庫:
解壓縮完成之后,因為.repo 是隱藏文件夾,所以,需要ls -a 才能看到:
執行以下命令,同步以下代碼。
cd .repo
python repo sync
中間可能執行失敗,注意,你的網絡一定要ok。可以設置代理,但是要保證有代理的情況下,可以訪問清華鏡像的網站。
但是最好不要設置代理。我當時就浪費了很多時間。
如果執行python repo sync
失敗了,那么在執行一次。多執行幾次。同時排除網絡原因。
成功之后,切換到AOSP 目錄:
也就是.repo 父目錄。
執行:
source build/envsetup.sh
注意:source 是 一個系統命令。
執行之后,可能沒有任何的提示。那么就是成功了。
當然有的電腦可能會有其他提示。都一樣。
如果出現source: not found錯誤
運行 ls -l /bin/sh
后顯示/bin/sh -> dash
這說明是用dash來進行解析的。
解決方案:
命令行執行:dpkg-reconfigure dash(需要root權限)
在界面中選擇no
再運行ls -l /bin/sh 后顯示/bin/sh -> bash
執行source build/envsetup.sh
不報錯,即OK。
補充一點 :
編譯環境初始化(即執行source build/envsetup.sh)之后,我們可以得到一些有用的指令,除了上邊用到的lunch,還有以下:
- 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.
其中mmm指令就是用來編譯指定目錄.通常來說,每個目錄只包含一個模塊.比如這里我們要編譯Setting 模塊,執行指令:
mmm packages/apps/Settings/
接着執行lunch
lunch
選擇編譯的目標版本:
如果你的電腦是64位的話,選擇27,因為x86版本的虛擬機運行比較快。不要編譯arm 版本的了,我當時編譯過,但是運行起來太慢,而且運行不起來。浪費了一晚上。
編譯之前:
編譯之前一定要保證一些庫安裝了,不然編譯了幾個小時之后報錯,某個庫沒有安裝,豈不是很難受?
//防止出現/bin/bash:m4:command not found
sudo apt-get install m4
///防止出現bin/bash: xmllint: command not found
sudo apt-get install libxml2-utils
編譯
make -j8
如果遇到了下面的錯誤:
/usr/lib/aosp_dir/aosp/frameworks/base/media/java/android/media/MediaDrm.java:1207: warning: Returning unexpected constant SECURITY_LEVEL_MAX; is @SecurityLevel missing this constant? Expected one of SECURITY_LEVEL_UNKNOWN, SECURITY_LEVEL_SW_SECURE_CRYPTO, SECURITY_LEVEL_SW_SECURE_DECODE, SECURITY_LEVEL_HW_SECURE_CRYPTO, SECURITY_LEVEL_HW_SECURE_DECODE, SECURITY_LEVEL_HW_SECURE_ALL [ReturningUnexpectedConstant]
15:58:20 ninja failed with: exit status 1
在執行一次make -j8
如果遇到下面的錯誤:
如果你提示了no such file /…/.lunchrc 出現只能說明之前你沒有執行source build/envsetup.sh
:
最終編譯成功:
Unsparsing /media/caoxinyu/TomasYu/out/target/product/generic_x86_64/system.img
[100% 8408/8408] Creating /media/caoxinyu/TomasYu/out/target/product/generic_x86_64/VerifiedBootParams.textproto
#### build completed successfully (37:36 (mm:ss)) ####
編譯success 之后:
啟動模擬器
emulator
如果遇到一下錯誤:
emulator: ERROR: x86_64 emulation currently requires hardware acceleration!
Please ensure KVM is properly installed and usable.
CPU acceleration status: KVM is not installed on this machine (/dev/kvm is missing).
檢查你的電腦支不支持虛擬機
egrep -c '(vmx|svm)' /proc/cpuinfo
如果顯示0表示不支持,如果是1,以以上,表示支持。
然后去boss 里面啟用虛擬機。
Open Virtual machine setting.
Go to the processor.
Check the virtualize Intel VT-x/EPT or AMD-RVI option.
Click ok and run the Virtual machine, it should work fine with you.
之后執行下面命令:
sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils
sudo adduser `id -un` libvirtd
sudo adduser `id -un` kv
如果遇到這個錯誤:
emulator: ERROR: x86 emulation currently requires hardware acceleration!
Please ensure KVM is properly installed and usable.
CPU acceleration status: This user doesn't have permissions to use KVM (/dev/kvm).
或者提示
emulator: ERROR: x86 emulation currently requires hardware acceleration!
Please ensure KVM is properly installed and usable.
CPU acceleration status: Could not open /dev/kvm :Permission denied
使用 root 權限執行以下三條命令后注銷當前用戶或者直接重啟電腦即可
(本例子中當前登錄圖形界面的用戶是 sman)
groupadd kvm
usermod -G kvm -a sman
echo 'KERNEL=="kvm",GROUP="kvm",MODE="0660"' >> /etc/udev/rules.d/androidUseKVM.rules
這三條命令的作用分別是:
#創建用戶組kvm
#將用戶sman添加到kvm用戶組
#開機時自動賦予用戶組kvm擁有0660的權限
如果第三條執行錯誤,沒有關系。
繼續執行emulator
如果報錯:
grant current user access to /dev/kvm 以及/dev/kvm devices: permission denies
解決方法:執行 sudo chown username -R /dev/kvm 注意username是你用的用戶名
繼續執行emulator
總結:
1.編譯源碼其實沒有那么難,只是你要付出時間去做這件事情。
2.遇到問題,百度、google解決問題。畢竟代碼都是別人上傳的沒有問題的,你只是編譯。
可能遇到的錯誤:
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000090e00000, 283115520, 0) failed; error='Not enough space' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 283115520 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /usr/lib/aosp_dir/aosp/hs_err_pid425.log
[ 98% 111020/112583] //frameworks/base/services:services d8 [common]
原因:內存不夠,可能是你開的線程太多了。比如make -j20 這種。還有一種原因是可能沒有設置交換區。
設置交換區參考:
https://blog.csdn.net/qq_35976351/article/details/79363760
感謝:
AOSP 編譯
https://blog.csdn.net/fnhfire_7030/article/details/79074415
安裝搜狗輸入法
https://blog.csdn.net/fx_yzjy101/article/details/80243710
安裝git
https://blog.csdn.net/m0_37950361/article/details/80138929
ubuntu系統shell 中source: not found錯誤
https://blog.csdn.net/buynow123/article/details/51774018
/bin/bash: m4: command not found
https://blog.csdn.net/yao2huan/article/details/78841683
CPU acceleration status: KVM is not installed on this machine (/dev/kvm is missing).
https://blog.csdn.net/fengxingzhe001/article/details/78060126
AOSP 編譯
https://blog.csdn.net/wuxiaolongtongxue/article/details/85633344
CPU acceleration status: This user doesn’t have permissions to use KVM (/dev/kvm).
http://www.cnblogs.com/howdop/p/5347729.html
grant current user access to /dev/kvm 以及/dev/kvm devices: permission denies