文章目录

  • 环境准备
  • 下载源码
    • 下载 repo 工具
    • repo init 原理
    • 初始化仓库
    • 选择同步指定分支
    • 同步代码
    • 切换源
  • 编译代码
  • 遇到的错误

很多人都写,如何在 Ubuntu 下编译 Android 源码。今天,标新立异一次。

环境准备

本次编译基于 Arch 系统。需要安装些开发组件,比较容易。

sudo pacman -S base-devel

至于 jdk 环境,有人说需要安装 open jdk 8 ,但是看 Android 源码中,prebuilts 目录下有提供 jdk 目录。

下载源码

国内下载源码,一般通过清华大学 AOSP 镜像或 中国科技大学 AOSP 镜像。他们也提供具体教程。
尝试过很多次,总结出一个比较实用的下载方式。

下载 repo 工具

Android 源码仓库有数百个,因此 google 使用 python 写 repo 用于管理其源码库。

# 在用户目录下创建bin目录,并进入
mkdir ~/bin
cd ~/bin
# 将bin目录加入到环境变量
vim ~/.bash_profile
# 添加下面配置
export PATH=$PATH:~/bin
# 使配置的环境变量生效
source ~/.bash_profile
# 下载 repo 工具,有两种方式,我使用第二种
# 方式一,清华大学 aosp 提供的下载方式
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo -o repo
chmod +x repo  # 添加可执行权限
# 方式二
git clone https://mirrors.tuna.tsinghua.edu.cn/git/git-repo
ln -sf git-repo/repo repo

检查 repo 版本。

repo init 原理

在执行 repo init 命令之前,有必要了解下 repo 的工作原理。可以更好的知道,后买每步操作的意义,如无兴趣,可跳过。

repo 本身只是一个 python 文件,但是他的功能很多,远远超出一个文件所能承受的能力。因此,在执行 repo init 命令时,它首先会 clone 一份自己的仓库,也即 REPO_URL 所指定的地址。其次再去从 -u 指定的 url 中获取源码仓库的清单文件。

REPO_URL = os.environ.get('REPO_URL', None)
if not REPO_URL:REPO_URL = 'https://gerrit.googlesource.com/git-repo'

这段代码表示,repo 默认从 goole 的服务器同步。解决方案有两种。

# 方式一,添加 REPO_URL 环境变量
vim ~/.bash_profile # 添加下面的内容
export REPO_URL=https://mirrors.tuna.tsinghua.edu.cn/git/git-repo
source ~/.bash_profile # 使配置生效
# 方式二
# 执行 init 命令时,手动指定 repo url
repo init --repo-url=https://mirrors.tuna.tsinghua.edu.cn/git/git-repo -u manifest

但是,之前下载 repo 工具时,已经 clone 过 git-repo 仓库。是可以复用的。

mkdir ~/aosp # 创建 Android 源码目录
cd ~/aosp    # 后面所有命令都在此目录下执行
mkdir .repo  # 创建 .repo 目录,其实 init 执行时也会创建
ln -sf ~/bin/git-repo .repo/repo  # 创建快捷方式,指向之前的 repo 仓库

此方案,建议熟悉 linux 命令的同鞋使用,如果你是一只 linux 小白或菜鸟,或许有难度。

初始化仓库

很多教程,有介绍使用 -b 参数,表示需要同步的代码版本,如 -b android-4.0.1_r1 。想说,查找能同步的代码版本列表,是个难题。因为我们打不开这个地址 https://source.android.com/setup/start/build-numbers#source-code-tags-and-builds (有梯子的可以嘲笑下)。或许有哪些热心的小伙伴整理过。但是,完全没有必要使用 -b

# 此处假装,你已经知道了配置 REPO_URL 或复用 git-repo 仓库。
# 不推荐使用 http 协议,因此只能使用科大的源
repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest

下面是复用了 git-repo 仓库的截图。

也许会提示没有配置 git 用户信息,使用下面两条命令即可。

git config --global user.name " Your Name "
git config --global user.email " Your Email"
git config -l # 查看配置的结果

选择同步指定分支

刚说了,没有必要在 init 时,指定 -b 参数。因为有更便捷的方式。

cd .repo/manifests
git branch -a # 查看全部版本列表
# 如同步 android-11.0.0_r27
git checkout -b android-11.0.0_r27 origin/android-11.0.0_r27
cd ../../

如果后面还需要同步其他分支的代码时,还可以用这种方式。

同步代码

执行下面命令,静等即可。建议选择白天,如果网速可以。两三小时就可以下载完成。

repo sync # 上面的命令可能会执行失败,因此需要一个自动继续执行的脚本
vim ~/bin/auto-repo # 添加下面的内容#!/bin/bash
echo "=========start repo sync=========="
repo syncwhile [ $? != 0 ]
doecho "========sync failed,re-sync again=========="sleep 2repo sync
done

然后使用 auto-repo 同步仓库即可。下载完成后如图:

切换源

如果之前已同步过 aosp 代码,现在去要切到清华或科大的源。只需修改 manifests 的仓库地址。

[core]repositoryformatversion = 0filemode = true
[filter "lfs"]smudge = git-lfs smudge --skip -- %fprocess = git-lfs filter-process --skip
[remote "origin"]# 清华地址# url = https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest# 可科大地址url = git://mirrors.ustc.edu.cn/aosp/platform/manifestfetch = +refs/heads/*:refs/remotes/origin/*
[branch "default"]remote = originmerge = refs/heads/master
[branch "android-11.0.0_r27"]remote = originmerge = refs/heads/android-11.0.0_r27

编译代码

# 初始化编译环境
source build/envsetup.sh
# 选择要编译的目标版本
lunch#-----------以下是终端显示内容----------------
You're building on LinuxLunch menu... pick a combo:1. aosp_arm-eng2. aosp_arm64-eng3. aosp_blueline-userdebug4. aosp_blueline_car-userdebug5. aosp_bonito-userdebug6. aosp_bonito_car-userdebug7. aosp_bramble-userdebug8. aosp_car_arm-userdebug9. aosp_car_arm64-userdebug10. aosp_car_x86-userdebug11. aosp_car_x86_64-userdebug12. aosp_car_x86_64_app-userdebug13. aosp_cf_arm64_auto-userdebug14. aosp_cf_arm64_phone-userdebug15. aosp_cf_x86_64_phone-userdebug16. aosp_cf_x86_auto-userdebug17. aosp_cf_x86_phone-userdebug18. aosp_cf_x86_tv-userdebug19. aosp_coral-userdebug20. aosp_coral_car-userdebug21. aosp_crosshatch-userdebug22. aosp_crosshatch_car-userdebug23. aosp_flame-userdebug24. aosp_flame_car-userdebug25. aosp_redfin-userdebug26. aosp_sargo-userdebug27. aosp_sunfish-userdebug28. aosp_trout_arm64-userdebug29. aosp_trout_x86-userdebug30. aosp_x86-eng31. aosp_x86_64-eng32. arm_krait-eng33. arm_v7_v8-eng34. armv8-eng35. armv8_kryo385-eng36. beagle_x15-userdebug37. beagle_x15_auto-userdebug38. car_x86_64-userdebug39. db845c-userdebug40. fuchsia_arm64-eng41. fuchsia_x86_64-eng42. hikey-userdebug43. hikey64_only-userdebug44. hikey960-userdebug45. hikey960_tv-userdebug46. hikey_tv-userdebug47. pixel3_mainline-userdebug48. poplar-eng49. poplar-user50. poplar-userdebug51. qemu_trusty_arm64-userdebug52. silvermont-eng53. uml-userdebug54. yukawa-userdebug55. yukawa_sei510-userdebugWhich would you like? [aosp_arm-eng] 31============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=11
TARGET_PRODUCT=aosp_x86_64
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_ARCH=x86_64
TARGET_ARCH_VARIANT=x86_64
TARGET_2ND_ARCH=x86
TARGET_2ND_ARCH_VARIANT=x86_64
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-5.10.9-arch1-1-x86_64-Arch-Linux
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=RQ1A.210105.003
OUT_DIR=out
PRODUCT_SOONG_NAMESPACES=device/generic/goldfish device/generic/goldfish-opengl hardware/google/camera hardware/google/camera/devices/EmulatedCamera device/generic/goldfish device/generic/goldfish-opengl
============================================

由于使用模拟器测试,因此选择 x86x86_64 的编译目标。我选择 31 aosp_x86_64-eng

在执行,最后的编译操作之前,需要了解下电脑的 CPU 核心数量。

cat /proc/cpuinfo | grep 'process'
# 下面表示 4 核
processor   : 0
processor   : 1
processor   : 2
processor   : 3

最后,使用 make -j4 执行编译。

我的电脑性能比较差,如图。耗时约 10 小时。建议睡前执行。万一不出错的情况下,睡醒就完成了。

编译成功之后,使用 emulator 命令启动模拟器。

遇到的错误

编译过程中,遇到两个 so 文件没有,通过添加快捷方式引用到系统中,其他新版本的 so 。

libncurses.so.5 -> /usr/lib64/libncursesw.so.6

libtinfo.so.5 -> libtinfo.so.6

sudo ln -sf /usr/lib64/libncursesw.so.6 /usr/lib/libncurses.so.5
sudo ln -sf /usr/lib/libtinfo.so.6 /usr/lib/libtinfo.so.5

遇到 command not found 错如,如 zip ,直接安装即可。

