Android Emulator 构建和启动
AVD 的创建和启动
欢迎入群交流

编译模拟器
下载Android源码
参考文章
Android AOSP 下载和编译
source ./build/envsetup.sh编译x86-64,userdebug版本
lunch sdk_phone_x86_64-userdebug编译arm64,userdebug版本
lunch sdk_phone_arm64-userdebugmake -j32编译sdk
make -j32 sdk sdk_repo会在 aosp-master/out/host/linux-x86/sdk/sdk_phone_x86 下创建两个文件:sdk-repo-linux-system-images-eng.[username].ziprepo-sys-img.xml
启动模拟器
-
安装JDK17
-
下载command-tools
https://developer.android.com/studio?hl=zh-cn#command-tools
下载上述基本的 Android 命令行工具。使用随附的 sdkmanager
下载其他 SDK 软件包。
- 创建sdk文件夹,移动cmdline-tools到sdk目录
cmdline-tools 目录中,创建一个名为 latest 的子目录。将原始 cmdline-tools 目录内容(包括 lib 目录、bin 目录、NOTICE.txt 文件和 source.properties 文件)移动到新创建的 latest 目录中
- 设置环境变量 /etc/profile
export JAVA_HOME=/tools/jdk-17.0.1/export PATH=$JAVA_HOME/bin:$PATHexport CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/libexport ANDROID_SDK_ROOT=/tools/sdk/export ANDROID_HOME=/tools/sdk/export PATH=$ANDROID_HOME/emulator/:$PATHexport PATH=$ANDROID_HOME/platform-tools/:$PATHexport PATH=$ANDROID_HOME/cmdline-tools/latest/bin/:$PATH
- 安装必要的工具
sdkmanager "platform-tools"
sdkmanager "emulator"
sdkmanager "system-images;android-26;default;x86_64"
sdkmanager "platforms;android-26"
组合下载使用空格隔开
sdkmanager "platform-tools" "emulator" "system-images;android-26;default;x86_64" "platforms;android-26"
- 创建模拟器
avdmanager create avd -n android26 -k "system-images;android-26;default;x86_64"
- 检查环境
检查是否安装vm-linux
emulator -accel-check
INFO | Storing crashdata in: /tmp/android-root/emu-crash-34.1.20.db, detection is enabled for process: 2623
accel:
8
/dev/kvm is not found: VT disabled in BIOS or KVM kernel module not loaded
accelegrep -c '(vmx|svm)' /proc/cpuinfo
输出为 1 或更大意味着支持虚拟化。输出 0 表示您的 CPU 不支持硬件虚拟化。kvm-ok
Expected output:
INFO: /dev/kvm exists
KVM acceleration can be usedInstall KVM on Linux
sudo apt-get install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils
VM加速有以下限制:
您无法在另一个虚拟机(例如由 VirtualBox、VMWare 或 Docker 托管的虚拟机)内运行虚拟机加速模拟器。您必须直接在主机上运行 VM 加速模拟器。
根据您的操作系统和虚拟机管理程序,您可能无法在运行 VM 加速模拟器的同时运行使用其他虚拟化技术的软件。此类软件的示例包括但不限于虚拟机解决方案、某些防病毒程序和一些反作弊解决方案。这种情况主要发生在关闭 Hyper-V 的 Windows 上。大多数此类软件可以与android模拟器共存,没有任何问题。但当发生冲突时,建议不要同时运行VM加速模拟器和此类软件。
如果要在容器中开启kvm,需要在创建容器的时候加 –privileged 参数启动容器,这样容器将拥有更多的权限,包括访问宿主机的硬件虚拟化功能
docker run --privileged -it your_image_name
- 启动模拟器
emulator -avd android26 -no-window 后台启动nohup emulator -avd android26 -no-window &
- 自定义的模拟器启动
编译的模拟器路径:
out/host/linux-x86/sdk/sdk_phone_x86_64/android-sdk_eng.root_linux-x86/system-images/android-8.1.0/x86_64sdk路径
将编译好的模拟器镜像复制到sdk/system-images,路径sdk/system-images/android-26/default/x86_64_2将自动创建模拟器生成的 package.xml,复制到此文件夹修改package.xml路径January 16, 2019</license><localPackage path="system-images;android-26;default;x86_64_2",保存后创建模拟器avdmanager create avd -n android26_2 -k "system-images;android-26;default;x86_64_2"
遇到的问题
- 启动模拟器报错
ProbeKVM: This user doesn't have permissions to use KVM (/dev/kvm).
The KVM line in /etc/group is: [LINE_NOT_FOUND]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 kvmIf we see kvm:... but no username at the end, running the following command may allow KVM access:sudo gpasswd -a $USER kvmYou 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: [LINE_NOT_FOUND]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 kv
More info on configuring VM acceleration on Linux:
https://developer.android.com/studio/run/emulator-acceleration#vm-linux
General information on acceleration:解决方法:
sudo addgroup kvm
sudo usermod -a -G kvm secneo
sudo chown secneo -R /dev/kvm
- 编译模拟器报错
error: ro.build.fingerprint cannot exceed 91 bytes: Android/mini_emulator_x86/mini-emulator-x86:5.0.555/AOSP/username02280306:userdebug/test-keys (97)
make: *** [out/target/product/mini-emulator-x86/system/build.prop] Error 1
make: *** Deleting file `out/target/product/mini-emulator-x86/system/build.prop'
make: *** Waiting for unfinished jobs....
error: static_assert failed "Size mismatch."解决方法:
build/tools/post_process_props.py. Change lines as follows
PROP_NAME_MAX = 31
# PROP_VALUE_MAX = 91
PROP_VALUE_MAX = 128bionic/libc/include/sys/system_properties.h. Change lines as follows:
#define PROP_NAME_MAX 32
// #define PROP_VALUE_MAX 92
#define PROP_VALUE_MAX 128system/bt/osi/include/properties.h
#define PROPERTY_VALUE_MAX 128
//#define PROPERTY_VALUE_MAX 92frameworks/native/cmds/installd/installd_deps.h
// constexpr size_t kPropertyValueMax = 92u;
constexpr size_t kPropertyValueMax = 128u;make clean
make
微信公众号(他晓),关注并转发,谢谢
