安装WSL
现在,可以在 PowerShell 或 Windows 命令提示符中输入此命令,然后重启计算机来安装WSL所需的全部内容,默认版本是 WSL 2, Linux是Ubuntu的
wsl --install
下载源码前的准备工作
设置aosp源码所在路径为大小写敏感
设置D:\androidSource的所有用户为“完全控制”权限
下载源码
安裝repo工具
mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
repo的运行过程中会尝试访问官方的git源更新自己,一般无法正常访问,如果想使用tuna的镜像源进行更新,要将如下内容复制到你的~/.bashrc里
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
然后重启终端模拟器,重启后还需要再次执行PATH=~/bin:$PATH,否则找不到repo
下载每月更新的初始化包
直接使用迅雷下载,地址https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar,这个文件很大,我下载的时候已经达到123GB了,因此要准备比较大的空间,下载完成后进行以下命令
cd AOSP # 解压得到的 AOSP 工程目录
# 这时 ls 的话什么也看不到,因为只有一个隐藏的 .repo 目录
repo sync # 正常同步一遍即可得到完整目录
repo sync 完成后,会显示下图
选择对应的分支并拉出代码
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-11.0.0_r17
repo sync
repo sync -j1 --fail-fast:在遇到第一个错误的时候退出
如果不知道有哪些分支可以切换,
可以进入该google官网代号标记和细分版本查看:
https://source.android.com/setup/start/build-numbers;
也可以输入以下命令,查看可切换分支
cd .repo/manifests
git branch -a | cut -d / -f 3
repo sync遇到的问题:
问题一
fatal: unable to access 'http://aosp.tuna.tsinghua.edu.cn/platform/manifest/': server certificate verification failed. CAfile: none CRLfile: none
解决方法:
禁用git的SSL验证
git config --global http.sslverify false
git config --global https.sslverify false
问题二
error: .repo/manifests/: contains uncommitted changes
解决方法:
cd .repo/manifests
git config core.filemode false
Git不仅能够管理文件的版本,而且能够管理对文件的访问权限,在本地工作目录只改变文件的访问权限,也是对git库中管理的文件的修改,执行git status可以看到文件的变化,Git对文件的访问权限的管理与配置选项core.filemode有关,core.filemode选项默认true,即区分文件的执行权限
查看filemode选项 git config --get core.filemode 或 git config core.filemode
设置filemode选项 git config core.filemode true
问题三
没有改动代码,也有可能出现此问题,因为有可能是build的时候进行的修改
packages/apps/Traceur/: discarding 808 commits error: packages/apps/Traceur/: platform/packages/apps/Traceur checkout dfa65bdd6454b97fc184a55c949deb391f976540 error: Cannot checkout platform/packages/apps/Traceur packages/providers/TelephonyProvider/: discarding 834 commits error: packages/providers/TelephonyProvider/: platform/packages/providers/TelephonyProvider checkout 8e526a25a711c3d446b027df71bf8a096b243b8f error: Cannot checkout platform/packages/providers/TelephonyProvider error: Your local changes to the following files would be overwritten by checkout: Android.mk AndroidManifest.xml jni/Android.bp jni/GifTranscoder.cpp proguard.flags res/drawable-hdpi/ic_music.png
解决方法:
//repo forall -c 是对所有git仓库执行后面的命令
repo forall -c git reset --hard
再重新选择对应的分支并拉出代码:
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-11.0.0_r17
repo sync
可以到.repo/manifests目录下看当前版本
cd .repo/manifests
git branch -a
或者查看 .repo/manifests.git 目录下的 config 文件,在 [branch "default"] 的 merge 那里,没切换前是merge = refs/heads/master
cd .repo/manifests.git
cat config
问题四
info: A new version of repo is available
warning: repo is not tracking a remote branch, so it will not receive updates
repo reset: error: Entry 'command.py' not uptodate. Cannot merge.
fatal: Could not reset index file to revision 'v2.17.1^0'.
解决方法:
//找到该文件目录
find . -name command.py
//进入到上一个命令找到的目录下
cd .repo/repo
//更新文件
git pull
//回到aosp目录
cd ../..
问题五
/usr/bin/env: ‘python’: No such file or directory
解决方法:
//查找python3的安装位置,默认会安装python3
whereis python3
//为其创建符号连接
sudo ln -s /usr/bin/python3 /usr/bin/python
编译准备工作
安装 jdk
sudo apt install openjdk-8-jdk
安装依赖包
为了提高软件包的安装速度,可以更换软件源为国内的源,更换方式如下
更换Linux安装源
实际上是修改一个文件,先到文件目录下
cd /etc/apt
将该文件进行备份,以免误操作
cp sources.list sources.list.bak
修改文件内容
vi /etc/apt/sources.list
在文件顶部增加以下任意一种镜像源
##阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
##网易源
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
##清华源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
##中科大源
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
可以通过下面的地址获取清华源的配置
https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/
更新源
sudo apt-get update
安装依赖库
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 libssl-dev
安装依赖库遇到的问题
问题一
当使用apt-get install 安装32位支持库的时候,提示无法找到对应包
E: Unable to locate package libx11-dev:i386
E: Unable to locate package libreadline6-dev:i386
解决方法:
dpkg --add-architecture i386
apt-get update
问题二
执行 sudo apt-get install libesd0-dev
时出现
E: Unable to locate package libesd0-dev
解决方法:
## 解决libesd0-dev无法安装问题
deb http://us.archive.ubuntu.com/ubuntu/ xenial main universe
deb-src http://us.archive.ubuntu.com/ubuntu/ xenial main universe
更新软件源并重新安装
开始编译
source build/envsetup.sh
// 进行清除操作,以避免之前进行的build干扰到接下来的build
make clobber
// 选择编译开发工程师的版本,方便debug
lunch aosp_x86_64-eng
make -j6
编译 make 遇到的问题
执行 make -j6 是出现以下报错:
问题一
Failed to listen for path logs: listen unix out/.path_interposer_log: bind: operation not supported
解决方法:
原因是因为我的代码在 /mnt/d/androidSource/aosp 中,还是在 windows 路径中,导致无法启动编译。会一直报系统不支持的错误,可以到 /home/user 目录下编译,但空间可能不够,因此需要把 Ubuntu 移到一个空间足够大的盘下
查看已安装的ubuntu版本
wsl -l --all -v
导出分发版为tar文件到d盘
wsl --export Ubuntu D:\Ubuntu\wsl-ubuntu.tar
注销当前分发版
wsl --unregister Ubuntu
重新导入并安装在D盘
wsl --import ubuntu D:\Ubuntu D:\Ubuntu\wsl-ubuntu.tar --version 2
设置默认登陆用户为安装时用户名
ubuntu config --default-user USERNAME
删除wsl-ubuntu.tar
del d:\wsl-ubuntu.tar
扩展虚拟磁盘空间
WSL2系统的虚拟磁盘空间,默认是250G,android 11 代码量很大,因此我扩展为 600G,这一段是在PowerShell上输入的,输入的新值必须大于此原始值(以 MB 为单位)
PS C:\WINDOWS\system32> diskpart
Microsoft DiskPart 版本 10.0.19041.964
Copyright (C) Microsoft Corporation.
在计算机上: DESKTOP-IP7P9JS
DISKPART> select vdisk file="D:\Ubuntu\ext4.vhdx"
DiskPart 已成功选择虚拟磁盘文件。
DISKPART> expand vdisk maximum="600000"
100 百分比已完成
DiskPart 已成功扩展虚拟磁盘文件。
DISKPART>
查看虚拟大小的值
DISKPART> detail vdisk
设备类型 ID: 0 (未知)
供应商 ID: {00000000-0000-0000-0000-000000000000} (未知)
状态: 已添加
虚拟大小: 585 GB
物理大小: 2894 MB
文件名: D:\Ubuntu\ext4.vhdx
为子级: 否
父文件名:
找不到关联的磁盘号。
在 WSL 的 Ubuntu 里行以下命令,让 WSL 知道它可扩展其文件系统的大小
sudo mount -t devtmpfs none /dev
mount | grep ext4
第一个命令会输出:mount: /dev: none already mounted on /dev. 可以不管这个
第二个命令输出下面内容,可以知道磁盘设备为 /dev/sdb:
/dev/sdb on / type ext4 (rw,relatime,discard,errors=remount-ro,data=ordered)
再输入命令:
sudo resize2fs /dev/sdb
扩展前的大小:
扩展后大小为
问题二
FAILED: out/soong/build.ninja
out/soong/.bootstrap/bin/soong_build -t -l out/.module_paths/Android.bp.list -b out/soong -n out -d out/soong/build.ninja.d -globFile out/soong/.bootstrap/build-globs.ninja -o out/soong/build.ninja Android.bp
error: cts/hostsidetests/appsecurity/test-apps/CorruptApkTests/Android.bp:37:1: module "CtsCorruptApkTests_b79488511" variant "android_common": module source path "cts/hostsidetests/appsecurity/test-apps/CorruptApkTests/b79488511.apk" does not exist
20:23:41 soong bootstrap failed with: exit status 1
#### failed to build some targets (24 seconds) ####
解决方法:
//到报错目录下
cd cts/hostsidetests/appsecurity/test-apps/CorruptApkTests
//给 Android.bp 和 Android.mk 改名
mv Android.bp Android.bp.ori
mv Android.mk Android.mk.ori
问题三
FAILED: out/target/product/generic_x86/obj/RENDERSCRIPT_BITCODE/libclcore.bc_intermediates/rs_cl.bc
/bin/bash -c "PWD=/proc/self/cwd prebuilts/clang/host/linux-x86/clang-3289846/bin/clang -Iframeworks/rs/script_api/include -Iexternal/clang/lib/Headers -MD -DRS_VERSION=24 -std=c99 -c -O3 -fno-builtin -emit-llvm -target renderscript32-linux-androideabi -fsigned-char -D__i386__ -Wno-deprecated -Werror -Werror -Wall -Wextra -Iframeworks/rs/cpu_ref -DRS_DECLARE_EXPIRED_APIS -x renderscript frameworks/rs/driver/runtime/rs_cl.c -o out/target/product/generic_x86/obj/RENDERSCRIPT_BITCODE/libclcore.bc_intermediates/rs_cl.bc"
prebuilts/clang/host/linux-x86/clang-3289846/bin/clang.real: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
14:28:09 ninja failed with: exit status 1
解决方法,其他so依赖问题也是同样处理方法:
//找到报错里的so文件
find / -name libncurses.so.5
//将so文件拷贝到/usr/lib下
cp prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8/sysroot/usr/lib/libncurses.so.5 /usr/lib
最后成功
运行虚拟机
emulator
暂时不能在 WSL 中运行 emulator,下面的不用看了。因为 WSL 本身就是一个模拟器,要在模拟器中运行模拟器,WSL 目前还不支持
运行虚拟机 emulator 遇到的问题
问题一
emulator: ERROR: x86_64 emulation currently requires hardware acceleration!
CPU acceleration status: KVM requires a CPU that supports vmx or svm
More info on configuring VM acceleration on Linux:
https://developer.android.com/studio/run/emulator-acceleration#vm-linux
General information on acceleration: https://developer.android.com/studio/run/emulator-acceleration.
解决方法:
//检查 Linux 上当前是否安装了 KVM
kvm-ok
//如果没有 kvm-ok 命令,安装包含 kvm-ok 命令的 cpu-checker 软件包
sudo apt-get install cpu-checker
//下面的命令输出值 1 或更大值表示支持虚拟化。输出值 0 表示您的 CPU 不支持硬件虚拟化
egrep -c '(vmx|svm)' /proc/cpuinfo
再次运行 kvm-ok
kvm-ok
预期输出:INFO: /dev/kvm exists KVM acceleration can be used,如果出现以下错误,安装KVM
Your CPU does not support KVM extensions
KVM acceleration can NOT be used
安装 KVM
Cosmic (18.10) 或更高版本:
sudo apt-get install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils
Lucid (10.04) 或更高版本:
sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils
Karmic (9.10) 或更低版本:
sudo aptitude install kvm libvirt-bin ubuntu-vm-builder bridge-utils