1. 程式人生 > 其它 >Android7.1.2 原始碼編譯並燒寫Nexus5X

Android7.1.2 原始碼編譯並燒寫Nexus5X

1、環境配置

  • Ubuntu18.04
  • openJDK1.8
  • Python2.7

2、更新Ubuntu映象源

編輯/etc/apt/source.list,替換為以下內容

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

# deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

然後執行

sudo apt-get udpate

3、安裝相關依賴

3.1 安裝Java

sudo apt-get install openjdk-8-jdk

3.2 安裝Python

sudo apt-get install python2.7

3.3 安裝其它依賴

sudo apt-get install git-core gnupg flex bison build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 libncurses5 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig

參考:
構建依賴
各個Ubuntu版本需要安裝的包

4、配置Git

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

5、下載Repo

5.1 配置Repo下載路徑

編輯你的~/.bashrc檔案,追加如下內容,表示將從該地址下載Repo工具

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

然後重啟終端。

5.2 下載Repo

mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

參考:
Repo初始化流程
Repo下載地址配置
這些命令執行完成將在當前使用者的家目錄生成一個bin目錄,該目錄下面放著一個repo可執行檔案。它便是Repo工具,其實就是一個Python指令碼。同時將該指令碼臨時加入了PATH環境變數,如果你想將其永久加入環境變數,可以修改/etc/profile檔案。

6、選擇原始碼版本

這一步很關鍵,需要找到支援你的裝置的原始碼版本,還要考慮該版本是否有對應版本的驅動檔案。我的裝置是Nexus 5x,想編譯Android7.1.2,所以選擇這個版本的原始碼:

N2G47Z這個Build id可以用於後面選擇驅動檔案。

參考:
原始碼版本
驅動檔案版本

7、下載原始碼

7.1 下載manifest倉庫

repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-7.1.2_r18

這條指令執行後,首先會在當前目錄生成一個.repo目錄,然後下載一個叫manifests的git倉庫,該倉庫會記錄當前分支的Android原始碼都需要下載哪些倉庫及其下載地址,分支資訊等(Android原始碼是由多個倉庫組成的)。
repo init通過-b指定分支,如果不指定,那就是主分支。

7.2 開始下載原始碼

執行repo sync便開始下載原始碼

repo sync

8、編譯原始碼

下載完成後,程式碼會被自動checkout出來,然後執行如下命令進行編譯

source build/envsetup.sh
lunch aosp_bullhead-userdebug
make -j 16

我的裝置是Nexus5x,所以選擇aosp_bullhead-userdebug,具體裝置要參考裝置代號

### 9、刷機
編譯完成後`out/target/product/bullhead/`目錄生成相應的映象檔案,我們只需要在原始碼根目錄執行下面命令進行刷機

adb reboot bootloader //手機重啟進入bootloader模式
fastboot flashall -w //刷寫映象