Ubuntu14.04环境搭建

电脑为笔记本,CPU i7-10750H,16G内存

虚拟机使用VMware Workstation 15 Pro

操作系统为Ubuntu 64 位 14.04

AOSP比较大,安装虚拟机的时候预留250G+的空间,8G内存。具体的安装请自行进行。

根据你要编译的系统版本,选择相应的 Ubuntu版本:

  • Android 6.0 (Marshmallow) – AOSP master:Ubuntu 14.04 (Trusty)
  • Android 2.3.x (Gingerbread) – Android 5.x (Lollipop):Ubuntu 12.04 (Precise)
  • Android 1.5 (Cupcake) – Android 2.2.x (Froyo):Ubuntu 10.04 (Lucid)

Ubuntu 环境准备

  • 设置root密码:sudo passwd

  • 安装VMware Tools,方便与window交互
    sudo apt-get install open-vm-tools-desktop -y
    然后重启
    sudo reboot

  • 替换国内源,见“镜像替换国内阿里”,apt-get intstall 会快很多
    重启,然后
    sudo apt-get update

  • 安装编译源码时候的第三方库(14.04)
    sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g+±multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev libgl1-mesa-dev libxml2-utils xsltproc unzip

    注意:如需使用 SELinux 工具进行政策分析cank,您还需要安装 python-networkx 软件包。
    注意:如果您使用的是 LDAP 并且希望运行 ART 主机测试,还需要安装 libnss-sss:i386 软件包。

  • 安装adb sudo apt-get install android-tools-adb

  • python 安装 (推荐采用3.6.8,使用3.7.X的版本存在import ssl的错误,理论上3.6+即可)

    参考“python3.6.8 安装”

  • 分区工具,可以给虚拟机多分配些内存。现在系统安装号默认是12G的物理内存加8G虚拟内存。在编译过程中发现内存不够用(出现堆溢出,内存溢出),增加了6G虚拟内存,总共12G+14G内存。(推测和make -j8相关,如果内存不够的话j后面的数字小些)
    sudo apt-get install gparted

镜像替换国内阿里

用你熟悉的编辑器打开:

/etc/apt/sources.list

例如:sudo gedit /etc/apt/sources.list

替换默认的

http://archive.ubuntu.com/

https://mirrors.aliyun.com/

以Ubuntu 14.04.5 LTS为例,最后的效果如下:

deb https://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiversedeb https://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiversedeb https://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse## Not recommended
# deb https://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiv

Python3.8.3 安装

安装SSL:

apt-get install openssl
apt-get install libssl-dev

安装Python3.6

wget https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tgz
tar -zxvf Python-3.8.3.tgz
cd Python-3.8.3

修改Moudles/Setup (该目录在Python-3.6.8目录下),把以下行的注释去掉

SSL=/usr/local/ssl
_ssl _ssl.c \-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \-L$(SSL)/lib64 -lssl -lcrypto

编译安装

./configure
make && make install

切换版本,使用该方式比较简单:

查看python版本的优先级(初次没有,报错的话正常)

sudo update-alternatives --config python

以通过update-alternatives来设置默认python版本, 最后的参数1,2是优先级,数字越大优先级越高,比如经过如下设置后,在终端输入python,显示的就是3.6的版本了。

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.8 3

注意:自行编译安装的Python是在/usr/local/bin/目录下

Android源码下载更新

Android 源代码树位于由 Google 托管的 Git 代码库中。Git 代码库中包含 Android 源代码的元数据,其中包括与对源代码进行的更改以及更改日期相关的元数据。下面介绍了如何下载特定 Android 代码源代码树。

安装 Repo

Repo 是一款工具,可让您在 Android 环境中更轻松地使用 Git。要安装 Repo,请执行以下操作:

mkdir ~/bin # 创建文件夹
PATH=~/bin:$PATH #设置环境变量
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo #下载repro 到/bin/repo文件里
chmod a+x ~/bin/repo # 给repo 文件权限

更新 Repo

repo的运行过程中会尝试访问官方的git源更新自己,如果想使用tuna的镜像源进行更新,可以将如下内容复制到你的~/.bashrc

export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'

source ~/.bashrc

替换国内清华的镜像

用第三方工具打开repo文件,替换国内清华的镜像

https://android.googlesource.com/ 全部使用 https://aosp.tuna.tsinghua.edu.cn/ 代替即可。

由于使用 HTTPS 协议更安全,并且更便于灵活处理,所以强烈推荐使用 HTTPS 协议同步 AOSP 镜像。

由于 AOSP 镜像造成CPU/内存负载过重,限制了并发数量,因此建议:

  1. sync的时候并发数不宜太高,否则会出现 503 错误,即-j后面的数字不能太大,建议选择4。
  2. 请尽量选择流量较小时错峰同步。

