Ubuntu20.4编译AOSP源码实践
本文记录使用Ubuntu20.4编译AOSP的过程以及一些错误解决,使用的编译的分支为 android-8.1.0_r38
参考清华大学开源软件站构建代码仓库,本文采用repo同步的方式进行构建,构建的操作系统是Ubuntu20.4
一.Ubuntu虚拟机配置
主要关注内存和磁盘大小,本文中我分配了8G内存和200G的磁盘空间
二.依赖库安装
需要安装java环境、官方给定的环境依赖,ubuntu20.4自带python环境,如果没有需自行安装,用到的命令如下
# 环境安装
echo "更新apt-get"
sudo apt-get update
echo "安装jdk8"
yes | sudo apt-get install openjdk-8-jdk
echo "安装python2"
yes | sudo apt-get install python
echo "安装curl"
yes | sudo apt-get install curl
echo "安装git"
yes | sudo apt-get install git
echo "安装依赖"
yes | 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 ccache libgl1-mesa-dev libxml2-utils xsltproc unzip
echo "设置git账户"
git config --global user.email "Cubeeeeee@qq.com"
git config --global user.name "Cubeeeeee"
三.AOSP源代码同步
使用清华源同步,代码如下
# 源代码同步
echo "需要将脚本放到工作目录下执行"
mkdir -p ~/bin
PATH=~/bin:$PATH
sudo curl -k https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo
sudo chmod a+x ~/bin/repo
cd ~/bin/aosp
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
# 如需其他分支 修改此处
yes | repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-8.1.0_r38
echo "开始同步AOSP源码"
repo sync
同步成功后如下图

如果同步失败请移步下文错误解决汇总
四.源码编译
涉及到的命令如下,如果出错可以参考下文错误解决
bash
cd ~/bin/aosp
# 如果内存够大可以考虑设置缓存
# prebuilts/misc/linux-x86/ccache/ccache -M 50G
# 根据自己内存大小调整 默认用6g
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx6g"
make clobber
source build/envsetup.sh
# 版本根据需要修改
# lunch 24
lunch
# 线程数根据处理器数量修改
make -j4
五.错误解决
1.Curl下载Repo报SSL Error
添加-k参数,忽略https证书错误 即修改为
bash
sudo curl -k https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo
2.同步源码出错,git报CA证书错误
server certificate verification failed. CAfile: none CRLfile: none
禁用git的ssl校验即可
git config --global http.sslverify false
git config --global https.sslverify false
3.同步源码出错,无法连接到 https://android.googlesource.com/
未正确配置环境变量,检查~/.bashrc,正确配置如下图

或者在同一终端执行
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
4.同步源码出错,python相关错误
修改python优先级,默认使用python3
bash
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150
5.编译报错Jack server(35). Try 'jack-diagnose'
详细报错内容如下:
[ 10% 538/4980] Ensuring Jack server is installed and started
FAILED: setup-jack-server
/bin/bash -c "(prebuilts/sdk/tools/jack-admin install-server prebuilts/sdk/tools/jack-launcher.jar prebuilts/sdk/tools/jack-server-4.11.ALPHA.jar 2>&1 || (exit 0) ) && (JACK_SERVER_VM_ARGUMENTS=\"-Dfile.encoding=UTF-8 -XX:+TieredCompilation\" prebuilts/sdk/tools/jack-admin start-server 2>&1 || exit 0 ) && (prebuilts/sdk/tools/jack-admin update server prebuilts/sdk/tools/jack-server-4.11.ALPHA.jar 4.11.ALPHA 2>&1 || exit 0 ) && (prebuilts/sdk/tools/jack-admin update jack prebuilts/sdk/tools/jacks/jack-4.32.CANDIDATE.jar 4.32.CANDIDATE || exit 47 )"
Jack server already installed in "/home/user/.jack-server"
Communication error with Jack server (35), try 'jack-diagnose' or see Jack server log
SSL error when connecting to the Jack server. Try 'jack-diagnose'
SSL error when connecting to the Jack server. Try 'jack-diagnose'
[ 10% 541/4980] build out/target/product/rk3399_mid/obj/ETC/precompiled_sepolicy_intermediates/precompiled_sepolicy
ninja: build stopped: subcommand failed.
22:32:18 ninja failed with: exit status 1
#### failed to build some targets (01:11 (mm:ss)) ####
Build android failed!
解决方案为修改/etc/java-8-openjdk/security/java.security文件,取消TLSv1, TLSv1.1 禁用,修改后内容如下

