Android 系统源码编译环境搭建

一、Ubuntu 14.04

1.1 安装openjdk

1
2
3
4
5
6
7
8
9
sudo apt-get purge openjdk*
sudo add-apt-repository ppa:webupd8team/java
sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install oracle-java8-installer
sudo apt-get install openjdk-8-jdk
#同时安装多个版本的Jdk,切换JDK版本的方法
sudo update-alternatives --config java
sudo update-alternatives --config javac

1.2 安装软件包

1
2
3
4
5
#编译依赖包
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
sudo apt-get install libncurses5-dev:i386 libreadline6-dev:i386 tofrodos python-markdown dpkg-dev lib32readline-gplv2-dev uboot-mkimage
#常用工具包
sudo apt-get install vim htop nautilus-open-terminal exfat-utils sysv-rc-conf exuberant-ctags openssh-server samba smbclient smbfs subversion

二、MAC OS

2.1 创建磁盘映像

1
2
hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 40g ~/android.dmg
hdiutil resize -size <new-size-you-want>g ~/android.dmg.sparseimage

2.2 挂载/卸载磁盘

1
2
3
4
# mount the android file image
mountAndroid() { hdiutil attach ~/android.dmg -mountpoint /Volumes/android;}
# unmount the android file image
umountAndroid() { hdiutil detach /Volumes/android; }

2.3 安装jdk

1
2
3
4
#安装 Xcode 命令行工具
xcode-select --install
#通过 MacPorts 获取 Make、Git 和 GPG 软件包
POSIXLY_CORRECT=1 sudo port install gmake libsdl git gnupg

三、其他组件安装

3.1 安装Repo

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

3.2 配置git账号

1
2
git config --global user.name "Name"
git config --global user.email "Name@company.com"

3.3 拉取代码

1
2
3
4
repo init -u https://android.googlesource.com/platform/manifest
#指定branch分支
repo init -u https://android.googlesource.com/platform/manifest -b android-9.0.1_r1
repo sync

3.4 编译代码

1
2
3
source build/envsetup.sh
lunch aosp_arm-userdebug
make -j4 2>&1 | tee build.log

3.5 fastboot烧录软件

1
2
3
4
5
6
7
8
9
10
11
12
adb reboot bootloader
#打开开发者选项中OEM解锁,再使用fastboot解锁
fastboot flashing unlock
#清空系统分区,然后将system.img刷写到系统分区
fastboot erase system
fastboot flash system out/target/product/aosp/system.img
#擦除用户数据并清除其他必要分区的数据
fastboot -w
#重新锁定bootloader
fastboot flashing lock
#重新启动
fastboot reboot

四、常见环境不匹配报错案例

4.1 java版本

1
2
3
4
5
6
7
8
************************************************************
You are attempting to build with the incorrect version
of java.
Your version is: WRONG_VERSION.
The correct version is: RIGHT_VERSION.
Please follow the machine setup instructions at
https://source.android.com/source/initializing.html
************************************************************

4.2 mac不区分大小写文件系统

1
2
3
4
************************************************************
You are building on a case-insensitive filesystem.
Please move your source tree to a case-sensitive filesystem.
************************************************************