使用每月更新的初始化包(推荐使用该方法)

由于首次同步需要下载约 30GB 数据,过程中任何网络故障都可能造成同步失败,强烈建议使用初始化包进行初始化。

下载 https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar (可以使用三方工具下载,支持断点续传),下载完成后记得根据 checksum.txt 的内容校验一下。

由于所有代码都是从隐藏的 .repo 目录中 checkout 出来的,所以我们只保留了 .repo 目录,下载后解压 再 repo sync 一遍即可得到完整的目录。

使用方法如下:

wget -c https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下载初始化包
tar xf aosp-latest.tar
cd AOSP   # 解压得到的 AOSP 工程目录
# 这时 ls 的话什么也看不到,因为只有一个隐藏的 .repo 目录 ,可以使用ls -al
repo sync # 正常同步一遍即可得到完整目录
# 或 repo sync -l 仅checkout代码

此后,每次只需运行 repo sync 即可保持同步。

可以选择该命令同时发起四个并发请求,之所以选择4是因为清华的镜像的并发请求的限制的上限就是4个。

repo sync -j4

注意:出现奇奇怪怪的bug,可以重复执行一下,很多bug是由网络原因造成的。

一定要确保文件都下载成功,否则编译时会出现一些问题。

(强烈多同步几次,确保同步没有错误。在第一次同步完成后再进行一次同步,如果第一次没问题的话速度会很快的)

也可以写个脚本,自动执行

#!/bin/bash
repo sync -j4
while [ $? = 1 ]; do
echo "================sync failed, re-sync again ====="
sleep 3
repo syncdone

传统初始化方法(不推荐)

建立工作目录:

mkdir WORKING_DIRECTORY
cd WORKING_DIRECTORY

初始化仓库:

repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest

如果提示无法连接到 gerrit.googlesource.com,请参照“更新 Repo”。

同步源码树(以后只需执行这条命令来同步):

repo sync

切换版本

如果需要某个特定的 Android 版本:

repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-10.0.0_r10

默认是master,是android11版,我选择了Android10。我使用Android10

查看版本:cd .repo/manifests && git branch -a

Android 编译

设置文件描述符限制

先用$ ulimit查看,如果是无限制就不需要修改

可能默认限制的同时打开的文件数量很少,不能满足编译过程中的高并发需要,因此需要在shell中运行命令:

$ ulimit -S -n 2048

避免OutOfMemoryError(内存足够的话不会出现)

Android10之前的解决方案:OutOfMemoryError: Java heap space解决方案

export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m"

Android10以后:

export _JAVA_OPTIONS="-Xmx4096m"

或者增加虚拟机内存或是swp空间

环境设置

在源码根目录下调用下面的命令:

$ source build/envsetup.sh

选择设备

在命令行输入下面的命令选择打算编译的源码类型

Lunch menu... pick a combo:1. aosp_arm-eng2. aosp_arm64-eng3. aosp_blueline-userdebug4. aosp_bonito-userdebug5. aosp_car_arm-userdebug6. aosp_car_arm64-userdebug7. aosp_car_x86-userdebug8. aosp_car_x86_64-userdebug9. aosp_cf_arm64_phone-userdebug10. aosp_cf_x86_64_phone-userdebug11. aosp_cf_x86_auto-userdebug12. aosp_cf_x86_phone-userdebug13. aosp_cf_x86_tv-userdebug14. aosp_crosshatch-userdebug15. aosp_marlin-userdebug16. aosp_sailfish-userdebug17. aosp_sargo-userdebug18. aosp_taimen-userdebug19. aosp_walleye-userdebug20. aosp_walleye_test-userdebug21. aosp_x86-eng22. aosp_x86_64-eng23. beagle_x15-userdebug24. fuchsia_arm64-eng25. fuchsia_x86_64-eng26. hikey-userdebug27. hikey64_only-userdebug28. hikey960-userdebug29. hikey960_tv-userdebug30. hikey_tv-userdebug31. m_e_arm-userdebug32. mini_emulator_arm64-userdebug33. mini_emulator_x86-userdebug34. mini_emulator_x86_64-userdebug35. poplar-eng36. poplar-user37. poplar-userdebug38. qemu_trusty_arm64-userdebug39. uml-userdebugWhich would you like? [aosp_arm-eng] 21

根据后缀可以判断出使用的场景如下:

类型 用途
user 权限少,用于刷机使用
userdebug 和“user”类似,但可以root,并且可以调试
eng 具有开发配置,并且有额外的调试工具

根据需要选择对应的类型,比如我选择“21”。(推荐采用x86的,模拟器速度快一些)

============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=10
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-4.4.0-148-generic-x86_64-Ubuntu-14.04.6-LTS
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=QP1A.191105.003
OUT_DIR=out
============================================

开始编译

清理msk

make clobber

为了加快编译的速度,最好并发来编译(并发数不建议太高,可能会造成内存不足,磁盘不足等问题)

make -j8

编译结束以后,会显示下面的日志:

#### build completed successfully (02:00:35 (hh:mm:ss)) ####

错误FAILED: system-qemu.img

============================================
wildcard(out/target/product/generic_x86/clean_steps.mk) was changed, regenerating...
[ 99% 11311/11312] Create system-qemu.img now
FAILED: out/target/product/generic_x86/system-qemu.img
/bin/bash -c "(export SGDISK=out/host/linux-x86/bin/sgdisk SIMG2IMG=out/host/linux-x86/bin/simg2img;      device/generic/goldfish/tools/mk_combined_img.py -i out/target/product/generic_x86/system-qemu-config.txt -o out/target/product/generic_x86/system-qemu.img)"File "device/generic/goldfish/tools/mk_combined_img.py", line 48print "'%s' cannot be converted to int" % (line[2])^
SyntaxError: invalid syntax

若出现该错误,切换python2.7:sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 10

启动模拟器

emulator

KVM错误

emulator: ERROR: x86 emulation currently requires hardware acceleration!
Please ensure KVM is properly installed and usable.CPU acceleration status: KVM is not installed on this machine (/dev/kvm is missing).
egrep -c '(vmx|svm)' /proc/cpuinfo

先使用该指令查看是否支持虚拟化,如果不支持的话在虚拟机的CPU选项中打开即可。

sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils
sudo adduser `id -un` libvirtd
sudo adduser `id -un` kvm#检查是否安装成功
sudo kvm-ok

模拟器黑屏

emulator -partition-size 4096 -kernel ./prebuilts/qemu-kernel/x86/4.9/kernel-qemu2

通过使用kernel-qemu-armv7内核 解决模拟器等待黑屏问题.而-partition-size 4096则是解决警告: system partion siez adjusted to match image file (3083 MB > 800 MB)

模拟器启动后崩溃

VMware: vmw_ioctl_command error Invalid argument.
Aborted (core dumped)

关闭硬件加速

export SVGA_VGPU10=0
echo "export SVGA_VGPU10=0" >> ~/.bashrc
source ~/.bashrc

Android Studio查看源码

编译源码idegen模块

mmm development/tools/idegen/

这行命令的意思是编译idegen这个模块项目,然后生成idegen.jar文件。

编译结束以后,会显示下面的日志:

[100% 11109/11109] Install: out/host/linux-x86/framework/idegen.jar#### build completed successfully (01:26 (mm:ss)) ####