6.编译报错,Python相关错误
错误内容
system/core/libsysutils/EventLogTags.logtags system/core/logcat/event.logtags system/core/logd/event.logtags system/core/storaged/EventLogTags.log
tags"
File "build/tools/merge-event-log-tags.py", line 51
except getopt.GetoptError, err:
^
SyntaxError: invalid syntax
[ 0% 6/89331] Ensuring Jack server is installed and started
Jack server already installed in "/root/.jack-server"
Server is already running
ninja: build stopped: subcommand failed.
错误原因是编译时需要使用python2.7(源码同步时使用python3.8),只需修改python优先级,优先使用python2.7即可
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 150
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 100
7.编译报错Jack server(52). Try 'jack-diagnose'
错误示例
bash
[ 76% 69866/91547] Building with Jack:...k_intermediates/with-local/classes.dex
FAILED: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/with-local/classes.dex
/bin/bash out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/with-local/classes.dex.rsp
Communication error with Jack server (52). Try 'jack-diagnose'
[ 76% 69869/91547] //art/compiler:liba...izing/code_generator_x86_64.cc [linux]
ninja: build stopped: subcommand failed.
06:32:11 ninja failed with: exit status 1
05:16:52.810: INFO: com.android.jack.server.JackHttpServer: Jack 1.3-rc7 'Douarn' (445000 d7be3910514558d6715ce455ce0861ae2f56925a by N/A) available in ./jack/jack-1635487485073-0.jar
05:16:52.811: INFO: com.android.jack.server.JackHttpServer: Starting service connection server on /127.0.0.1:18076
解决方案是设置 JACK_JAR
环境变量,在同一终端执行export命令或者写入~/.bashrc即可解决,
如果仍未解决可以尝试同时修改
$HOME/.jack-settings和$HOME/.jack-server/config.properties
中的端口号(比如都改为8386/8387)
8.编译报错 Aborted (core dumped)
错误内容如下
flex-2.5.39: loadlocale.c:130: _nl_intern_locale_data: Assertioncnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))' failed. Aborted (core dumped)
执行
bash
export LC_ALL=C
去除本地化设置
9.编译报错缺少libncurses.so.5
缺少依赖
shell
sudo apt-get install libncurses*
六.脚本整合
为了便于使用,我将上述过程整理成了几个自动化脚本文件,在Ubuntu20.4下可以自动进行环境配置、源码同步、源码编译
envinstall.sh 环境安装
shell
# 环境安装
echo "更新apt-get"
sudo apt-get update
echo "安装jdk8"
yes | sudo apt-get install openjdk-8-jdk
echo "安装curl"
yes | sudo apt-get install curl
echo "安装git"
yes | sudo apt-get install curl
echo "安装依赖"
yes | 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 ccache libgl1-mesa-dev libxml2-utils xsltproc unzip
echo "设置git账户"
git config --global user.email "Cubeeeeee@qq.com"
git config --global user.name "Cubeeeeee"
# 修改java配置文件 否则jack会报 35错误
sudo cp /etc/java-8-openjdk/security/java.security /etc/java-8-openjdk/security/java.security.bak
sudo rm /etc/java-8-openjdk/security/java.security
sudo cp java.security /etc/java-8-openjdk/security/java.security
repo.sh 拉取代码
shell
# 源代码同步
echo "需要将脚本放到工作目录下执行"
mkdir ~/bin
temp=$(pwd) | PATH=$temp:$PATH
# 同步源码时使用python3
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150
sudo curl -k https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo
sudo chmod a+x ~/bin/repo
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
# 如需其他分支 修改此处
yes | repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-8.1.0_r38
echo "开始同步AOSP源码"
repo sync
compile.sh 编译
shell
# 编译时使用python2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 150
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 100
echo "正在清理"
make clobber
source ./build/envsetup.sh
# 版本根据需要修改
echo "选择编译版本"
lunch 24
# lunch
# 线程数根据处理器数量修改
echo "开始全量编译"
make -j4