在WSL2编译安卓13并在WSL2和windows运行
刚开始的时候,我是在master分支编译安卓的,结果运行emulator起不来,后来切换到android-13.0.0_r35就OK了。
在wsl2编译并运行
以下是编译和运行步骤:
wesley@Wesley:~/aosp$ source build/envsetup.sh
wesley@Wesley:~/aosp$ lunch sdk_pc_x86_64-userdebug
wesley@Wesley:~/aosp$ make -j18
wesley@Wesley:~/aosp$ emulator
可能出现的错误
1、MoTTY X11 proxy: Unsupported authorisation protocolMoTTY X11 proxy: Unsupported authorisation protocol
#普通用户报错,root用户不报错的解决方法:
cp /root/.Xauthority /home/xxx/
#普通用户不报错,root用户报错的解决方法:
cp /home/xxx/.Xauthority /root/
#xxx为用户名
2、kvm无权限访问
wesley@Wesley:~/aosp$ emulator
INFO | Android emulator version 31.3.9.0 (build_id 8748233) (CL:N/A)
INFO | Storing crashdata in: /tmp/android-wesley/emu-crash.db, detection is enabled
INFO | Duplicate loglines will be removed, if you wish to see each indiviudal line launch with the -log-nofilter flag.
ProbeKVM: This user doesn't have permissions to use KVM (/dev/kvm).
The KVM line in /etc/group is: [kvm:x:108:wesley]
If the current user has KVM permissions,
the KVM line in /etc/group should end with ":" followed by your username.
If we see LINE_NOT_FOUND, the kvm group may need to be created along with permissions:
sudo groupadd -r kvm
# Then ensure /lib/udev/rules.d/50-udev-default.rules contains something like:
# KERNEL=="kvm", GROUP="kvm", MODE="0660"
# and then run:
sudo gpasswd -a $USER kvm
If we see kvm:... but no username at the end, running the following command may allow KVM access:
sudo gpasswd -a $USER kvm
You may need to log out and back in for changes to take effect.
ERROR | x86_64 emulation currently requires hardware acceleration!
CPU acceleration status: This user doesn't have permissions to use KVM (/dev/kvm).
The KVM line in /etc/group is: [kvm:x:108:wesley]
If the current user has KVM permissions,
the KVM line in /etc/group should end with ":" followed by your username.
If we see LINE_NOT_FOUND, the
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.
解决办法:
1、创建kvm用户组
sudo groupadd -r kvm #创建kvm用户组
sudo gpasswd -a $USER kvm #添加用户到kvm
2、更改/dev/kvm访问权限
/dev/kvm默认权限是600,只有root用户可以访问,我们需要将其用户组更改为kvm并赋予660权限。
编辑/etc/wsl.conf
[boot]
command = /bin/bash -c 'chown -v root:kvm /dev/kvm && chmod 660 /dev/kvm'
低版本的windows可能没有打开嵌套虚拟化,需要加入:
[wsl2]
nestedVirtualization=true
重启wsl
wsl.exe --shutdown
在windows上运行
由于嵌套虚拟化,目前在wsl2直接运行模拟器会比较卡,我们需要切换到windows运行。
打包 AVD 映像 zip 文件
安卓12开始
make -j18 emu_img_zip
安卓12以下
$ make -j18 sdk sdk_repo
编译完成后会生成以 sdk-repo-linux-system-images
为前缀的 AVD 映像 zip 文件。
比如我的是\home\wesley\aosp\out\target\product\emulator64_x86_64\sdk-repo-linux-system-images-eng.wesley.zip
可以将其解压在当前文件夹或者其他windows路径,我这里选择就地解压:
获得windows访问路径:\\wsl.localhost\Ubuntu-20.04\home\wesley\aosp\out\target\product\emulator64_x86_64\x86_64
在powershell执行:
我已经将C:\Users\Windows\AppData\Local\Android\Sdk\emulator路径添加到环境变量了,Large_Desktop_API_33是我在android studio创建模拟器的名称。
emulator -avd Large_Desktop_API_33 -sysdir \\wsl.localhost\Ubuntu-20.04\home\wesley\aosp\out\target\product\emulator64_x86_64\x86_64 -dns-server 8.8.8.8,114.114.114.114 -verbose
参考资料
从命令行启动模拟器 | Android Studio | Android Developers
创建和管理虚拟设备 | Android Studio | Android Developers
用作开发平台的 Android 虚拟设备 | Android 开源项目 | Android Open Source Project
使用 Android 模拟器虚拟设备 | Android 开源项目 | Android Open Source Project
[AOSP-13 : Not able to create AVD system image from 244956535] - Visible to Public - Issue Tracker
hyper v - How to run KVM nested in WSL2 (or vmware)? - Server Fault
Linux远程图形化界面出错:MoTTY X11 proxy: Unsupported authorisation protocol - 时间的风景 - 博客园