编译android系统

161 阅读2分钟

一、下载AOSP源码

按照 Google 官方教程 source.android.com/source/down…

https://android.googlesource.com/platform/manifest 替换为 git://mirrors.ustc.edu.cn/aosp/platform/manifest

具体做法摘录如下(以防墙抽风):

首先下载 repo 工具。

mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
## 如果上述 URL 不可访问,可以用下面的:
## curl -sSL  'https://gerrit-googlesource.proxy.ustclug.org/git-repo/+/master/repo?format=TEXT' |base64 -d > ~/bin/repo
chmod a+x ~/bin/repo

然后建立一个工作目录(名字任意)

mkdir WORKING_DIRECTORY
cd WORKING_DIRECTORY

初始化仓库:

repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest
## 如果提示无法连接到 gerrit.googlesource.com,可以编辑 ~/bin/repo,把 REPO_URL 一行替换成下面的:
## REPO_URL = 'https://gerrit-googlesource.proxy.ustclug.org/git-repo'

如果需要某个特定的 Android 版本(Android 版本列表)(推荐):

repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest -b android-4.0.1_r1

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

repo sync

二、搭建编译环境

参考官网:source.android.com/source/init…

三、编译 Android

参考官网:source.android.com/docs/setup/…

四、常见问题

1、JDK版本问题

报错信息如下:

You asked for an OpenJDK based build but your version is
java version "1.8.0_111" Java(TM) SE Runtime Environment (build 1.8.0_111-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode).

**注意:**AOSP 编译需要使用 openjdk,不能使用oracle 的JDK 版本

解决办法:

安装open jdk

sudo apt-get update
sudo apt-get install openjdk-8-jdk    

2、去除所有本地化的设置

error:flex-2.5.39: loadlocale.c:130: _nl_intern_locale_data: Assertion `cnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))' failed. Aborted (core dumped)

或者

FAILED: out/host/linux-x86/obj/EXECUTABLES/checkpolicy_intermediates/policy_scan.c
 /bin/bash -c "prebuilts/misc/linux-x86/flex/flex-2.5.39 -oout/host/linux-x86/obj/EXECUTABLES/checkpolicy_intermediates/policy_scan.c external/selinux/checkpolicy/policy_scan.l"
 flex-2.5.39: loadlocale.c:130: _nl_intern_locale_data: Assertion `cnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))' failed.
 Aborted (core dumped)

解决办法:

在编译脚本中执行

export LC_ALL=C

LC_ALL=C 是为了去除所有本地化的设置,让命令能正确执行, 但是不可以修改~/.bashrc,会导致终端内中文显示为数字(应该是对应的编码)

3、Java heap 空间不足

[ 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>'

解决办法:

export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m"
out/host/linux-x86/bin/jack-admin kill-server
out/host/linux-x86/bin/jack-admin start-server

4、库冲突

libGL error: unable to load driver: i965_dri.so
libGL error: driver pointer missing
libGL error: failed to load driver: i965
libGL error: unable to load driver: i965_dri.so
libGL error: driver pointer missing
libGL error: failed to load driver: i965
libGL error: unable to load driver: swrast_dri.so
libGL error: failed to load driver: swrast

解决办法:

//依次输入以下命令(如果是在编译成功源码之后直接想运行模拟器,则直接输入emulator命令就行,因为前面编译源码已经输入过以上两条命令)

source build/envsetup.sh
lunch(选择刚才你编译源码设置的目标版本)
emulator -use-system-libs

或者

source build/envsetup.sh
lunch(选择刚才你编译源码设置的目标版本)
export ANDROID_EMULATOR_USE_SYSTEM_LIBS=1
emulator

5、subcommand failed

[  0% 2/55630] Lex: applypatch <= bootable/recovery/edify/lexer.ll
FAILED: out/target/product/generic/obj/STATIC_LIBRARIES/libedify_intermediates/lexer.cpp 
/bin/bash -c "prebuilts/misc/linux-x86/flex/flex-2.5.39 -oout/target/product/generic/obj/STATIC_LIBRARIES/libedify_intermediates/lexer.cpp bootable/recovery/edify/lexer.ll"
flex-2.5.39: loadlocale.c:130:_nl_intern_locale_data: ?? 'cnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))' ???
Aborted
ninja: build stopped: subcommand failed.
17:45:24 ninja failed with: exit status 1
make: *** [build/core/main.mk:21:run_soong_ui] 错误 1

解决办法:

Try running export LC_ALL=C before building.

I had a similar error when building on Ubuntu 18.04.

6、ubuntu 打开Android模拟器(Android源码编译)提示:emulator:未找到命令

解决办法:

source build/envsetup.sh
lunch
emulator

7、系统编译后无法在模拟器上运行

emulator: WARNING: encryption is off
sh: 1: glxinfo: not found

解决办法

https://groups.google.com/g/android-building/c/YZOr5YCIQgo
I had similar issues. I think it was related to the video driver.  
Still what fixed my problems was to use  
  
lunch full-eng  
  
After this, make and start the emulator. It should work.

或者

Try using the "-gpu off" option.

emulator -gpu off

which will run without opengl support.