Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

單機搭建 Android 開發環境(四):從 JDK 到首次編譯成功

編輯:關於 Android 開發

單機搭建 Android 開發環境(四):從 JDK 到首次編譯成功

前三篇屬於磨刀,這一篇終於要動真格的:安裝 JDK、repo、git 以及各類編譯依賴,然後拉代碼、跑編譯。以下步驟基於 64 位 Ubuntu 12.04 Server,如果你用的是其他版本,建議直接對照 Google 官方說明操作。

1. 安裝 OpenJDK 7

sudo apt-get install openjdk-7-jdk --force-yes -y

驗證安裝結果:

java -version
# java version "1.7.0_95"
# OpenJDK Runtime Environment (IcedTea 2.6.4) (7u95-2.6.4-0ubuntu0.12.04.1)
# OpenJDK 64-Bit Server VM (build 24.95-b01, mixed mode)

配置環境變量:

sudo vim ~/.profile
# 文件末尾加入
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

# 讓配置立即生效
source ~/.profile

2. 安裝編譯依賴

Android 源碼編譯依賴一大堆基礎庫和 32 位運行時,這一步不能省:

sudo apt-get install gnupg flex bison gperf build-essential --force-yes -y
sudo apt-get install zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev --force-yes -y
sudo apt-get install libx11-dev:i386 libreadline6-dev:i386 --force-yes -y
sudo apt-get install libgl1-mesa-dev g++-multilib mingw32 tofrodos --force-yes -y
sudo apt-get install python-markdown libxml2-utils xsltproc zlib1g-dev:i386 --force-yes -y
sudo apt-get install libgl1-mesa-dri:i386 dpkg-dev --force-yes -y

3. 安裝 repo

原版 repo 在國內網絡下經常報這類錯誤:

fatal: Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle
fatal: error [Errno 101] Network is unreachable

建議直接下載國內定制版(例如 repo 1.22),拷到 /bin 並賦予執行權限:

sudo cp /home/repo /bin/
sudo chmod a+x /bin/repo

4. 安裝 git 並配置身份

sudo apt-get install git --force-yes -y

git config --global user.email "hjb@bonovo.cn"
git config --global user.name "hjb"

5. 生成並上傳公鑰

ssh-keygen -t rsa -C hjb@bonovo.cn

cp /home/hjb/.ssh/id_rsa.pub /home/hjb/id_rsa.pub.hjb113

# 在 git 服務器上,把公鑰追加到授權列表
cat /home/hjbang/id_rsa.pub.hjb113 >> /home/bnv/.ssh/authorized_keys

6. 建立工作目錄

cd ~
mkdir mt8735
cd mt8735

7. 同步代碼

repo init -u bnv@192.168.1.12:/home/git/MTK/lp5.1-mt8735m-mirror/bonovo/platform/manifests.git -b default
repo sync
repo forall -c git checkout lp5.1-mt6735m
repo forall -c git checkout mt8735-avn401
repo forall -c git pull

8. 配置 ccache

Android 全量編譯的龐大程度,不用 ccache 簡直是自找麻煩:

sudo apt-get install -y ccache
sudo vim ~/.bashrc
# 末尾加入
export USE_CCACHE=1

source ~/.bashrc
echo $USE_CCACHE

9. 開始編譯

cd ~/mt8735
prebuilts/misc/linux-x86/ccache/ccache -M 50G
. build/envsetup.sh
launch 25
make update-api

make update-api 大約耗時 18 分鐘,主要在做文檔抽取:

Docs droiddoc: out/target/common/docs/doc-comment-check
DroidDoc took 513 sec. to write docs to out/target/common/docs/doc-comment-check
Copying current.txt
Copying removed.txt
Docs droiddoc: out/target/common/docs/system-api-stubs
DroidDoc took 13 sec. to write docs to out/target/common/docs/system-api-stubs
Copying system-current.txt
Copying system-removed.txt
#### make completed successfully (17:54 (mm:ss)) ####

接著編譯 5.1 系統鏡像:

make -j24

編譯過程中想實時觀察緩存命中情況,可以另開一個終端:

watch -n1 -d prebuilts/misc/linux-x86/ccache/ccache -s
cache directory /home/hjb/.ccache
cache hit (direct)                     0
cache hit (preprocessed)               0
cache miss                         43927
called for link                     1209
preprocessor error                   732
unsupported source language          968
unsupported compiler option            2
files in cache                    138583
cache size                        18.0 Gbytes
max cache size                    50.0 Gbytes

可以看到緩存已經漲到 18GB。這也解釋了一個常見困惑:ccache 並不會讓第一次編譯變快,反而因為要寫緩存而更慢,但後續增量編譯的提速非常明顯。

四個半小時後,首次全量編譯終於通過:

Creating filesystem with parameters:
    Size: 1610612736
    Block size: 4096
    Blocks per group: 32768
    Inodes per group: 8192
    Inode size: 256
    Journal blocks: 6144
    Label:
    Blocks: 393216
    Block groups: 12
    Reserved block group size: 95
Created filesystem with 2436/98304 inodes and 221139/393216 blocks
Install system fs image: out/target/product/mt8735_tb_l1/system.img
#### make completed successfully (04:25:03 (hh:mm:ss)) ####

而後續只改動內核模塊代碼再重新編譯,13 分鐘左右就能出鏡像:

Install system fs image: out/target/product/mt8735_tb_l1/system.img
#### make completed successfully (13:04 (mm:ss)) ####

即便使用 SSD,這個增量時間也很難壓到 5 分鐘以內。所以調內核驅動模塊時,強烈建議改用動態加載模塊(insmod/rmmod)的方式驗證,而不是每次都重新出鏡像、重新刷機,效率差距會非常明顯。

至此,Android 系統的編譯環境基本搭建完成。下一篇我們來處理應用層開發環境。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved