mkdir android-6.0.1_r72
cd android-6.0.1_r72
2.下载相应版本的源码
先查询设备支持的版本,你要下载那个版本的源码,科学上网查看source.android.com/source/buil…。表格如下格式,查询对应设备的相应分支。
Build | Branch | Version | Supported devices |
---|---|---|---|
NDE63X | android-7.1.0_r7 | Nougat | Pixel XL, Pixel |
… | … | … | … |
M4B30X | android-6.0.1_r72 | Marshmallow | Nexus 5 |
… | … | … | … |
MOB30Y | android-6.0.1_r60 | Marshmallow | Nexus 5 |
… | … | … | … |
MMB29K | android-6.0.0_r1 | Marshmallow | Nexus 5 |
… | … | … | … |
源码下载,具体下载那个版本只需修改后面的分支名称。
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-6.0.1_r72
init成功后,开始漫长的同步源码树(以后只需执行这条命令来同步)。
repo sync
这还不够,因为很有可能出错跳出来了,我们需要一个脚本,在出问题的情况下再次执行repo sync,在当前需要下载源码的目录下新建down.sh文件,直接使用vi命令即可。
vi down.sh
接下来,在vi编辑器内输入下面这段脚本代码,保存退出。
#!/bin/sh
repo sync
while [ $? -ne 0 ]
do
repo sync
done
用这段脚本执行,替换repo sync这条命令。
./down.sh
N个小时过去后,终于下载好了。小水管的笔者下了15个多小时才完成。开始着手搭建源码编译环境。
五、源码编译
对应分支的源码下载好以后,接下来就开始激动人心的编译工作,但编译之前还需要先配置环境。
1.搭建编译环境
笔者环境
- Ubuntu 16.04 LTS
- Android android-6.0.1_r72分支源码
- Open JDK 7
上面的两者已经具备了,那么还需要安装Open JDK 7,注意是Open JDK,并非甲骨文的Oracle JDK。
这部分内容引自csdn博主梧桐那时雨。
(1) 更换软件源
为了更快的安装软件,我们需要更换软件源为国内的软件源,这里推荐使用Ubuntu官方指定的国内软件源阿里云,具体方法是:
sudo gedit /etc/apt/sources.list
在文件最前面加入下面代码:
deb http://mirrors.aliyun.com/ubuntu/ quantal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ quantal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ quantal-updates main restricted universe multiverse
deb http://mirror
s.aliyun.com/ubuntu/ quantal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ quantal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ quantal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ quantal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ quantal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ quantal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ quantal-backports main restricted universe multiverse
(2) 安装依赖
sudo apt-get install -y git flex bison gperf build-essential libncurses5-dev:i386
sudo apt-get install libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev g+±multilib
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 lib32z-dev ccache
sudo apt-get install libgl1-mesa-dev libxml2-utils xsltproc unzip m4
Ubuntu 16.04请务必使用上面的依赖,Ubuntu 16.04需要的依赖和Ubuntu 14.04所需要的依赖是不同的。
(3) 安装open JDK7
由于Ubuntu 16.04没有OpenJDK7的源,因此在16.04上安装OpenJDK7需要执行下面的命令:
sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-7-jdk
配置OpenJDK.打开/etc/profile文件:
sudo gedit /etc/profile
在末尾追加下面代码:
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
export JRE_HOME= J A V A H O M E / j r e e x p o r t C L A S S P A T H = . : {JAVA_HOME}/jre export CLASSPATH=.: JAVAHOME/jreexportCLASSPATH=.:{JAVA_HOME}/lib: J R E H O M E / l i b e x p o r t P A T H = {JRE_HOME}/lib export PATH= JREHOME/libexportPATH={JAVA_HOME}/bin:$PATH
修改了/etc/profile文件需要重启才能生效,但使用下面命令可以在不重启的情况下在当前bash环境生效:
source /etc/profile
检查OpenJDK配置是否正确:
java -version
配置正确如下图:
2.修改源码待编译
修改android-6.0.1_r72/art/build/Android.common_build.mk 文件,定位到75行,将下面的代码改为false:
ifneq ( ( W I T H O U T H O S T C L A N G ) , t r u e ) 改为 i f e q ( (WITHOUT_HOST_CLANG),true) 改为 ifeq ( (WITHOUTHOSTCLANG),true)改为ifeq((WITHOUT_HOST_CLANG),false)
3.下载驱动
官网下载对应版本的驱动:developers.google.com/android/nex…
我这里下载对应的M4B30X,下载成功后得到三个压缩包broadcom-hammerhead-m4b30x-7c7b231f.tgz、lge-hammerhead-m4b30x-74fa3aa5.tgz、qcom-hammerhead-m4b30x-158606cf.tgz,解压这三个压缩包,解压出来是三个sh文件,放到Android源码目录下面即android-6.0.1_r72下,然后执行。会将相关驱动放到vender目录下面。
拷贝三个sh到源码目录:
cp /media/lhw/LHW/extract-broadcom-hammerhead.sh /home/lhw/aosp/android-6.0.1_r72/
cp /media/lhw/LHW/extract-lge-hammerhead.sh /home/lhw/aosp/android-6.0.1_r72/
cp /media/lhw/LHW/extract-qcom-hammerhead.sh /home/lhw/aosp/android-6.0.1_r72/
拷贝成功后,赋予执行权限
chmod a+x extract-broadcom-hammerhead.sh
chmod a+x extract-lge-hammerhead.sh
chmod a+x extract-qcom-hammerhead.sh
执行每个sh,输入I ACCEPT,最终将驱动放到vender目录待使用。
4.开始编译
(1) 在 .bashrc文件末尾添加:export USE_CCACHE = 1
echo export USE_CCACHE=1 >> ~/.bashrc
(2) 为了提高编译效率,设置编译器高速缓存:
prebuilts/misc/linux-x86/ccache/ccache -M 50G
(3) 接着导入编译Android源码所需的环境变量和其它参数:
source build/envsetup.sh
including device/asus/deb/vendorsetup.sh
including device/asus/flo/vendorsetup.sh
including device/asus/fugu/vendorsetup.sh
including device/generic/mini-emulator-arm64/vendorsetup.sh
including device/generic/mini-emulator-armv7-a-neon/vendorsetup.sh
including device/generic/mini-emulator-mips/vendorsetup.sh
including device/generic/mini-emulator-x86_64/vendorsetup.sh
including device/generic/mini-emulator-x86/vendorsetup.sh
including device/htc/flounder/vendorsetup.sh
including device/huawei/angler/vendorsetup.sh
including device/lge/bullhead/vendorsetup.sh
including device/lge/hammerhead/vendorsetup.sh
including device/moto/shamu/vendorsetup.sh
including sdk/bash_completion/adb.bash
(4) 运行lunch命令选择编译目标:
lunch
Lunch menu… pick a combo:
- aosp_arm-eng
- aosp_arm64-eng
- aosp_mips-eng
- aosp_mips64-eng
- aosp_x86-eng
- aosp_x86_64-eng
- aosp_deb-userdebug
- aosp_flo-userdebug
- full_fugu-userdebug
- aosp_fugu-userdebug
- mini_emulator_arm64-userdebug
- m_e_arm-userdebug
- mini_emulator_mips-userdebug
- mini_emulator_x86_64-userdebug
- mini_emulator_x86-userdebug
- aosp_flounder-userdebug
- aosp_angler-userdebug
- aosp_bullhead-userdebug
- aosp_hammerhead-userdebug
- aosp_hammerhead_fp-userdebug
- aosp_shamu-userdebug
选择19:
Which would you like? [aosp_arm-eng] 19
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=6.0.1
TARGET_PRODUCT=aosp_hammerhead
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a-neon
TARGET_CPU_VARIANT=krait
TARGET_2ND_ARCH=
TARGET_2ND_ARCH_VARIANT=
TARGET_2ND_CPU_VARIANT=
HOST_ARCH=x86_64
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.4.0-47-generic-x86_64-with-Ubuntu-16.04-xenial
HOST_BUILD_TYPE=release
BUILD_ID=M4B30X
OUT_DIR=out
(5) 开始编译,执行make -j6, 6为同时编译的线程数,一般google推荐这个数字为2倍的cpu个数再加上2,比如4核,就是10。
make -j6
查看cpu核心个数命令:
cat /proc/cpuinfo
编译结束,大功告成!首次编译花费了6个多小时,out目录会有对应的输出文件。
五、刷机
1.adb、fastboot命令生效
关于这一步大家应该不陌生,android platform-tools里有adb、fastboot。具体可以到www.androiddevtools.cn/下载,配置环境变量,确保fastboot命令正常工作。
2.进入recovery模式
Nexus 5关机状态下,长按音量下键和power键,即可进入recovery模式
3.开始刷机
源码根目录下输入:
fastboot -w flashall
执行命令后输出如下:
target reported max download size of 1073741824 bytes
Creating filesystem with parameters:
Size: 29236371456
Block size: 4096
Blocks per group: 32768
Inodes per group: 8192
Inode size: 256
Journal blocks: 32768
Label:
Blocks: 7137786
Block groups: 218
Reserved block group size: 1024
Created filesystem with 11/1785856 inodes and 156120/7137786 blocks
Creating filesystem with parameters:
Size: 734003200
Block size: 4096
Blocks per group: 32768
Inodes per group: 7472
Inode size: 256
Journal blocks: 2800
Label:
Blocks: 179200
Block groups: 6
Reserved block group size: 47
Created filesystem with 11/44832 inodes and 5813/179200 blocks
Bootloader Version…: HHZ20f
Baseband Version…: M8974A-2.0.50.2.29
Serial Number…: 0313b757307b7e19
checking product…
OKAY [ 0.100s]
sending ‘boot’ (9160 KB)…
d block group size: 47
Created filesystem with 11/44832 inodes and 5813/179200 blocks
Bootloader Version…: HHZ20f
Baseband Version…: M8974A-2.0.50.2.29
Serial Number…: 0313b757307b7e19
checking product…
OKAY [ 0.100s]
sending ‘boot’ (9160 KB)…