Android7.0源码编译运行指南

2024-02-17 08:08

本文主要是介绍Android7.0源码编译运行指南,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://blog.csdn.net/HardReceiver/article/details/52650303

编译环境:Ubuntu 16.04

镜像文件:清华大学AOSP镜像 Android7.0

一、源码下载

1 镜像地址

清华大学AOSP(Android Open Source Project)
https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/
  • 1
  • 2

2 过程摘录

下载Repo 工具
mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
  • 1
  • 2
  • 3
  • 4
使用每月更新的初始化包

由于首次同步需要下载 24GB 数据,过程中任何网络故障都可能造成同步失败,我们强烈建议您使用初始化包进行初始化。下载 https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar,下载完成后记得根据 checksum.txt 的内容校验一下。由于所有代码都是从隐藏的 .repo 目录中 checkout 出来的,所以我们只保留了 .repo 目录,下载后解压 再 repo sync 一遍即可得到完整的目录。

使用方法如下:

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

此后,每次只需运行 repo sync 即可保持同步。 我们强烈建议您保持每天同步,并尽量选择凌晨等低峰时间

3 解决repo sync下载代码时Failed connect错误的办法:

下载android代码时错误提示:error: Failed connect to android.googlesource.com:443 
解决方法:编辑/etc/hosts文件

 vim /etc/hosts
  • 1

增加下面内容,保存

173.194.72.82 http://www.googlesource.com
173.194.72.82 android.googlesource.com
37.61.54.158 cache.pack.google.com
173.194.74.82 gerrit.googlesource.com
  • 1
  • 2
  • 3
  • 4

4 其他

  • 初始化包大小21.4G,我的网速5-6M/s,下载时间基本一个小时左右。
  • Android7.0,aosp目录编译完之后的大小是54.6G,安装Ubuntu时要注意留够足够的空间,推荐100G+以上。

二、源码编译

1 搭建编译环境

  • openJdk is needed !我自己平时开发用的是JDK1.8,这里就要安装一下openjdk,并且更改环境变量。
sudo apt-get update
sudo apt-get install openjdk-7-jdk
  • 1
  • 2

但是如果你是刚安装完Ubuntu,那么就可以跳过此步骤,Ubuntu自带openjdk

  • required package
sudo apt-get install bison g++-multilib git gperf libxml2-utils make python-networkx zlib1g-dev:i386 zip
  • 1
  • 因为源码包含C 和 C++代码,gcc为必须
sudo apt-get install gcc
  • 1
  • Setting up ccache , 使用ccache

ccache是一个编译器ccache用于C和C++。可以让构建更加快速。在源代码的根目录处,做如下操作:

$export USE_CCACHE=1
$export CCACHE_DIR=/<path_of_your_choice>/.ccache
$prebuilts/misc/linux-x86/ccache/ccache -M 50G</path_of_your_choice>
  • 1
  • 2
  • 3

建议的cache大小为50-100G

可以使用如下操作来查看使用的ccache大小

$watch -n1 -d prebuilts/misc/linux-x86/ccache/ccache -s
  • 1

若使用Ice Cream Sandwich(4.0.x)或更老的版本,需要用prebuilts来代替prebuilts/misc

2 开始编译 Building the System

  • 1.Set up environment
$ source build/envsetup.sh
  • 1

or

$ . build/envsetup.sh
  • 1
  • 2.Choose a Target
$ lunch You're building on LinuxLunch menu... pick a combo:1. aosp_arm-eng2. aosp_arm64-eng3. aosp_mips-eng4. aosp_mips64-eng5. aosp_x86-eng6. aosp_x86_64-eng7. aosp_manta-userdebug8. aosp_flo-userdebug9. aosp_deb-userdebug10. full_fugu-userdebug11. aosp_fugu-userdebug12. aosp_tilapia-userdebug13. aosp_grouper-userdebug14. aosp_mako-userdebug15. aosp_hammerhead-userdebug16. aosp_flounder-userdebug17. aosp_shamu-userdebug18. mini_emulator_x86-userdebug19. mini_emulator_arm64-userdebug20. mini_emulator_x86_64-userdebug21. mini_emulator_mips-userdebug22. m_e_arm-userdebugWhich would you like? [aosp_arm-eng] 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 3.build

使用make构建,GNU make可以通过使用-jN参数来处理并行任务。此处的N,根据所用的计算机的硬件的核数来指定,一般N指定为CPU核心的1到2倍。例如我的CPU是i5-6500(CPU4个核,每个核2个线程),那么最优的构建是使用make -j8。

make -j8
  • 1

3 build successfully

#### make completed successfully (01:04:27 (hh:mm:ss)) ####
  • 1
  • 目录结构如下:
  7    924 00:36 android-info.txt62   924 00:22 build_fingerprint.txt4.0K 924 10:34 cache66M  924 12:26 cache.img70K  923 23:51 clean_steps.mk38   924 00:22 current_build_config.mk4.0K 924 10:41 data4.0K 924 09:51 dex_bootjars4.0K 924 01:33 gen1.4K 924 12:25 hardware-qemu.ini70K  924 11:00 installed-files.txt1.1M 924 00:30 module-info.json4.0K 924 11:00 obj38   923 23:51 previous_build_config.mk1.5M 924 10:40 ramdisk.img4.0K 924 10:39 recovery4.0K 924 10:39 root4.0K 924 10:39 symbols4.0K 924 10:50 system1.8G 924 11:01 system.img550M 924 10:58 userdata.img550M 924 12:41 userdata-qemu.img~/aosp/out/target/product/generic$ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

4 编译中的问题解决

  • 第一次编译,在编译到82%的时候失败,报错 : Increase Java heap size