mmm指令就是用来编译指定目录。通常来说,每个目录只包含一个模块。

  - croot: Changes directory to the top of the tree.- m: Makes from the top of the tree.- mm: Builds all of the modules in the current directory.- mmm: Builds all of the modules in the supplied directories.- cgrep: Greps on all local C/C++ files.- jgrep: Greps on all local Java files.- resgrep: Greps on all local res/*.xml files.- godir: Go to the directory containing a file.

生成AS配置文件

接着执行如下脚本:

development/tools/idegen/idegen.sh

这行命令的意思是在根目录生成对应的android.ipr、android.iml IEDA工程配置文件。

等待片刻得到类似如下信息说明OK:

Read excludes: 16ms
Traversed tree: 61259ms

警告1:

emulator: WARNING: Couldn't find crash service executable /Volumes/android/aosp/prebuilts/android-emulator/darwin-x86_64/emulator64-crash-service

这个警告一般没影响,是模拟器崩溃时进行处理的程序,对使用者意义不是很大。

警告2:

emulator: WARNING: system partition size adjusted to match image file (3083 MB > 800 MB)

调整系统分区大小以匹配图像文件,执行命令:emulator -partition-size 4096。(可以不管)

推荐采用x86的,模拟器速度快一些。我这个机器启动arm架构的模拟器花费很长时间。

导入源码

启动Android Studio,然后选择打开一个已存在的Android Studio工程,选择源码根目录的android.ipr,经过的加载过程以后,Android 源码就已经成功的加载到了Android Studio中。

OK,至此我们就完成了下载AOSP并编译导入Android Studio的完整过程。

参考:

1.https://source.android.google.cn/setup/start

2.https://blog.csdn.net/baidu_32237719/article/details/105715371

欢迎关注我的公众号,持续分析优质技术文章

Android10源码AOSP(从Ubuntu14.04环境搭建到下载编译运行)相关推荐

  1. (01)ORB-SLAM2源码无死角解析-(01) 环境搭建,demo运行,ROS一键安装_清除各种疑难杂症

    讲解关于slam一系列文章汇总链接:史上最全slam从零开始,针对于本栏目讲解的(01)ORB-SLAM2源码无死角解析-接如下(本文内容来自计算机视觉life ORB-SLAM2 课程课件): (0 ...

  2. (02)Cartographer源码无死角解析-(01) 环境搭建,demo运行,ROS一键安装_清除各种疑难杂症

    讲解关于slam一系列文章汇总链接:史上最全slam从零开始,针对于本栏目讲解(02)Cartographer源码无死角解析链接如下: (02)Cartographer源码无死角解析-(00)目录_最 ...

  3. Windows Java JavaFX IntelliJ IDEA 开发环境搭建 创建工程 编译运行 打包分发 自定义运行时

    博文目录 文章目录 本文说明 JavaFX 简单说明 JavaFX 版本说明 JavaFX 与 JDK 的关系 JavaFX 与 JDK Modular (JDK 9 模块化系统) JavaFX 模块 ...

  4. 手游无间狱完整源码(开发+运维+环境搭建+GM命令等)

    中文名:无间狱 版 本:1.0 大 小:180.52 MB 类 型:角色扮演 游戏介绍:无间狱 游戏背景是起源于东方武侠世界中最为黑暗的时期,到处出现暗杀.武侠争霸的年代. 无间江湖 设有无间狱榜,一 ...

  5. Ubuntu16.04编译Android10源码并刷机Pixel

    一.环境描述 电脑配置: 系统:Windows10 内存:16GB 硬盘:500GB 软件环境: VirtualBox虚拟机 虚拟配置: 系统:Ubuntu16.04.7 内存:9GB 硬盘:300G ...

  6. Android10源码编译报错ninja: build stopped: subcommand failed处理

    1.背景说明 虚拟机:wsl 2.0 Ubuntu18.04 虚拟机配置:8G内存,1T存储 软件版本:Android10源码,无任何修改 报错内容:编译至97%时,ninja编译中断,ninja: ...

  7. 安卓源码AOSP下载使用的正确姿势

    安卓源码AOSP下载使用的正确姿势 从同步源码到编译完成,整个过程应至少准备200G空间. 编译时需要的内存数与编译线程数相关,博主实测比较极限的配置是4核8G,超过这个范围将触发swap交换导致编译 ...

  8. Linux安装minigui教程,MiniGui开发:Ubuntu14.04环境安装qvfb工具

    在Ubuntu14.04上搭建MiniGui开发环境,需要依赖qvfb工具. minigui官网提供了qvfb的安装包,下载连接 http://www.minigui.org/en/download/ ...

  9. 玩转Android10源码开发定制(二)之基于Pixel 3手机超级详细演示recovery刷机

    玩转Android10源码开发定制(二)之基于Pixel 3手机超级详细演示recovery刷机 本节主要内容: Pixel 3通过fastboot方式刷入Android 11.0官方工厂镜像 Pix ...

最新文章

  1. 数据结构与算法:算法简介
  2. AttributeError:module 'numbers' has no attribute 'Integral'.
  3. Asp.net Web API实战
  4. c语言fopen函数打不开,[讨论]用fopen函数无法打开文件有哪几种情况
  5. cass3d基础版_v1.1_仪表不凡说表:N厂“一劳永逸”V11版实至名归!
  6. 调用Hybris API时遇到的错误消息Cannot find user with uid如何解决
  7. Java集合List、Set、Map
  8. 【Android】LMK 工作机制
  9. 洛奇英雄转无法读取游戏服务器状态,洛奇英雄传无法连接服务器认证失败处理方法讲解...
  10. C#通过Socket在网络间发送和接收图片的演示源码
  11. Java多线程——同步容器类
  12. Lecture 1:强化学习简介
  13. 按键精灵 android 模拟器 登录,按键精灵怎么设置安卓模拟器后台
  14. IIS DirectoryEntry
  15. delphi阿里云短信(支持SendSms短信发送、SendBatchSms短信批量发送、QuerySendDetails查询短信发送记录),D7~XE10可用
  16. 3个文件,不到400行代码打造经典吃豆人小游戏
  17. 30 多名腾讯工程师,七天七夜拯救微盟的奇迹行动
  18. 开源电子书项目FBReader初探(三)
  19. 阿里百川的用户反馈(Android)
  20. akka kafka java_Jay Kreps谈基于Apache Kafka和Kafka Streams的分布式流处理

热门文章

  1. 在投射和录制时显示敏感信息时,免root授权确认框
  2. 2014网易游戏实习生面试体验
  3. 计算机学院考勤管理办法,学生考勤管理规定
  4. 实现文本框以及其背景(根据输入字数)动态拉伸
  5. kaggle Notebook Threw Exception问题解决
  6. HTML5之 Microdata微数据
  7. on device trainning
  8. 每周分享第 38 期
  9. 红帽linux安装intel(R) wifi link 5100 AGN总结
  10. 云端服务器上传本地项目