编译、运行源码
本文最后更新于:1 年前
Reference
软、硬件环境
构建编译环境
安装 Git 并配置用户名、密码
$ sudo apt-get install git $ git config --global user.email "
[email protected]" $ git config --global user.name "xx"
安装 JDK
$ sudo apt-get update
$ sudo apt-get install openjdk-8-jdk
安装依赖
$ sudo apt-get install libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev g++-multilib
$ sudo apt-get install -y git flex bison gperf build-essential libncurses5-dev:i386
$ 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 libgl1-mesa-dev libxml2-utils xsltproc unzip m4
$ sudo apt-get install lib32z-dev ccache
下载源码
安装 Repo 工具
-
$ mkdir ~/bin $ PATH=~/bin:$PATH
-
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo $ chmod a+x ~/bin/repo
-
$ curl https://storage-googleapis.lug.ustc.edu.cn/git-repo-downloads/repo > ~/bin/repo
line 1: html: No such file or directory line 2: syntax error near unexpected token `<' line 2: `<head><title>301 Moved Permanently</title></hea'>
-
如果下载失败,原因是未翻墙,可在别处下载好并复制到特定目录中
$ cp repo ~/bin
同步源码
-
$ wget https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar
-
$ tar xf aosp-latest.tar
-
$ cd aosp
-
$ sudo gedit ~/bin/repo
fatal: unable to access 'https://gerrit.googlesource.com/git-repo/': Failed to connect to gerrit.googlesource.com port 443: Connection timed out fatal: unable to access 'https://gerrit.googlesource.com/git-repo/': Failed to connect to gerrit.googlesource.com port 443: Connection timed out error: Cannot fetch repo
-
登录 源代码标记和细分版本 查看,比如 Pixel XL 最新的是 android-7.1.1_r26。
-
设置获取的源码的版本
在命令行运行:
$ repo init -b android-7.1.1_r26
-
同步源码树
在命令行运行:
$ repo sync
-
同步完成后 aosp 会显示内容
编译源码
设置环境
-
执行 envsetup.sh 脚本
在命令行运行:
$ source build/envsetup.sh
设置目标
-
登录 选择设备编译系统 查看,BUILD 为
aosp_x86_64
对应 Intel 平台的模拟器,BUILD 为aosp_marlin
对应 Pixel XL。BUILD 包含
arm
,表示系统运行在 arm 架构的处理器上,arm64
表示系统运行在 64 位 arm 架构的处理器上。x86
和mips
同理。BUILDTYPE 是
user
,表示编译出的系统镜像是可以用来正式发布到市场的版本,没有 root 权限和 debug 权限。userdebug
表示有 root 权限和 debug 权限。eng
表示系统镜像是开发版,有最大的权限。在命令行运行:
$ lunch
会打印出所有目标,通过输入序号可以选择目标:
You're building on Linux Lunch menu... pick a combo: 1. aosp_arm-eng 2. aosp_arm64-eng 3. aosp_mips-eng 4. aosp_mips64-eng 5. aosp_x86-eng 6. aosp_x86_64-eng 7. full_fugu-userdebug 8. aosp_fugu-userdebug 9. car_emu_arm64-userdebug 10. car_emu_arm-userdebug 11. car_emu_x86_64-userdebug 12. car_emu_x86-userdebug 13. mini_emulator_arm64-userdebug 14. m_e_arm-userdebug 15. m_e_mips64-eng 16. m_e_mips-userdebug 17. mini_emulator_x86_64-userdebug 18. mini_emulator_x86-userdebug 19. uml-userdebug 20. aosp_dragon-userdebug 21. aosp_dragon-eng 22. aosp_marlin-userdebug 23. aosp_marlin_svelte-userdebug 24. aosp_sailfish-userdebug 25. aosp_angler-userdebug 26. aosp_bullhead-userdebug 27. aosp_bullhead_svelte-userdebug 28. hikey-userdebug 29. hikey960-userdebug Which would you like? [aosp_arm-eng] 6 ============================================ PLATFORM_VERSION_CODENAME=P PLATFORM_VERSION=P TARGET_PRODUCT=aosp_x86_64 TARGET_BUILD_VARIANT=eng TARGET_BUILD_TYPE=release TARGET_PLATFORM_VERSION=PPR1 TARGET_BUILD_APPS= TARGET_ARCH=x86_64 TARGET_ARCH_VARIANT=x86_64 TARGET_CPU_VARIANT= TARGET_2ND_ARCH=x86 TARGET_2ND_ARCH_VARIANT=x86_64 TARGET_2ND_CPU_VARIANT= HOST_ARCH=x86_64 HOST_2ND_ARCH=x86 HOST_OS=linux HOST_OS_EXTRA=Linux-4.4.0-59-generic-x86_64-Ubuntu-16.04.1-LTS HOST_CROSS_OS=windows HOST_CROSS_ARCH=x86 HOST_CROSS_2ND_ARCH=x86_64 HOST_BUILD_TYPE=release BUILD_ID=OC OUT_DIR=out AUX_OS_VARIANT_LIST= ============================================
-
也可以直接选择目标
在命令行运行:
$ lunch aosp_x86_64-eng
-
设置 8 个线程参与编译,并开始编译
在命令行运行:
$ make -j8
当看到
#### build completed successfully (01:45:47 (hh:mm:ss)) ####
即表示编译成功,括号内为编译共计耗时。
运行
在模拟器中运行
配置参数
-
$ emulator -partition-size 4096
emulator: WARNING: system partition size adjusted to match image file (2048 MB > 200 MB) emulator: WARNING: data partition size adjusted to match image file (550 MB > 200 MB)
-
$ emulator -memory 2048
emulator: WARNING: Increasing RAM size to 1GB
模拟器运行所需要的 4 个文件包括:
Linux Kernel
、system.img
、userdate.img
、ramdisk.img
。