1. 制定工作计划
Android 官方声明 AOSP 一直在 Ubuntu LTS 版本中开发和测试。因此,为了防止任何其他必要的麻烦,请使用 Ubuntu 和其他基于 Ubuntu 的发行版
最低环境要求:
2.下载Aosp源码
1.安装GIT
首先需要安装Git,因为源代码是用Git管理的。
sudo apt-get install git
然后创建一个bin文件夹并将其添加到PATH中,有点像Windows环境变量。
mkdir ~/bin
PATH=~/bin:$PATH
然后初始化Git,邮箱和姓名。
git config --global user.email "xxx@gmail.com"
git config --global user.name "xxx"
2.安装Python环境
sudo apt-get install python
3.安装repo并配置
repo是一个python脚本(所以我们要提前配置Python环境)runtime源码编译详细,由于Android源代码包含数百个git库,它简化了帮助管理gitAndroid版本库的工具。
(1)安装curl下载的库:
sudo apt-get install curl
(2) 下载repo并设置运行权限。
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo
chmod a+x ~/bin/repo
(3)添加下载源
对于google的AOSP,由于FQ和数据量太大,需要翻墙影响速度,优先考虑国外镜像(本文使用复旦来源)。
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'
4.初始化与同步源码
(1) 创建文件夹
创建一个AOSP文件夹,cdd到该文件夹并快速下载源代码:
mkdir aosp
cd aosp
(2)初始化Aosp仓库
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest
(3)初始化并指定版本
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-8.0.0_r36
AOSP通信查看地址:通信
(4)开始同步源码
repo sync -j4
-j表示并发数.
随着Android的源码越来越大,reposync失败的概率也越来越高。所以我们可以避免使用reposync的方式,采用下载预下载包的形式来下载源码。
5.预下载包的形式
1.下载预下载包
在windows或者Linux上面通过迅雷下载https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar
wget -c https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar #下载初始化包
2.解压预下载包
tar xf aosp-latest.tar
cd AOSP->解压得到的 AOSP 工程目录,这时 ls 的话什么也看不到,因为只有一个隐藏的 .repo 目录
3.查看分支
cd .repo/manifests
git branch -a
4、在aosp目录下选择需要同步的版本
repo init -b android-9.0.0_r55
repo sync # 正常同步一遍即可得到完整目录
:
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-8.0.0_r36
repo sync # 正常同步一遍即可得到完整目录
:
repo sync platform/prebuilts/clang/host/darwin-x86
5.避免下载源码和执行脚本时卡住。
通过自定义Shell脚本启动源码下载,可以有效避免同步源码时脚本卡住的问题。
(1)在AOSP文件夹下新建down.sh文件
echo “======= start repo sync =======”
cd ~/Desktop/AOSP
repo sync -j4
while [ $? == 1 ]; do
echo “====== sync failed! re-sync again =====”
sleep 3
repo sync -j4
(2)执行down.sh文件
shdown.sh
3.AOSP源码目录结构
4.AOSP源码重组
编译AOSP源码需要配置JAVA环境。
1.安装Java编译环境
sudo apt-get update
sudo apt-get install openjdk-8-jdk
2.进入AOSP文件夹并编译
(1)初始化编译环境
source build/envsetup.sh
(2)删除上次编译的结果。 第一次编译不需要此步骤。
make clobber
(3)选择设备对应的编译版本
lunch XX
选择设备对应的编译版本。 例如:编译开发工程师的版本lunchaosp_x86-eng,可以方便调试
编译版本选择
如果lunch命令没有添加对应的编译版本,则会输出以下信息:
building on Linux
Lunch menu... pick a combo:
aosp_arm-eng
aosp_arm64-eng
aosp_mips-eng
aosp_mips64-eng
aosp_x86-eng
aosp_x86_64-eng
full_fugu-userdebug
aosp_fugu-userdebug
mini_emulator_arm64-userdebug
m_e_arm-userdebug
m_e_mips64-eng
m_e_mips-userdebug
mini_emulator_x86_64-userdebug
mini_emulator_x86-userdebug
aosp_dragon-userdebug
aosp_dragon-eng
aosp_marlin-userdebug
aosp_sailfish-userdebug
aosp_flounder-userdebug
aosp_angler-userdebug
aosp_bullhead-userdebug
hikey-userdebug
aosp_shamu-userdebug
Which would you like? [aosp_arm-eng]
这里需要选择编译目标的格式(选择上面的序号并回车),编译目标的格式为BUILD-BUILDTYPE,例如aosp_arm-eng的BUILD为aosp_arm,BUILDTYPE为eng 。 其中,BUILD表示编译后的镜像可以运行在哪些环境,aosp表示Android开源项目,arm表示系统运行在基于arm的处理器上;
BUILDTYPE指的是编译类型,共有三种类型:
(4)开始编译
make -j8
j前面的数字是线程数runtime源码编译详细,不能超过你CPU的线程总数。
如果编译成功,会显示如下:
Creating filesystem with parameters:
Size: 2147483648
Block size: 4096
Blocks per group: 32768
Inodes per group: 8192
Inode size: 256
Journal blocks: 8192
Label: system
Blocks: 524288
Block groups: 16
Reserved block group size: 127
Created filesystem with 2216/131072 inodes and 199826/524288 blocks
[100% 7669/7669] Install system fs ima.../target/product/generic_x86/system.img
out/target/product/generic_x86/system.img+ maxsize=2192446080 blocksize=2112 total=2147483648 reserve=22146432
#### make completed successfully (01:24:41 (hh:mm:ss)) ####
图像文件将在源代码和目录 out/target/product/angler 目录中生成:
最终会在out/target/product/generic_x86/目录下生成三个重要的镜像文件:system.img、userdata.img、ramdisk.img。 大约介绍了三个镜像文件:
3.编译错误解决
(1)缺少libncurses.so.5
错误信息:
error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
解决方案:
sudo apt-get update
for 32-bit binaries :
sudo apt-get install libncurses5:i386
for 64-bit binaries :
sudo apt-get install libncurses5
(2)缺少M4
错误信息:
/bin/bash: m4: command not found
解决方案:
sudo apt-get install m4
(3) 消除所有本地化设置
错误信息:
FAILED: out/host/linux-x86/obj/EXECUTABLES/checkpolicy_intermediates/policy_scan.c
解决方案:
export LC_ALL=C
LC_ALL=C 是为了去除所有本地化的设置,让命令能正确执行, 但是不可以修改~/.bashrc,会导致终端内中文显示为数字(应该是对应的编码)
(4) xmllint的问题
错误信息:
/bin/bash: xmllint: command not found
解决方案:
sudo apt-get install libxml2-utils
(5)编译内存不足
错误信息
Try increasing heap size with java option '-Xmx'错误
解决方案:
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"
jack-admin kill-server
jack-admin start-server
5.运行模拟器
启动模拟器
编译完成后,可以通过以下命令运行Android虚拟机,因为之前已经执行过source和lunch命令,直接运行即可:
source build/envsetup.sh
lunch aosp_x86-eng
emulator
将启动模拟器
6. 从AOSP源代码编译单个模块
我们在编译中编译了整个Android系统。 如果我们要编译系统的Settings应用模块,这属于源代码列表中编译的某个模块。
在AOSP根目录下执行:
source build/envsetup.sh
lunch aosp_x86-eng
进入设置目录:
cd packages/apps/Settings
通过mm编译当前目录下的模块,不编译依赖模块。
毫米
编译成功后,会提示生成文件的存储路径。 out/target/product/generic_x86/system/priv-app/Settings目录下不仅会生成Settings.odex文件,还会生成Settings.apk。
据悉,可以使用以下命令进行单次编辑:
如果修改系统模块的源码并查看生成的APK文件,有两种方法: