一.准备工作
Android 官方声明 AOSP 历来是在 Ubuntu LTS 版本中进行开发和测试。所以为了避免任何其他不必要的学习之下载详解麻烦,请使用 Ubuntu 及其他基于 Ubuntu 的源码发行版
最低环境要求:
二.下载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环境),因为Android源码包含数百个git库,学习之下载详解简化帮助管理git Android版本库的源码工具。
(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的源码越来越大,repo sync失败的概率也越来越高。所以我们可以避开使用repo sync的方式,而采用下载预下载包的方式来实现下载源码
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 # 正常同步一遍即可得到完整目录
或者直接在aosp目录设置好你要同步的分支:
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文件
#!/bin/bash
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文件
sh down.sh
三.AOSP源码目录结构
四.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
选择与设备对应的编译版本.如:编译开发工程师的版本lunch aosp_x86-eng,可以方便debug
编译版本选择
如果lunch命令没有加对应的编译版本则会有以下信息输出:
You're building on Linux
Lunch menu... pick a combo:
1. aosp_arm-eng
2. aosp_arm64-eng
3. aosp_mips-eng
4. aosp_mips64-eng
5. aosp_x86-eng
6. aosp_x86_64-eng
7. full_fugu-userdebug
8. aosp_fugu-userdebug
9. mini_emulator_arm64-userdebug
10. m_e_arm-userdebug
11. m_e_mips64-eng
12. m_e_mips-userdebug
13. mini_emulator_x86_64-userdebug
14. mini_emulator_x86-userdebug
15. aosp_dragon-userdebug
16. aosp_dragon-eng
17. aosp_marlin-userdebug
18. aosp_sailfish-userdebug
19. aosp_flounder-userdebug
20. aosp_angler-userdebug
21. aosp_bullhead-userdebug
22. hikey-userdebug
23. 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后面数字几就是多少线程,最多不超过你的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<size>'错误
解决方案:
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"
jack-admin kill-server
jack-admin start-server
五.运行模拟器
启动模拟器
在编译完成之后,就可以通过以下命令运行Android虚拟机了,由于之前已经执行过source和lunch命令了,可以直接运行:
source build/envsetup.sh
lunch aosp_x86-eng
emulator
就会启动模拟器
六.AOSP源码编译某个单独的模块
上面的编译我们都是对整个Android系统进行编译的.如果我们要编译系统的Settings应用模块,这就属于源码单编某一个模块.
在AOSP根目录执行:
source build/envsetup.sh
lunch aosp_x86-eng
进入Settings的目录:
cd packages/apps/Settings
通过mm编译当前目录下的模块,不编译依赖模块。
mm
编译成功后会有提示生成文件的存放路径。除了Settings.odex文件,还会在out/target/product/generic_x86/system/priv-app/Settings目录下生成Settings.apk。
此外还有以下命令可以进行单编:
如果对系统模块的源码进行修改,查看生成的APK文件,有两种方式:
(责任编辑:探索)
“放水养鱼”式管理激发市场活力 安徽降本减负典型经验做法获点赞
精选层与科创板存在多维度“神似” 62家“新三板基因”公司正申报科创板
埃斯顿(002747.SZ):埃斯顿投资减持749.18万股 占公司总股本的比例约为0.89%
安徽省池州市运用PPP模式建设海绵城市 成功解决了城市建设中资金难题