AOSP學(xué)習(xí)(一)Ubuntu編譯Android13系統(tǒng)源碼

前言

做Android應(yīng)用好多年,一直對AOSP無所知,都不知道什么東西,目前車載那么火,就想介入進(jìn)去,開始嘗試去編譯一下系統(tǒng)源碼,然后去看了一下Android官網(wǎng),目前最新的版本還不支持mac和window,只支持ubuntu,去整了個(gè)硬盤準(zhǔn)備刷個(gè)系統(tǒng),可能是Macmini原因,一直沒能成功,很尷尬,最后整了虛擬機(jī)。

整個(gè)過程挺麻煩的,我下載是Android13最新的源碼,因?yàn)樵创a需要翻墻,我用清華的鏡像,下載倒是挺快,沒有像別人說的兩三天,但也有半天,編譯也用了半天,過程有很多很多坑,記錄下來給大家分享一下,一起學(xué)習(xí)。

一、構(gòu)建環(huán)境搭建

1.1、準(zhǔn)備工作

VMWare:https://www.vmware.com/products/fusion/fusion-evaluation.html
Ubuntu鏡像:http://mirrors.aliyun.com/ubuntu-releases/

1.2、VMWare安裝Ubuntu

1.2.1、我的電腦是Mac,所以下載的是VMWare Fusion虛擬機(jī),Android官網(wǎng)建議ubuntu版本選擇18.0.4,給的內(nèi)存大小盡量給大一些,我是16G,給了12G,磁盤大小給了500G

1675172744307.jpg

1.2.2、如果是Mac的話,給了一個(gè)新的硬盤,格式化的話選擇區(qū)分大小寫,不然Androidstudo去跑源碼會莫名的報(bào)錯(cuò)

1675173592846.jpg

1.3、下載git與Python3

1.3.1、git安裝與下載

安裝git:

    sudo apt install git

安裝依賴工具:

sudo apt install git-core libssl-dev  libffi-dev gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev libz-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip

設(shè)置git身份,添加自己的郵箱和姓名:

git config --global user.email "zhangxiaoxiao@qq.com"
git config --global user.name "zhangxiaoxiao"

1.3.2、Python3的安裝與切換

下載Python3:

sudo apt install python3

配置update-alternatives,用于切換當(dāng)前使用的python版本,需要設(shè)置軟鏈:

sudo update-alternatives --install  /usr/bin/python python /usr/bin/python2.7(python2安裝地址)  2(權(quán)重號)
sudo update-alternatives --install  /usr/bin/python python /usr/bin/python3.7(python3的安裝地址)  3(權(quán)重號)

切換Python版本:

sudo update-alternatives --config python
1675174640671.jpg

二、源碼下載與編譯

AOSP(Android Open Source Project)是Google開放的Android 開源項(xiàng)目,中文官網(wǎng)為:https://source.android.google.cn/ AOSP通俗來講就是一個(gè)Android系統(tǒng)源碼項(xiàng)目,通過它可以定制 Android 操作系統(tǒng),國內(nèi)手機(jī)廠商都是在此基礎(chǔ)上開發(fā)的定制系統(tǒng)。因?yàn)橐獕?,如果無法連接Google服務(wù)器獲取AOSP源碼,可以從 清華大學(xué)鏡像站或者 中科大鏡像。我用的是中科的例子

2.1、下載 repo 工具

創(chuàng)建bin,并加入到PATH中:

mkdir ~/bin
PATH=~/bin:$PATH

安裝curl庫:

sudo apt-get install curl

下載repo并設(shè)置權(quán)限

curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
## 如果上述 URL 不可訪問,可以用下面的:
## curl -sSL  'https://gerrit-googlesource.proxy.ustclug.org/git-repo/+/master/repo?format=TEXT' |base64 -d > ~/bin/repo
chmod a+x ~/bin/repo

2.2、下載源碼

然后建立一個(gè)工作目錄(名字任意)

mkdir aosp
cd aosp

初始化倉庫:

repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest
## 如果提示無法連接到 gerrit.googlesource.com,可以編輯 ~/bin/repo,把 REPO_URL 一行替換成下面的:
## REPO_URL = 'https://gerrit-googlesource.proxy.ustclug.org/git-repo'

1675178768113.jpg

如果需要某個(gè)特定的 Android 版本(Android 版本列表 ):

repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest -b android-13.0.0_r24

同步源碼樹(以后只需執(zhí)行這條命令來同步):

repo sync

等待漫長的好幾個(gè)小時(shí),終于完成,成功會有這樣提示

WechatIMG321.png

2.3、安裝構(gòu)建環(huán)境

安裝 jdk8

sudo apt-get update
sudo apt-get install openjdk-8-jdk

安裝依賴:

sudo apt-get install libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev g++-multilib
sudo apt-get install -y git flex bison gperf build-essential libncurses5-dev:i386
sudo apt-get install tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386
sudo apt-get install dpkg-dev libsdl1.2-dev libesd0-dev
sudo apt-get install git-core gnupg flex bison gperf build-essential
sudo apt-get install zip curl zlib1g-dev gcc-multilib g++-multilib
sudo apt-get install libc6-dev-i386
sudo apt-get install lib32ncurses5-dev x11proto-core-dev libx11-dev
sudo apt-get install libgl1-mesa-dev libxml2-utils xsltproc unzip m4
sudo apt-get install lib32z-dev ccache
sudo apt-get install libssl-dev

安裝 sudo apt-get install libesd0-dev 會報(bào) Unable to locate package libesd0-dev 這個(gè)錯(cuò),解決辦法

sudo gedit /etc/apt/sources.list  //在行尾添加如下兩行的內(nèi)容
deb http://us.archive.ubuntu.com/ubuntu/ xenial main universe
deb-src http://us.archive.ubuntu.com/ubuntu/ xenial main universe
1675180175543.jpg