including ./tools/external/fat32lib/Android.mk ...  
Starting build with ninja  
ninja: Entering directory `.'  
[  0% 1/21542] Ensure Jack server is installed and started  
Jack server already installed in "/home/smile/.jack-server"  
Server is already running  
[  0% 2/21542] Building with Jack: out/target/common/obj/JAVA_LIBRARIES/core-all_intermediates/with-local/classes.dex  
FAILED: /bin/bash out/target/common/obj/JAVA_LIBRARIES/core-all_intermediates/with-local/classes.dex.rsp  
Java heap space  
Try increasing heap size with java option '-Xmx<size>'  
Warning: This may have produced partial or corrupted output.  
ninja: build stopped: subcommand failed.  
build/core/ninja.mk:146: recipe for target 'ninja_wrapper' failed  
make: *** [ninja_wrapper] Error 1  \#### make failed to build some targets (08:25 (mm:ss)) ####  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
    stackoverflow上关于此问题的解决方法([详见这里](http://stackoverflow.com/questions/34940793/increasing-heap-size-while-building-the-android-source-code-on-ubuntu-15-10)),在make之前进行heap size配置:
1. export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m"  
2. out/host/linux-x86/bin/jack-admin kill-server  
3. out/host/linux-x86/bin/jack-admin start-server  
  • 1
  • 2
  • 3

三、运行

1 推送设备(真机运行)Flash device

  • To flash a device, you will need to use fastboot, which should be included in your path after a successful build. Place the device in fastboot mode either manually by holding the appropriate key combination at boot, or from the shell with
$ adb reboot bootloader
  • 1
  • Once the device is in fastboot mode, run
$ fastboot flashall -w
  • 1
  • The -w option wipes the /data partition on the device; this is useful for your first time flashing a particular device but is otherwise unnecessary.

2 模拟器运行 Flash emulator

  • 1.build generic img

lunch 1即可

  • 2.启动模拟器
~/aosp$ emulator emulator: WARNING: system partition size adjusted to match image file (1536 MB > 200 MB)emulator: WARNING: data partition size adjusted to match image file (550 MB > 200 MB)emulator: WARNING: Increasing RAM size to 1GB 
Creating filesystem with parameters: 
Size: 576716800 
Block size: 4096 
Blocks per group: 32768 
Inodes per group: 7040 
Inode size: 256 
Journal blocks: 2200 
Label: 
Blocks: 140800 
Block groups: 5 
Reserved block group size: 39 
Created filesystem with 11/35200 inodes and 4536/140800 blocks 
resize2fs 1.42.13 (17-May-2015) 
The filesystem is already 140800 (4k) blocks long. Nothing to do!Creating filesystem with parameters: 
Size: 69206016 
Block size: 4096 
Blocks per group: 32768 
Inodes per group: 4224 
Inode size: 256 
Journal blocks: 1024 
Label: 
Blocks: 16896 
Block groups: 1 
Reserved block group size: 7 
Created filesystem with 11/4224 inodes and 1302/16896 blocks 
emulator: UpdateChecker: skipped version check
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

3 注意事项

  • 1 因为上面的环境变量配置的是临时的,所以终端关闭后,直接运行emulator是不行的,需要重新运行下/build/envsetup.sh 和lunch 选择之前编译的版本

  • 2 安装kvm,开启硬件加速

    无论是在Windows平台还是Linux平台,或者还是Mac OSX平台,虚拟机的硬件加速全靠这个。

$ sudo apt-get install qemu-kvm
  • 1

直接执行,完成安装即可。

开启之后我们可以使用下面的指令来验证

$ egrep -c '(vmx|svm)' /proc/cpuinfo
  • 1

执行的结果不为0表示开启成功(我的是4)。

更好的阅读体验,请访问http://timeriver.com.cn/

这篇关于Android7.0源码编译运行指南的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/717178

相关文章

Python正则表达式匹配和替换的操作指南

《Python正则表达式匹配和替换的操作指南》正则表达式是处理文本的强大工具,Python通过re模块提供了完整的正则表达式功能,本文将通过代码示例详细介绍Python中的正则匹配和替换操作,需要的朋... 目录基础语法导入re模块基本元字符常用匹配方法1. re.match() - 从字符串开头匹配2.

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

Python实现精确小数计算的完全指南

《Python实现精确小数计算的完全指南》在金融计算、科学实验和工程领域,浮点数精度问题一直是开发者面临的重大挑战,本文将深入解析Python精确小数计算技术体系,感兴趣的小伙伴可以了解一下... 目录引言:小数精度问题的核心挑战一、浮点数精度问题分析1.1 浮点数精度陷阱1.2 浮点数误差来源二、基础解决

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

从基础到高级详解Python数值格式化输出的完全指南

《从基础到高级详解Python数值格式化输出的完全指南》在数据分析、金融计算和科学报告领域,数值格式化是提升可读性和专业性的关键技术,本文将深入解析Python中数值格式化输出的相关方法,感兴趣的小伙... 目录引言:数值格式化的核心价值一、基础格式化方法1.1 三种核心格式化方式对比1.2 基础格式化示例

sysmain服务可以禁用吗? 电脑sysmain服务关闭后的影响与操作指南

《sysmain服务可以禁用吗?电脑sysmain服务关闭后的影响与操作指南》在Windows系统中,SysMain服务(原名Superfetch)作为一个旨在提升系统性能的关键组件,一直备受用户关... 在使用 Windows 系统时,有时候真有点像在「开盲盒」。全新安装系统后的「默认设置」,往往并不尽编

Python ORM神器之SQLAlchemy基本使用完全指南

《PythonORM神器之SQLAlchemy基本使用完全指南》SQLAlchemy是Python主流ORM框架,通过对象化方式简化数据库操作,支持多数据库,提供引擎、会话、模型等核心组件,实现事务... 目录一、什么是SQLAlchemy?二、安装SQLAlchemy三、核心概念1. Engine(引擎)

Python自动化处理PDF文档的操作完整指南

《Python自动化处理PDF文档的操作完整指南》在办公自动化中,PDF文档处理是一项常见需求,本文将介绍如何使用Python实现PDF文档的自动化处理,感兴趣的小伙伴可以跟随小编一起学习一下... 目录使用pymupdf读写PDF文件基本概念安装pymupdf提取文本内容提取图像添加水印使用pdfplum