安装 wsl
windows11
首先在 microsoft store 里面安装ubuntu版本,我选择的是 ubuntu 18.04:

安装完成后,在任务栏的搜索栏里面输入启动或关闭 Windows 功能
,打开下面的选项:

然后打开 ubuntu 18.06,如果遇到报错WslRegisterDistribution failed with error: 0x800701bc
,安装这个升级补丁。
安装 vscode
为何要安装 vscode,因为 vscode 安装一个 wsl 插件后,简直无敌。Linux 与 Window 无缝衔接,甩虚拟机体验几条街。

安装完成后,我们就可以在 remote 里面看到 ubuntu 18.04 了:

点击箭头右边的文件夹图标,vscode 会自动帮我们往 ubuntu 18.04 里面安装 vscode 的 server 插件,安装完成后,我们就可以使用 code 命令,打开与创建任意的文件/文件夹了:

该命令会打开一个新的 vscode 窗口:

点击 yes,就可以在 windows 里面操作 ubnutu 18.04 里面的文件了。

在这个窗口下面,几乎可以做任何事情,其余做不到的,还有 terminal 可以使用,要不是 vscode 的 android 插件少了点,我都想用 vscode 开发 android 了。
根据我的使用体验来说,用了 wsl + code,很难再回到虚拟机了,实在要用虚拟机也可以,为了减少性能消耗,我们可以安装 server 版的 linux,然后配合 vscode,可以做到一样的效果。
给wsl换源
找到 /etc/apt/sources.list
,备份,然后清空,粘贴下面的源(当然你也可以选择其他的源):
ruby
# 清华源
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
迁移WSL2到一个可用空间至少200G的磁盘
由于我的是笔记本,就一个C盘,500G,所以下面的操作未验证
wsl2 默认安装在C盘,因为Android 10 系统源码比较庞大,下载加编译需要100多G空间,而且文件系统必须要用linux的,必须在wsl2 unbutu系统内部的文件夹下载编译AOSP,不要下载到window 的文件系统里,比如下载到挂载点 /mnt/d 也就是对应window文件系统的D盘,下载是成功的,但是编译过程会因为跨了window和linux文件系统交叉,会编译出错。系统源码要下载到wsl 虚拟机内部空间里头,所以需要做一个操作:把WSL2迁移到一个空间至少有200G剩余的磁盘里。
以下迁移操作均在 powershell 中实现。此处选择 D 盘进行举例。
lua
2.导出wsl 内核到非系统分区
wsl --export Ubuntu-20.04 d:\wsl-ubuntu20.04.tar
3. 注销当前内核
wsl --unregister Ubuntu-20.04
4. 重新安装 WSL
wsl --import Ubuntu-20.04 d:\wsl-ubuntu20.04 d:\wsl-ubuntu20.04.tar --version 2
5. 设置默认登陆用户
USERNAME 替换成系统中已有的用户名。
ubuntu2004 config --default-user USERNAME
6. 删除中间缓存文件
del d:\wsl-ubuntu20.04.tar
下载android源码
我选择的是 android-7.1.2_r33,至于为啥呢,当然是因为
youpk
了
打开一个 powershell,进入 wsl,cd 到 home:

安装git,配置 email 与 name:
arduino
sudo apt-get install git
安装python,repo依赖2.x,所以需要安装2.x版本的:
sudo apt install python
初始化repo环境:
bash
mkdir ~/bin
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo
chmod +x repo

设置环境变量:
bash
vim ~/.bashrc
添加下面两行环境变量配置:
ini
PATH=~/bin:$PATH
export REPO_URL=https://mirrors.tuna.tsinghua.edu.cn/git/git-repo
使环境变量生效:
bash
source ~/.bashrc
在你想要存放源码的地方建立一个目录,比如 aosp,并切换到该目录:

初始化源码目录(因为我不需要源码的 commit ,所以设置了 ---depth=1):
bash
repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest -b android-7.1.2_r33 --depth=1
成功如下图:

同步源码(-c 似乎失效了):
bash
repo sync -c -j 8
只需要15分钟,总共 32G,源码就下载完成了:

关注我的微信公众号: 二手的程序员
编译源码
Android-7.1.2 源码依赖的是OpenJDK 8版本,因此需要安装配置下:
sql
sudo apt-get install openjdk-8-jdk
sudo update-alternatives --config java
安装依赖工具链
css
sudo apt-get install libncurses5 libgl1-mesa-dev g++-multilib flex bison gperf build-essential tofrodos python-markdown libxml2-utils xsltproc dpkg-dev libsdl1.2-dev git-core gnupg flex bison gperf zip curl zlib1g-dev gcc-multilib libc6-dev-i386 x11proto-core-dev libx11-dev libgl1-mesa-dev libxml2-utils xsltproc unzip m4 ccache
进入源码目录,可以开始编译了:

执行:
bash
source build/envsetup.sh

执行:
lunch

如果是编译模拟器镜像的话,这里直接可以选了,因为我要刷 pixel ,所以还要下载驱动。
pixel驱动下载
官网查看源码对应的 build ID:

驱动官网下载对应的驱动:

下载到桌面后,将其拷贝到源码目录,windows 可以直接看到 wsl 的目录:

然后解压文件:
tar -zxvf qcom-sailfish-nzh54d-56bb51e3.tgz
tar -zxvf google_devices-sailfish-nzh54d-cc9ac173.tgz
得到两个 .sh 文件,都执行一下:
bash
./extract-google_devices-sailfish.sh
./extract-qcom-sailfish.sh
然后按回车来阅读协议,但是由于协议过长,可以按ctrl+c
进行中断,在此会提示需要输入I ACCEPT
,搞定后目录多出来一个 vendor
目录。
继续编译

pixel 对应 sailfish,所以我们选择 18:

然后执行 make -j8
,就可以吃饭去了。
编译:
make -j8
,8
表示线程数,最好和CPU核数一致。
如编译成功,得到如下结果:

编译错误

解决:Remove TLSv1, TLSv1.1 from jdk.tls.disabledAlgorithms in /etc/java-8-openjdk/security/java.security file
不用启动 jack-server,编译时会自动启用,否则会导致端口占用,有点奇葩。
刷机
adb 与 fastboot 都在 android sdk 里面
因为windows提供了直接访问 wsl 文件的方法,所以我们只需要设置一下 ANDROID_PRODUCT_OUT
,就可以直接执行 fastboot 命令了。
adb reboot bootloader
然后使用 fastboot 查看可用设备,如果找不到就是设备驱动问题,找个google的usb驱动装一下,可从官网下载:developer.android.com/studio/run/...
然后执行刷机操作:
fastboot -w flashall

刷机成功!!!
如果刷机后无法启动,找个对应的google的刷机包下载下来,刷回来:

再重新刷一下编的镜像,因为可能你的现有版本不对应,flashall 也只是刷了几个编译出来的主要的镜像(boot.img
、system.img
、recovery.img
、cache.img
、userdata.img
)而已。
测试
在 as 里面查看pixel可调试进程,发现所有都是可调试的:

收工!!!
有兴趣的可以自己改Android 源码,然后编译,刷机。这里我们将 github.com/youlor/unpa... 的代码替换一下,一个可以脱壳的rom就搞定了。
最后
手机启动后可能会出现如下弹窗:

也可以不用管,但是每次重启都会弹出一次,强迫症可以将相关源码弹窗逻辑注释掉,再次编译刷机。