一、前言
安卓AOSP源码的拉取和编译都需要基于Linux系统,推荐ubuntu24.04(250G+6核+16GRAM),具体如何获取Ubuntu系统,请自行网上搜索,如果想要使用window环境,通过虚拟机安装Ubuntu,可以参考Android系统源码_AOSP入门(一)使用VirtualBox安装Ubuntu系统这篇文章。
二、安装repo
2.1 简介
Repo 是我们以 Git 为基础构建的代码库管理工具。Repo 可以在必要时整合多个 Git 代码库,将相关内容上传到我们的修订版本控制系统,并自动执行 Android 开发工作流程的部分环节。Repo 并非用来取代 Git,只是为了让您在 Android 环境中更轻松地使用 Git,Repo的常见应用场景:
- 大型软件开发项目:多仓,多分支,多流程
- 多仓管理:多责任田,多评审网站等
- 搭配Gerrit使用
由于Android系统源码是非常庞大的,而且每个模块都是用git来进行管理 ,整个Android源码是由很多个git项目构成,Google对Android代码的更新也是更新到相应模块的git项目上。那对于需要编译Android的开发者来说,要分别clone 每个git项目而且还要放到固定的位置确实是件惨绝人寰的事,所以Google就开发了一个基于Python编写的帮助开发者管理多个项目的工具,这个工具就叫repo,repo就是封装了git命令的python脚本。
2.2 安装python3
由于Python 2 的支持已于 2020 年 1 月 1 日停止,现在的Repo也必须使用Python 3。
sudo apt-get install python3
alias python=python3
2.3 安装Git & 配置Git信息
sudo apt-get install git
git config --global user.name "afinalstone"
git config --global user.email "afinalstone@foxmail.com"
git config --list
2.4 安装curl
sudo apt-get install curl
2.5 安装repo
由于国内网络的原因,这里我使用的是清华源来下载源码和repo工具,在下载好repo工具之后,添加对应权限,然后将repo添加到环境变量中。
mkdir ~/bin
sudo chmod 777 ~/bin
PATH=~/bin:$PATH
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo
chmod a+x ~/bin/repo
export PATH=~/bin:$PATH
三、常见异常
3.1 file=sys.stderr)异常
解决方案:请注意python的版本是python3而不是python2
3.2 mkdir: cannot create directory ‘xxx’: Permission denied
解决方案:赋予文件读写权限
sudo chmod 777 xxx
3.3 /usr/bin/env: ‘python’: No such file or directory
解决方案:查找python3的位置,为其创建链接
whereis python3
sudo ln -s /usr/bin/python3 /usr/bin/python
3.4 Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle
- 解决方案1:可以输入以下指令修改repo仓库更新的地址为国内地址,再尝试repo init
REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
- 解决方案2:还可以将如下内容复制到~/.bashrc里,这样每次系统启动都会自动修改repo仓库更新的地址为国内地址,再尝试repo init
sudo vim ~/.bashrc
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
- 解决方案3:直接修改~/bin/repo文件内容中REPO_URL 的赋值
#REPO_URL = os.environ.get('REPO_URL', None) //注释掉这一行,新增下面一行配置
REPO_URL = os.environ.get('REPO_URL', 'https://mirrors.tuna.tsinghua.edu.cn/git/git-repo')
if not REPO_URL:
#REPO_URL = 'https://gerrit.googlesource.com/git-repo' //注释掉这一行,新增下面一行配置
REPO_URL = 'https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
REPO_REV = os.environ.get('REPO_REV')
if not REPO_REV:
REPO_REV = 'stable'
# URL to file bug reports for repo tool issues.
BUG_URL = 'https://bugs.chromium.org/p/gerrit/issues/entry?template=Repo+tool+issue'
四、使用Repo初始化仓库
4.1 创建Android系统源码存放目录
mkdir /home/stone/workspace/android_aosp16(根据实际情况来新建目录)
cd /home/stone/workspace/android_aosp16
4.2 初始化仓库
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest
具体可以参考清华官网:https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/
五、同步源码
5.1 查看分支情况
cd .repo/manifests
git pull
git branch -a

5.1 获取仓库配置文件.repo
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-9.0.0_r8 //Android9分支
repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-14.0.0_r2 //Android14分支
repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-16.0.0_r2 //Android16分支
这里我选的是16.0.0_r2 这个分支。
5.2 同步源码
repo sync (等待源代码下载完成,长时间等待,若失败请检查网络等情况)
六、搭建编译环境
6.1 安装openJDK8
sudo apt-get update
sudo apt-get install openjdk-8-jdk
6.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
七、编译系统
7.1 设置环境:source build/envsetup.sh
使用 envsetup.sh 脚本初始化环境。请注意,将 source 替换成 .(一个点)可以省去一些字符,这种简写形式在文档中更为常用。
source build/envsetup.sh
7.2 选择目标:lunch
7.2.1 查看产品列表:list_products
lunch --help #寻求帮助
list_products #查看产品列表