sudo pacman -S zip

其他错误还未遇到。

Arch 编译 Android 11 源码相关推荐

  1. 修改编译Android 11源码时发现报can‘t find symble的错误

    在/home/wq/android_11_source_repo/packages/apps/Contacts/src/com/android/contacts/activities/ContactS ...

  2. quado编辑Android代码步骤,Ubuntu11.10下编译android内核源码

    编译android源码时并不会自动编译android内核,因此需要手动编译这部分代码.编译内核所用的时间比编译android源码要少得多,只需一会就编译完成. 1.确定内核版本 进入存放android ...

  3. Ubuntu18.04 编译Android 10源码 并烧录源码到pixel3的避坑指南

    Ubuntu18.04 编译Android 10源码 并烧录源码到pixel3的避坑指南 实验环境 下载Android源码树 在pixel3上安装手机驱动版本 编译Android源码 Android ...

  4. Android 11源码 Framework修改默认usb连接模式为MTP模式

    Android 11源码 Framework修改默认usb连接模式为MTP模式 Android 11源码 Framework修改默认usb连接模式为MTP模式 修改Framework层源码 编译修改后 ...

  5. Android 11源码——安全策略SELinux关闭

    Android 11源码 安全策略SELinux关闭 SELinux介绍 背景 关闭Android的SELinux 临时关闭 永久关闭 SELinux介绍 作为 Android 安全模型的一部分,An ...

  6. 编译Android系统源码--搭建环境

    下载Ubantu14 Google官方建议使用Ubuntu 14 下载路径 双系统安装参考 下载VMware 建议:VMware Workstation 16 Pro 下载路径 下载Android系统 ...

  7. Win10 Linux子系统编译Android系统源码

    一.前言 听说win10合入了Linux作为一个子系统,起初只以为是类似虚拟机的实现机制,想必对性能的提高也很有限.但是稍微了解却发现是并不是虚拟机的那种机制,而是可以完全发挥Host机器的性能,这无 ...

  8. Ubuntu编译Android系统源码(msm8909_android5.1.1)

    安装工具 下载Android源码需要git,repo等工具,启动repo是Google写的一个专门用于下载Android源码的工具,主要调用了git. 科普一下git与repo的区别 Git:Git是 ...

  9. android 11源码开机更改屏幕方向触摸方向

    目录 1.修改屏幕方向 2.配置触摸方向 3.修改触摸相关配置源码 修改屏幕方向以后需要将系统的触摸方向一并配置,否则可能造成坐标错乱情况,修改完毕后重新编译源码烧录后验证结果 1.修改屏幕方向 这里 ...

最新文章

  1. 刨根问底: Kafka 到底会不会丢数据?
  2. 网页版python叫什么-用Python爬网页需要了解什么背景知识?
  3. Servlet程序入门
  4. 如何增加儿童产品中的趣味性?
  5. gitlab常规维护命令
  6. mysql分片库分页查询_mysql数据库分页查询优化
  7. linux新建用户代码,Linux_用dsadd添加用户的代码,描述: 此工具命令将一些具体 - phpStudy...
  8. python保存模型 特征_Pytorch提取模型特征向量保存至csv的例子
  9. 使用 .NET 升级助手将.NET Framework应用迁移到.NET 5
  10. 纯前端实现人脸识别-提取-合成
  11. JavaWeb那些事儿(一)--为什么做java的web开发使用struts2,springMVC和spring这样的框架
  12. 爆销产品标题怎么写_7个让销量暴增的商品标题文案套路
  13. 关于中值滤波算法,以及C语言实现
  14. SAM2195和SAM2695 和SAM5704硬音源设备在三四十年前MIDI技术刚刚起步之时
  15. Django模板 render_to_string与render
  16. 耶鲁大学 博弈论(Game Theory) 笔记1
  17. 使用微软官方工具制作Windows10的U盘启动盘
  18. MySQL重安装失败
  19. JAVA电子竞技赛事管理系统计算机毕业设计Mybatis+系统+数据库+调试部署
  20. 苹果ttc转ttf_ttf转ttc字体格式工具 ttctools

热门文章

  1. 命令行mysql回退_mysql-管理事务 - osc_mi06gsf5的个人空间 - OSCHINA - 中文开源技术交流社区...
  2. C语言的一小步—————— 一些小项目及解析
  3. css3中做3D导航栏
  4. JavaScript入门记录
  5. 罗定c语言,2021——云浮罗定C粉粘结剂
  6. 11、Linux系统基础原理、进程管理工具、任务计划
  7. Linux系统中的进程与计划任务管理
  8. 《计算机多媒体技术》课程标准
  9. 教你怎么用Python和Qt5编写中国象棋AI版——简明易懂版
  10. 趋势科技员工将68000名客户信息出售给犯罪分子