漫漫Android路---1.AOSP源码获取编译及导入
写在前面
年轻时候的他,发际线尤在,互联网某厂开发Android多年,退三四线某城市苟且
空有撸码双手,却无处安放,子曰温故而知新可以为师矣…故记录此系列以防失能
环境准备
- 1.Ubuntu22.04(700G+8核+8G DDR4)
- 2.VMware® Workstation 16 Pro
AOSP源码获取
AOSP源码较为庞大,相关各模块均是基于git工具进行项目管理与代码提交,整个源码是基于多个git目录分模块整合的
Google基于Python开发了辅助开发者进行多个项目管理的工具Repo,Repo就是封装了git命令的python脚本
1.Repo下载配置
# 在home下创建bin文件夹
mkdir ~/bin
# 把bin文件夹加入环境变量的
PATH=~/bin:$PATH
#下载repo脚本
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
#添加权限
chmod a+x ~/bin/repo
若提示curl提示需要安装
sudo apt install curl
2.Git安装及配置
sudo apt-get install git
git config --global user.name YourName
git config --global user.email [email protected]
3.AOSP获取
由于某些不可控的网络因素,需要从清华源去获取对应的AOSP源码
https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar
AOSP项目所有的分支列表:代号、标记和 Build 号 | Android 开源项目 | Android Open Source Project
具体流程如下:
# 讲代码包下载到当前目录
wget https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar
# 解压对应的压缩包得到aosp源码目录
tar xf aosp-latest.tar
# 进入源码目录
cd aosp
# 初始化仓库
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest
# repo制定android版本,末尾的-b android-9.0.0_r46用于指定具体的源码的版本
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-9.0.0_r46
# 同步源码,正常同步后即可获得完整目录
# 同步中提示无法连接到 gerrit.googlesource.com
# 需要修改~/bin/repo文件
# 找到REPO_URL,修改为https://mirrors.tuna.tsinghua.edu.cn/git/git-repo
repo sync
# 遇到同步失败,可以将如下内容复制到shell脚本中(遇到错误,让循环同步源码)
repo sync -j4
while [ $? -ne 0 ]
do
repo sync -j4
done
AOSP源码编译
官方参考:搭建构建环境 | Android 开源项目 | Android Open Source Project
1.安装编译依赖
sudo apt install libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev g++-multilib
sudo apt install -y git flex bison gperf build-essential libncurses5-dev:i386
sudo apt install tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386
sudo apt install dpkg-dev libsdl1.2-dev libesd0-dev
sudo apt install git-core gnupg flex bison gperf build-essential
sudo apt install zip curl zlib1g-dev gcc-multilib g++-multilib
sudo apt install libc6-dev-i386
sudo apt install lib32ncurses5-dev x11proto-core-dev libx11-dev
sudo apt install libgl1-mesa-dev libxml2-utils xsltproc unzip m4
sudo apt install lib32z-dev ccache
2.开始执行编译
2.1 初始化环境
执行envsetup.sh脚本初始化环境
# 进入源码目录
cd aosp
# 前面的点要加上 不然提示权限被拒绝
. build/envsetup.sh
2.2 选择编译目标
通过lunch命令指定编译目标,即编译出的镜像文件运行在哪个型号的Android设备,我这边编译aosp_x86-eng,执行命令
lunch aosp_x86-eng
2.3 开始编译镜像
通过make指令进行代码编译,该指令通过-j 参数设置编译线程数
以提高编译速度.比如这里我们设置4个线程同时编译:
make -j4
参与编译的线程并不是越多越好,通常是根据设备CPU的核心来确定core*2,也就是CPU核心的2倍
3.编译相关问题
3.1 Java与Python环境变量引起的
Java与Python两个版本共存及切换
原先配置的Java环境变量为Oracle的(路径为/home/主机名称/DevTools/jdk1.8.0/bin/)
AOSP编译需要指定为OpenJDK的
#add java alternatives orcale-jdk
sudo update-alternatives --install /usr/bin/java java /home/主机名称/DevTools/jdk1.8.0/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /home/主机名称/DevTools/jdk1.8.0/bin/javac 2
sudo update-alternatives --config java
#接下来选择对应的所需版本即可
Utuntu22.04系统内置Python版本为3.10,AOSP编译需要的Python版本为2.7
编译中经常会遇到Python2与Python3的语法不一致导致的问题
#add python alternatives python2.7 python3.10
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 2
sudo update-alternatives --config python
#接下来选择对应的所需版本即可
3.2 flex编译引起的问题
system/extras/micro_bench/micro_bench.cpp:434:12: warning: unused variable 'alloc_size' [-Wunused-variable]
size_t alloc_size = size * 2 + 3 * cmd_data.dst_align; // should alloc_size be used?
^
1 warning generated.
[ 6% 5444/79608] target C++: micro_bench_static <= system/extras/micro_bench/micro_bench.cpp
system/extras/micro_bench/micro_bench.cpp:434:12: warning: unused variable 'alloc_size' [-Wunused-variable]
size_t alloc_size = size * 2 + 3 * cmd_data.dst_align; // should alloc_size be used?
^
1 warning generated.
[ 7% 5599/79608] Build hyb out/target/product/generic_x86/obj/ETC/hyph-hu_intermediates/hyph-hu.hyb <- external/hyphenation-patterns/hu//hyph-hu.pat.txt
21515 unique nodes, 102669 total
[ 7% 5964/79608] Target buildinfo: out/target/product/generic_x86/obj/ETC/system_build_prop_intermediates/build.prop
Target buildinfo from: build/make/target/board/gsi_system.prop
[ 9% 7810/79608] //external/one-true-awk:awk_vendor yacc awkgram.y
external/one-true-awk/awkgram.y: conflicts: 43 shift/reduce, 85 reduce/reduce
[ 10% 8039/79608] //external/one-true-awk:awk yacc awkgram.y
external/one-true-awk/awkgram.y: conflicts: 43 shift/reduce, 85 reduce/reduce
[ 10% 8088/79608] //external/selinux/checkpolicy:checkpolicy lex policy_scan.l [linux_glibc]
FAILED: out/soong/.intermediates/external/selinux/checkpolicy/checkpolicy/linux_glibc_x86_64/gen/lex/external/selinux/checkpolicy/policy_scan.c
prebuilts/misc/linux-x86/flex/flex-2.5.39 -oout/soong/.intermediates/external/selinux/checkpolicy/checkpolicy/linux_glibc_x86_64/gen/lex/external/selinux/checkpolicy/policy_scan.c external/selinux/checkpolicy/policy_scan.l
flex-2.5.39: loadlocale.c:130: _nl_intern_locale_data: Assertion `cnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))' failed.
Aborted (core dumped)
[ 10% 8091/79608] //system/netd/bpfloader:bpfloader clang++ BpfLoader.cpp
ninja: build stopped: subcommand failed.
01:18:07 ninja failed with: exit status 1
分析日志
prebuilts/misc/linux-x86/flex/flex-2.5.39
oout/soong/.intermediates/external/selinux/checkpolicy/checkpolicy/linux_glibc_x86_64/gen/lex/external/selinux/checkpolicy/policy_scan.c
external/selinux/checkpolicy/policy_scan.l
Flex这款语法解释器生成器(lexical analyser generator),作用是将以*.l为扩展名的源文件编译成解释器的C语言代码
checkpolicy这个用于检测SELinux策略(SELinux policy)的工具,正是使用flex来解析策略文件的语法的
解决方案
flex需要手动解压并进行编译配置,命令如下:
cd prebuilts/misc/linux-x86/flex
rm flex-2.5.39
tar zxf flex-2.5.39.tar.gz
cd flex-2.5.39
./configure
make
mv flex ..
cd ..
rm flex-2.5.39
mv flex flex-2.5.39
AOSP源码导入AS
1.编译生成idegen.jar
源码根目录执行如下命令:
source build/envsetup.sh
make idegen
终端输出:
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=9
TARGET_PRODUCT=aosp_x86
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_ARCH=x86
TARGET_ARCH_VARIANT=x86
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-5.15.0-52-generic-x86_64-Ubuntu-22.04.1-LTS
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=PQ3A.190801.002
OUT_DIR=out
============================================
ninja: no work to do.
ninja: no work to do.
No need to regenerate ninja file
No need to regenerate ninja file
[100% 5/5] Install: out/host/linux-x86/framework/idegen.jar
#### build completed successfully (8 seconds) ####
2.生成 android.ipr 和 android.iml
$ development/tools/idegen/idegen.sh
Read excludes: 13ms
Traversed tree: 51647ms
3.AndroidStudio打开AOSP源码
File->Open->选择源码aosp目录下的android.ipr文件
导入完成后即可看到整个Android源码的目录,接下来从Java开始学习吧
版权归原作者 Six.Plus 所有, 如有侵权,请联系我们删除。