7.2.2 查看版本信息:list_releases
list_releases aosp_cf_x86_64_phone

7.2.3 查看变体信息:list_variants
list_variants aosp_cf_x86_64_phone

7.2.4 选择编译目标:lunch
lunch aosp_cf_x86_64_phone bp2a userdebug
7.2.5 编译目标介绍
所有编译目标都采用 BUILD-BUILDTYPE 形式,其中 BUILD 是表示特定功能组合的代号。BUILDTYPE 是以下类型之一:
| user | userdebug | eng |
|---|---|---|
| 仅安装标签为 user 的模块 | 安装标签为 user、debug 的模块 | 安装标签为 user、debug、eng 的模块 |
| 设定属性 ro.secure=1,打开安全检查功能 | 设定属性 ro.secure=1,打开安全检查功能 | 设定属性 ro.secure=0,关闭安全检查功能 |
| 设定属性 ro.debuggable=0,关闭应用调试功能 | 设定属性 ro.debuggable=1,启用应用调试功能 | 设定属性 ro.debuggable=1,启用应用调试功能 |
| 设定属性 ro.kernel.android.checkjni=1,启用 JNI 调用检查 | ||
| 默认关闭 adb 功能 | 默认打开 adb 功能 | 默认打开 adb 功能 |
| 关闭root权限 | 开启root权限 | 开启root权限 |
| 打开 Proguard 混淆器 | 打开 Proguard 混淆器 | 关闭 Proguard 混淆器 |
| 打开 DEXPREOPT 预先编译优化 | 打开 DEXPREOPT 预先编译优化 | 关闭 DEXPREOPT 预先编译优化 |
7.3 源码编译:make
您可以使用 make 编译任何代码。GNU make 可以借助 -j【N】 参数处理并行任务,通常使用的任务数 【N】 介于编译时所用计算机上硬件线程数的 1-2 倍之间。例如,在一台双核 E5520 计算机(2 个 CPU,每个 CPU 4 个内核,每个内核 2 个线程)上,要实现最快的编译速度,可以使用介于 make -j16 到 make -j32 之间的命令。
make -j16
如果编译完一个版本后想重新编译一个,可以使用 make clobber清除之前编译生成的文件。我们编译产生的文件都在 out文件夹下。
7.4 启动模拟器:emulator
编译成功后,输入emulator就可以启动模拟器进入Android系统了
八、如何查看源码的版本
8.1 从代码中查看当前版本,找到如下文件
build\core\version_defaults.mk
8.2 找到关键字 PLATFORM_VERSION
INTERNAL_BUILD_ID_MAKEFILE := $(wildcard $(BUILD_SYSTEM)/build_id.mk)
ifdef INTERNAL_BUILD_ID_MAKEFILE
include $(INTERNAL_BUILD_ID_MAKEFILE)
endif
DEFAULT_PLATFORM_VERSION := PPR1
MIN_PLATFORM_VERSION := PPR1
MAX_PLATFORM_VERSION := PPR1
ALLOWED_VERSIONS := $(call allowed-platform-versions,\
$(MIN_PLATFORM_VERSION),\
$(MAX_PLATFORM_VERSION),\
$(DEFAULT_PLATFORM_VERSION))
ifndef TARGET_PLATFORM_VERSION
TARGET_PLATFORM_VERSION := $(DEFAULT_PLATFORM_VERSION)
endif
ifeq (,$(filter $(ALLOWED_VERSIONS), $(TARGET_PLATFORM_VERSION)))
$(warning Invalid TARGET_PLATFORM_VERSION '$(TARGET_PLATFORM_VERSION)', must be one of)
$(error $(ALLOWED_VERSIONS))
endif
PLATFORM_VERSION.PPR1 := 9
九、系统编译/模块编译
9.1 m / mm / mmm / mmma 命令
// 编译整个安卓系统
function m()
(
_trigger_build "all-modules" "$@"
)
// 编译当前目录下的模块,当前目录下需要有 Android.mk, 否则就往上找最近的 Android.mk 文件
function mm()
(
_trigger_build "modules-in-a-dir-no-deps" "$@"
)
// 编译指定目录下的模块
function mmm()
(
_trigger_build "modules-in-dirs-no-deps" "$@"
)
// 当前目录有修改时,可以用这个命令重新编译
function mma()
(
_trigger_build "modules-in-a-dir" "$@"
)
// 指定目录有修改时,可以用这个命令重新编译
function mmma()
(
_trigger_build "modules-in-dirs" "$@"
)
function _trigger_build()
(
local -r bc="$1"; shift
if T="$(gettop)"; then
_wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
else
>&2 echo "Couldn't locate the top of the tree. Try setting TOP."
return 1
fi
)
十、一些常见模块的编译指令
10.1 完整系统编译
1)进入frameworks的上一级目录
2)source build/envsetup.sh
3)lunch 选择编译的类型
4)使用make命令开始进行系统整编
10.2 kernel模块编译:
1)/kernel/msm-3.18/drivers/misc/qcom/leapmotor/drv_gpio/
2)进入frameworks的上一级目录
3)source build/envsetup.sh
4)lunch 选择编译的类型
5)输入make bootimage 命令开始进行kernel模块的编译
6)根据最终输出信息确认rawprogram-boot.xml文件的路径
7)使用QF进行boot的刷机操作
10.3 frameworks/base模块编译
修改了framework/base目录下面的类代码之后,需要重新编译产生/system/framework/framework.jar相关文件并替换原有的文件。
1)进入frameworks的上一级目录
2)source build/envsetup.sh
3)lunch 选择编译的类型
4)make framework-minus-apex
5)编译完毕,framework.jar文件的生成目录为:/out/target/product/msm***/system/framework
6)使用adb push /out/…路径省略…/system/framework /system/ 替换掉系统中旧的framework.jar相关文件
10.4 framework/base/services模块编译
修改了framework/base/services目录下面的类代码之后,需要重新编译产生/system/framework/services.jar相关文件替换原有的文件。
1)删除/out/target/product/msm***/system/framework目录里面的内容
2)进入frameworks的上一级目录
3)source build/envsetup.sh
4)lunch 选择编译的类型
5)make services
6)编译完毕,services.jar文件的生成目录为:/out/target/product/msm***/system/framework
7)adb root、adb remount获取系统硬盘读写权限
8)使用adb push /out/…路径省略…/system/framework /system/ 替换掉系统中旧的services.jar相关文件
9)重启系统或者killall system_server进程
10.5 framework/base/core/res/res模块编译
修改了framework/base/core/res/res目录下面的资源之后,需要重新编译产生/system/framework/framework-res.apk文件并替换原有的文件。
1)进入frameworks的上一级目录
2)source build/envsetup.sh
3)lunch 选择编译的类型
4)make framework-res
6)编译完毕,framework-res.apk文件的生成目录为:/out/target/product/msm***/system/framework
7)adb root、adb remount获取硬盘读写权限
8)使用adb push /out/…/system/framework/framework-res.apk /system/framework/ 替换掉系统中旧的framework-res.apk
adb push /out/…/product/overlay/framework-res__auto_generated_rro_product.apk /product/overlay/
adb push /out/…/vendor/overlay/framework-res__auto_generated_rro_product.apk /vendor/overlay/
10.6 SystemUI模块编译
修改了framework/base/packages/SystemUI目录下面的代码和资源之后,需要重新编译产生SystemUI文件替换原有的文件。
1)进入frameworks的上一级目录
2)source build/envsetup.sh
3)lunch 选择编译的类型
4)make SystemUI
5)编译完毕,根据最终输出信息确认SystemUI.apk件的路径
6)out/target/product/msm***/system_ext/priv-app/SystemUI/SystemUI.apk
7)使用adb push /out/…路径省略… /system_ext/priv-app/SystemUI /system_ext/priv-app/ 替换掉系统中旧的SystemUI模块
10.7 Launcher3模块编译
修改了framework/base/packages/SystemUI目录下面的代码和资源之后,需要重新编译产生SystemUI文件替换原有的文件。
1)进入frameworks的上一级目录
2)source build/envsetup.sh
3)lunch 选择编译的类型
4)make Launcher3QuickStep
5)编译完毕,根据最终输出信息确认SystemUI.apk件的路径
6)out/target/product/msm***/system_ext/priv-app/Launcher3QuickStep/Launcher3QuickStep.apk
7)使用adb push /out/…路径省略… /system_ext/priv-app/Launcher3QuickStep /system_ext/priv-app/ 替换掉系统中旧的SystemUI模块
10.8 其他常见编译指令
make bootimage #kernel模块
make libinputflinger #input模块
make selinux_policy #selinx模块
make idegen #idegen模块
💡 技术无价,赞赏随心
写文不易,如果本文帮你避开了“八小时踩坑”,或者让你直呼“学到了!”
欢迎扫码赞赏,让我知道这篇内容值得!
7051


从下载系统源码到编译系统镜像&spm=1001.2101.3001.11974&articleId=125891484&d=1&t=3&u=a0f463a048f74acaa4e54ee98a3a4bbf)

被折叠的 条评论
为什么被折叠?



