Android10源碼AOSP(從Ubuntu14.04環境搭建到下載編譯運行)

Ubuntu14.04環境搭建

電腦爲筆記本,CPU i7-10750H,16G內存

虛擬機使用VMware Workstation 15 Pro

操作系統爲Ubuntu 64 位 14.04

AOSP比較大,安裝虛擬機的時候預留250G+的空間,8G內存。具體的安裝請自行進行。

根據你要編譯的系統版本,選擇相應的 Ubuntu版本:

  • Android 6.0 (Marshmallow) – AOSP master:Ubuntu 14.04 (Trusty)
  • Android 2.3.x (Gingerbread) – Android 5.x (Lollipop):Ubuntu 12.04 (Precise)
  • Android 1.5 (Cupcake) – Android 2.2.x (Froyo):Ubuntu 10.04 (Lucid)

Ubuntu 環境準備

  • 設置root密碼:sudo passwd

  • 安裝VMware Tools,方便與window交互
    sudo apt-get install open-vm-tools-desktop -y
    然後重啓
    sudo reboot

  • 替換國內源,見“鏡像替換國內阿里”,apt-get intstall 會快很多
    重啓,然後
    sudo apt-get update

  • 安裝編譯源碼時候的第三方庫(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 libgl1-mesa-dev libxml2-utils xsltproc unzip

    注意:如需使用 SELinux 工具進行政策分析cank,您還需要安裝 python-networkx 軟件包。
    注意:如果您使用的是 LDAP 並且希望運行 ART 主機測試,還需要安裝 libnss-sss:i386 軟件包。

  • 安裝adb sudo apt-get install android-tools-adb

  • python 安裝 (推薦採用3.6.8,使用3.7.X的版本存在import ssl的錯誤,理論上3.6+即可)

    參考“python3.6.8 安裝”

  • 分區工具,可以給虛擬機多分配些內存。現在系統安裝號默認是12G的物理內存加8G虛擬內存。在編譯過程中發現內存不夠用(出現堆溢出,內存溢出),增加了6G虛擬內存,總共12G+14G內存。(推測和make -j8相關,如果內存不夠的話j後面的數字小些)
    sudo apt-get install gparted

鏡像替換國內阿里

用你熟悉的編輯器打開:

/etc/apt/sources.list

例如:sudo gedit /etc/apt/sources.list

替換默認的

http://archive.ubuntu.com/

https://mirrors.aliyun.com/

以Ubuntu 14.04.5 LTS爲例,最後的效果如下:

deb https://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

## Not recommended
# deb https://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiv

Python3.8.3 安裝

安裝SSL:

apt-get install openssl
apt-get install libssl-dev

安裝Python3.6

wget https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tgz
tar -zxvf Python-3.8.3.tgz
cd Python-3.8.3

修改Moudles/Setup (該目錄在Python-3.6.8目錄下),把以下行的註釋去掉

SSL=/usr/local/ssl
_ssl _ssl.c \
        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
        -L$(SSL)/lib64 -lssl -lcrypto

編譯安裝

./configure
make && make install

切換版本,使用該方式比較簡單:

查看python版本的優先級(初次沒有,報錯的話正常)

sudo update-alternatives --config python

以通過update-alternatives來設置默認python版本, 最後的參數1,2是優先級,數字越大優先級越高,比如經過如下設置後,在終端輸入python,顯示的就是3.6的版本了。

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.8 3

注意:自行編譯安裝的Python是在/usr/local/bin/目錄下

Android源碼下載更新

Android 源代碼樹位於由 Google 託管的 Git 代碼庫中。Git 代碼庫中包含 Android 源代碼的元數據,其中包括與對源代碼進行的更改以及更改日期相關的元數據。下面介紹瞭如何下載特定 Android 代碼源代碼樹。

安裝 Repo

Repo 是一款工具,可讓您在 Android 環境中更輕鬆地使用 Git。要安裝 Repo,請執行以下操作:

mkdir ~/bin # 創建文件夾
PATH=~/bin:$PATH #設置環境變量
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo #下載repro 到/bin/repo文件裏
chmod a+x ~/bin/repo # 給repo 文件權限

更新 Repo

repo的運行過程中會嘗試訪問官方的git源更新自己,如果想使用tuna的鏡像源進行更新,可以將如下內容複製到你的~/.bashrc

export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'

source ~/.bashrc

替換國內清華的鏡像

用第三方工具打開repo文件,替換國內清華的鏡像

https://android.googlesource.com/ 全部使用 https://aosp.tuna.tsinghua.edu.cn/ 代替即可。

由於使用 HTTPS 協議更安全,並且更便於靈活處理,所以強烈推薦使用 HTTPS 協議同步 AOSP 鏡像。

由於 AOSP 鏡像造成CPU/內存負載過重,限制了併發數量,因此建議:

  1. sync的時候併發數不宜太高,否則會出現 503 錯誤,即-j後面的數字不能太大,建議選擇4。
  2. 請儘量選擇流量較小時錯峯同步。

使用每月更新的初始化包(推薦使用該方法)

由於首次同步需要下載約 30GB 數據,過程中任何網絡故障都可能造成同步失敗,強烈建議使用初始化包進行初始化。

下載 https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar (可以使用三方工具下載,支持斷點續傳),下載完成後記得根據 checksum.txt 的內容校驗一下。

由於所有代碼都是從隱藏的 .repo 目錄中 checkout 出來的,所以我們只保留了 .repo 目錄,下載後解壓 再 repo sync 一遍即可得到完整的目錄。

使用方法如下:

wget -c https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下載初始化包
tar xf aosp-latest.tar
cd AOSP   # 解壓得到的 AOSP 工程目錄
# 這時 ls 的話什麼也看不到,因爲只有一個隱藏的 .repo 目錄 ,可以使用ls -al
repo sync # 正常同步一遍即可得到完整目錄
# 或 repo sync -l 僅checkout代碼

此後,每次只需運行 repo sync 即可保持同步。

可以選擇該命令同時發起四個併發請求,之所以選擇4是因爲清華的鏡像的併發請求的限制的上限就是4個。

repo sync -j4

注意:出現奇奇怪怪的bug,可以重複執行一下,很多bug是由網絡原因造成的。

一定要確保文件都下載成功,否則編譯時會出現一些問題。

(強烈多同步幾次,確保同步沒有錯誤。在第一次同步完成後再進行一次同步,如果第一次沒問題的話速度會很快的)

也可以寫個腳本,自動執行

#!/bin/bash 
repo sync -j4
while [ $? = 1 ]; do 
echo "================sync failed, re-sync again =====" 
sleep 3 
repo sync
	done

傳統初始化方法(不推薦)

建立工作目錄:

mkdir WORKING_DIRECTORY
cd WORKING_DIRECTORY

初始化倉庫:

repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest

如果提示無法連接到 gerrit.googlesource.com,請參照“更新 Repo”。

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

repo sync

切換版本

如果需要某個特定的 Android 版本:

repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-10.0.0_r10

默認是master,是android11版,我選擇了Android10。我使用Android10

查看版本:cd .repo/manifests && git branch -a

Android 編譯

設置文件描述符限制

先用$ ulimit查看,如果是無限制就不需要修改

可能默認限制的同時打開的文件數量很少,不能滿足編譯過程中的高併發需要,因此需要在shell中運行命令:

$ ulimit -S -n 2048

避免OutOfMemoryError(內存足夠的話不會出現)

Android10之前的解決方案:OutOfMemoryError: Java heap space解決方案

export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m"

Android10以後:

export _JAVA_OPTIONS="-Xmx4096m"

或者增加虛擬機內存或是swp空間

環境設置

在源碼根目錄下調用下面的命令:

$ source build/envsetup.sh

選擇設備

在命令行輸入下面的命令選擇打算編譯的源碼類型

Lunch menu... pick a combo:
     1. aosp_arm-eng
     2. aosp_arm64-eng
     3. aosp_blueline-userdebug
     4. aosp_bonito-userdebug
     5. aosp_car_arm-userdebug
     6. aosp_car_arm64-userdebug
     7. aosp_car_x86-userdebug
     8. aosp_car_x86_64-userdebug
     9. aosp_cf_arm64_phone-userdebug
     10. aosp_cf_x86_64_phone-userdebug
     11. aosp_cf_x86_auto-userdebug
     12. aosp_cf_x86_phone-userdebug
     13. aosp_cf_x86_tv-userdebug
     14. aosp_crosshatch-userdebug
     15. aosp_marlin-userdebug
     16. aosp_sailfish-userdebug
     17. aosp_sargo-userdebug
     18. aosp_taimen-userdebug
     19. aosp_walleye-userdebug
     20. aosp_walleye_test-userdebug
     21. aosp_x86-eng
     22. aosp_x86_64-eng
     23. beagle_x15-userdebug
     24. fuchsia_arm64-eng
     25. fuchsia_x86_64-eng
     26. hikey-userdebug
     27. hikey64_only-userdebug
     28. hikey960-userdebug
     29. hikey960_tv-userdebug
     30. hikey_tv-userdebug
     31. m_e_arm-userdebug
     32. mini_emulator_arm64-userdebug
     33. mini_emulator_x86-userdebug
     34. mini_emulator_x86_64-userdebug
     35. poplar-eng
     36. poplar-user
     37. poplar-userdebug
     38. qemu_trusty_arm64-userdebug
     39. uml-userdebug

Which would you like? [aosp_arm-eng] 21

根據後綴可以判斷出使用的場景如下:

類型 用途
user 權限少,用於刷機使用
userdebug 和“user”類似,但可以root,並且可以調試
eng 具有開發配置,並且有額外的調試工具

根據需要選擇對應的類型,比如我選擇“21”。(推薦採用x86的,模擬器速度快一些)

============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=10
TARGET_PRODUCT=aosp_x86
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_ARCH=x86
TARGET_ARCH_VARIANT=x86
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.4.0-148-generic-x86_64-Ubuntu-14.04.6-LTS
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=QP1A.191105.003
OUT_DIR=out
============================================

開始編譯

清理msk

make clobber

爲了加快編譯的速度,最好併發來編譯(併發數不建議太高,可能會造成內存不足,磁盤不足等問題)

make -j8

編譯結束以後,會顯示下面的日誌:

#### build completed successfully (02:00:35 (hh:mm:ss)) ####

錯誤FAILED: system-qemu.img

============================================
wildcard(out/target/product/generic_x86/clean_steps.mk) was changed, regenerating...
[ 99% 11311/11312] Create system-qemu.img now
FAILED: out/target/product/generic_x86/system-qemu.img
/bin/bash -c "(export SGDISK=out/host/linux-x86/bin/sgdisk SIMG2IMG=out/host/linux-x86/bin/simg2img;      device/generic/goldfish/tools/mk_combined_img.py -i out/target/product/generic_x86/system-qemu-config.txt -o out/target/product/generic_x86/system-qemu.img)"
  File "device/generic/goldfish/tools/mk_combined_img.py", line 48
    print "'%s' cannot be converted to int" % (line[2])
          ^
SyntaxError: invalid syntax

若出現該錯誤,切換python2.7:sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 10

啓動模擬器

emulator

KVM錯誤

emulator: ERROR: x86 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

先使用該指令查看是否支持虛擬化,如果不支持的話在虛擬機的CPU選項中打開即可。

sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils
sudo adduser `id -un` libvirtd
sudo adduser `id -un` kvm

#檢查是否安裝成功
sudo kvm-ok

模擬器黑屏

emulator -partition-size 4096 -kernel ./prebuilts/qemu-kernel/x86/4.9/kernel-qemu2

通過使用kernel-qemu-armv7內核 解決模擬器等待黑屏問題.而-partition-size 4096則是解決警告: system partion siez adjusted to match image file (3083 MB > 800 MB)

模擬器啓動後崩潰

VMware: vmw_ioctl_command error Invalid argument.
Aborted (core dumped)

關閉硬件加速

export SVGA_VGPU10=0
echo "export SVGA_VGPU10=0" >> ~/.bashrc
source ~/.bashrc

Android Studio查看源碼

編譯源碼idegen模塊

mmm development/tools/idegen/

這行命令的意思是編譯idegen這個模塊項目,然後生成idegen.jar文件。

編譯結束以後,會顯示下面的日誌:

[100% 11109/11109] Install: out/host/linux-x86/framework/idegen.jar

#### build completed successfully (01:26 (mm:ss)) ####

mmm指令就是用來編譯指定目錄。通常來說,每個目錄只包含一個模塊。

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

生成AS配置文件

接着執行如下腳本:

development/tools/idegen/idegen.sh

這行命令的意思是在根目錄生成對應的android.ipr、android.iml IEDA工程配置文件。

等待片刻得到類似如下信息說明OK:

Read excludes: 16ms
Traversed tree: 61259ms

警告1:

emulator: WARNING: Couldn't find crash service executable /Volumes/android/aosp/prebuilts/android-emulator/darwin-x86_64/emulator64-crash-service

這個警告一般沒影響,是模擬器崩潰時進行處理的程序,對使用者意義不是很大。

警告2:

emulator: WARNING: system partition size adjusted to match image file (3083 MB > 800 MB)

調整系統分區大小以匹配圖像文件,執行命令:emulator -partition-size 4096。(可以不管)

推薦採用x86的,模擬器速度快一些。我這個機器啓動arm架構的模擬器花費很長時間。

導入源碼

啓動Android Studio,然後選擇打開一個已存在的Android Studio工程,選擇源碼根目錄的android.ipr,經過的加載過程以後,Android 源碼就已經成功的加載到了Android Studio中。

OK,至此我們就完成了下載AOSP並編譯導入Android Studio的完整過程。

參考:

1.https://source.android.google.cn/setup/start

2.https://blog.csdn.net/baidu_32237719/article/details/105715371

歡迎關注我的公衆號,持續分析優質技術文章
歡迎關注我的公衆號

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章