2.4、編譯

使用envsetup.sh腳本初始化環(huán)境:

source build/envsetup.sh

選擇用lunch構(gòu)建哪個(gè)目標(biāo),根據(jù)設(shè)備選擇需要的目標(biāo),該設(shè)備可以是google官方提供的,根據(jù)google提供的驅(qū)動(dòng)地址下載:https://developers.google.cn/android/drivers或者可以用電腦模擬器“sdk_phone_x86_64”,我這邊沒有設(shè)備使用的是電腦模擬器

lunch sdk_phone_x86_64

構(gòu)建代碼

m j8(8等于核數(shù)*2)

其他指令說明

- 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.
- clean - m clean 會刪除此配置的所有輸出和中間文件。此內(nèi)容與 rm -rf out/ 相同
其中mmm指令就是用來編譯指定目錄.通常來說,每個(gè)目錄只包含一個(gè)模塊.比如這里我們要編譯Setting 模塊,執(zhí)行指令:
mmm packages/apps/Settings/
WechatIMG322.png

啟動(dòng)模擬器

emulator
WechatIMG482.png

模擬器關(guān)閉時(shí)候,其實(shí)在后臺還存在,需要自行關(guān)閉

pkill qemu

三、自定義ROM真機(jī)刷機(jī)

USB 調(diào)試配置:(設(shè)備USB能連上電腦虛擬機(jī)則不用管,如果不是用虛擬機(jī)能連上電腦也忽略)https://developer.android.google.cn/studio/run/device.html#setting-up

驅(qū)動(dòng)下載:https://developers.google.cn/android/drivers#angleropr6.170623.019

1675256709006.jpg

下載完對應(yīng)的驅(qū)動(dòng),并將壓縮包放入已經(jīng)下載好的源碼根目錄,解壓后得到兩個(gè)腳本文件,從源代碼樹的根目錄運(yùn)行附帶的自解壓腳本,然后確認(rèn)您同意附帶的許可協(xié)議的條款。二進(jìn)制文件及其對應(yīng)的 makefile 將會安裝在源代碼樹的 vendor/ 層次結(jié)構(gòu)中

# 我的目錄/home/zhangxiaoxiao/aosp
extract-qcom-barbet.sh
extract-google_devices-barbet.sh
1675258441080.jpg

為了確保新安裝的二進(jìn)制文件在解壓后能被有效采用,請使用以下命令刪除所有之前 build 的已有輸出:

make clobber

進(jìn)入Bootloader模式:(解BL鎖)

adb reboot bootloader

查看連接設(shè)備:

fastboot devices

刷機(jī):

fastboot flashall -w

四、過程報(bào)錯(cuò)收集

無法安裝libesd0-dev

解決辦法:
sudo vim /etc/apt/sources.list  //在行尾添加如下兩行的內(nèi)容

deb http://us.archive.ubuntu.com/ubuntu/ xenial main universe
deb-src http://us.archive.ubuntu.com/ubuntu/ xenial main universe

更新軟件源并重新安裝:
sudo apt-get update && sudo apt-get install libesd0-dev

內(nèi)存不夠,新版本要求16G,可能是你開的線程太多了。比如make -j8 這種。還有一種原因是可能沒有設(shè)置swap交換區(qū),就是把磁盤空間當(dāng)作臨時(shí)內(nèi)存,注意的點(diǎn)是,設(shè)置完成需要重啟虛擬機(jī)或電腦。

//查看內(nèi)存大小
free -m
//創(chuàng)建交換分區(qū)的文件:增加16G大小的交換分區(qū),則命令寫法如下,其中的 count 等于想要的塊大小
dd if=/dev/zero of=/home/swapfile bs=1M count=16384
//設(shè)置交換分區(qū)文件,建立swap的文件系統(tǒng)
mkswap /home/swapfile
//立即啟用交換分區(qū)文件
swapon /home/swapfile

//如果要使系統(tǒng)開機(jī)時(shí)自啟用,要在文件/etc/fstab中添加一行
/home/swapfile swap swap defaults 0 0

KVM的虛擬化需要硬件支持報(bào)錯(cuò)

//報(bào)錯(cuò)信息
INFO    | Android emulator version 31.3.9.0 (build_id 9070145) (CL:N/A)
INFO    | Storing crashdata in: /tmp/android-zhangxiaoxiao/emu-crash.db, detection is enabled
INFO    | Duplicate loglines will be removed, if you wish to see each indiviudal line launch with the -log-nofilter flag.
ERROR   | x86_64 emulation currently requires hardware acceleration!
CPU acceleration status: KVM requires a CPU that supports vmx or svm

//安裝kvm解決辦法參考如下
https://help.ubuntu.com/community/KVM/Installation
//模擬器參考
https://blog.csdn.net/mvp_Dawn/article/details/107678057

make完不能直接emulator啟動(dòng)電腦模擬器,會報(bào)錯(cuò),lunch aosp_x86-eng這個(gè)版本不行,Android12改掉了會報(bào)錯(cuò)

ERROR   | Failed to create Vulkan instance.
qemu-system-x86_64: Could not open '/home/zhangxiaoxiao/aosp/out/target/product/generic_x86_64/userdata-qemu.img': No such file or directory

//解決辦法
lunch sdk_phone_x86_64

感謝

http://www.itdecent.cn/p/6dee223ac93e
https://blog.csdn.net/kfyzjd2008/article/details/124267159
http://www.itdecent.cn/p/53941de91c77
https://www.pudn.com/news/62599ce6be9ad24cfab6def3.html
https://www.cnblogs.com/caoxinyu/p/10568480.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容