Android 源码下载、编译填坑

本次源码下载编译使用了 Ubuntu20.04.1 版本的虚拟机,可自行安装。

1 源码下载

源码下载阶段十分漫长,需要耐心等待。

1.1 git、repo配置

sudo apt-get install git
git config --global user.name "your name"
git config --global user.email "[email protected]"

git clone https://aosp.tuna.tsinghua.edu.cn/git-repo/
chmod a+x git-repo/repo
export PATH=~/git-repo:$PATH

gedit ~/git-repo/repo
#替换 REPO_URL = 'https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
# 或者 REPO_URL = 'https://gerrit-googlesource.proxy.ustclug.org/git-repo'

1.2 下载源码(1)

repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest
或者 repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest
# 查看可以下载的android源码分支
cd .repo/manifests.git/
git branch -a
# 按需选择分支
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-8.1.0_r41
# 或者
repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest -b android-8.1.0_r41
repo sync

1.3 下载源码(2)

# http 下载 aosp-20160806.tar
https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/
# 解压
tar -xvf aosp-20160806.tar

# 已有仓库改用科大源(推荐)/清华源
cd aosp
gedit .repo/manifests.git/config
更改 url = https://android.googlesource.com/platform/manifes
为 url = git://mirrors.ustc.edu.cn/aosp/platform/manifest
或者 url = https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest


# 通过 HTTP 协议同步(不推荐)
gedit .repo/manifests.git/config
更改 url = git://mirrors.ustc.edu.cn/aosp/platform/manifest
为 url = http://mirrors.ustc.edu.cn/aosp/platform/manifest

# repo sync 命令默认使用 4 个并发连接,请勿使用 -j 参数增加并发连接数
repo sync

2 Jdk 及编译依赖包

编译之前需要安装相关的依赖包,否则会报各种莫名奇妙的错误。

sudo apt-get install openjdk-8-jdk
sudo apt-get install openjdk-8-jre
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
sudo apt-get install libncurses5

3 相关编译错误处理

# 常用的编译命令
# 1.进入源码根目录
source build/envsetup.sh
# 2. 选择要编译的 device
lunch
# 3. 执行编译命令 make
make -j8

在编译中可能会遇到的一些报错以及相关解决方法:

3.1 glxinfo: not found

ERROR:

sh: 1: glxinfo: not found

sudo apt install mesa-utils

3.2 去除所有本地化的设置

ERROR:

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: ?? ‘cnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))’ ???sh

# 去除所有本地化的设置
export LC_ALL=C

3.3 Jack Server 相关

ERROR:

Out of memory error (version 1.3-rc6 ‘Douarn’ (441800 22a11d4b264ae70e366aed3025ef47362d1522bb by [email protected])).
GC overhead limit exceeded.

# JVM heap size调整为4g
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m"
./prebuilts/sdk/tools/jack-admin kill-server
./prebuilts/sdk/tools/jack-admin start-server

# 配置jack-admin的环境变量
export PATH=$PATH:~/Android/source/prebuilts/sdk/tools
jack-admin start-server

3.4 python 解析器出错

ERROR:

FAILED: out/target/common/obj/all-event-log-tags.txt
/bin/bash -c "build/tools/merge-event-log-tags.py -o out/target/common/obj/all-event-log-tags.txt frameworks/base/core/java/android/app/admin/SecurityLogTags.logtags …
File “build/tools/merge-event-log-tags.py”, line 51
except getopt.GetoptError, err:
^
SyntaxError: invalid syntax

#原因:python解析器出错,源码编译用python2,不能用python3编译源码 (下载源码:要用python3.6以上,才能下载源码成功)
#解决方案:将python改成ubuntu系统自带的python2.7,不能用python3.6以上的编译。
cd /usr/bin/
ll |grep python
# 将popython2的优先级提高,从而让python指向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

4 emulator 启动模拟器

# 运行模拟器需指定以下四个参数
# Linux Kernel、system.img、userdata.img、ramdisk.img
emulator -help

默认情况下:
lunch aosp_arm-eng
Linux Kernel: ./prebuilds/qemu-kernel/arm/kernel-qeme => kernel-qemu
android image: ./out/target/product/generic => system.img、userdata.img、ramdisk.img

lunch aosp_arm64-eng
linux Kernel: ./prebuilds/qemu-kernel/arm64/kernel-qemu => kernel-qemu
android image: ./out/target/product/generic64 =>  system.img、userdata.img和ramdisk.img。

谷歌官方刷写设备

驱动的二进制文件

/root/Android/Sdk/emulator/emulator -kernel 
export PATH=$PATH:/home/wugm/code/aosp/out/host/linux-x86/bin
export ANDROID_PRODUCT_OUT=/home/wugm/code/aosp/out/target/product/marlin/

#emulator -kernel ./kernel/common/arch/arm/boot/zImage &
emulator -kernel ./prebuilt/android-arm/kernel/kernel-qemu -sysdir ./out/target/product/generic -system system.img -data userdata.img -ramdisk ramdisk.img

5 编译自己的sdk

# 输出见 out/host/linux-x86/sdk
source build/envsetup.sh
lunch sdk-eng
make sdk

6 Ubuntu 相关设置

6.1 输入法

搜狗输入法 for linux

6.2 系统升级

sudo apt-get update
sudo apt-get upgrade
sudo update-manager -c -d

6.3 解压 7z

apt-get install p7zip-full
7z x file.7z

6.4 python 软链接

# python 软链接
ln -s /usr/bin/python3.8 /usr/bin/python

相关参考:

Ubantu18.04环境下编译android源码

官方-下载源代码

Android 镜像使用帮助

Ubuntu(16.0.4)上编译android8.0源码(资料最全版本)

你可能感兴趣的:(Android,源码调试,java,android,ubuntu)