最近又个客户使用 Android.aarch64 平台总是有问题,于是决定装一个 Android 的模拟器跑一下看看。因为是调试动态库,所以不需要图形界面,这样安装的东西少,启动也快,并且不需要启动桌面系统。
下面是在 Ubuntu 14 服务器版本下面,使用 ssh 远程登录,在控制台运行模拟器 (Android arm64),并调试动态库的基本步骤
首先增加一个源,安装并配置 openjdk-8-jdk。Ubuntu 14 原装包里面的 JDK 是 7,Android SDK 中的命令行工具至少要求 8 以上
sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-8-jdk
sudo update-alternatives --config java
sudo update-alternatives --config javac
其次下载Andorid SDK 命令行工具
https://developer.android.google.cn/studio?hl=zh-cn#downloads
从这里下载 Linux 下面的 Command line tools only
,并解压下载好的压缩文件,例如
unzip commandlinetools-linux-7302050_latest.zip
ls cmdline-tools
然后使用工具里面的 sdkmanager
下载 build-tools, platform (android 24),system images和模拟器,
cd cmdline-tools
# 查看包
bin/sdkmanager --sdk_root=latest --list
# 安装包
bin/sdkmanager --sdk_root=latest "build-tools;26.0.0"
bin/sdkmanager --sdk_root=latest "platforms;android-24"
bin/sdkmanager --sdk_root=latest "system-images;android-24;default;arm64-v8a"
bin/sdkmanager --sdk_root=latest "emulator"
使用平台工具中的 avdmanager
创建 avd
latest/tools/bin/avdmanager create avd --name test24 -k "system-images;android-24;default;arm64-v8a"
使用模拟器里面的工具 qemu-system-aarch64-headless
启动 avd, gpu 模式使用 guest
,额外指定相关动态库路径 ./latest/emulator/lib64
,这样才能成功启动,为了能上传文件,还需要指定选项 -writable-system
sudo adb start-server
sudo LD_LIBRARY_PATH=./latest/emulator/lib64 latest/emulator/qemu/linux-x86_64/qemu-system-aarch64-headless -gpu guest -no-audio -no-snapshot -writable-system -avd test24
重新打开一个终端,就可以使用 adb
上传文件和运行相关命令了
sudo adb devices -l
# 使用 root 用户进行操作
sudo adb root
# 上传文件到 /mnt
sudo adb push pyarmor-core/platforms/android.aarch64.0/_pytransform.so /mnt
# 登录模拟器
sudo adb shell
generic_arm64:/ $
常见错误
- 使用 openjdk-7 运行 Android 的命令行工具会报错
Unsupported major.minor version 52.0
,解决方案是安装 openjdk-8-jdk
$ bin/sdkmanager
Exception in thread "main" java.lang.UnsupportedClassVersionError:
com/android/sdklib/tool/sdkmanager/SdkManagerCli : Unsupported major.minor version 52.0
- 直接运行 emulator,则报错
libdbus-1.so.3: no version information available
jondy@dashingsoft:~/android/cmdline-tools$ latest/tools/emulator -avd test24
/home/jondy/android/cmdline-tools/latest/emulator/qemu/linux-x86_64/qemu-system-aarch64: /lib/x86_64-linux-gnu/libdbus-1.so.3: no version information available (required by latest/emulator/lib64/qt/lib/libQt5WebEngineCoreAndroidEmu.so.5)
- 解决方案是使用直接使用模拟器目录下面的
qemu-system-aarch64-headless
,但是找不到动态库libtcmalloc_minimal.so.4
jondy@dashingsoft:~/android/cmdline-tools$ sudo latest/emulator/qemu/linux-x86_64/qemu-system-aarch64-headless -avd test24
latest/emulator/qemu/linux-x86_64/qemu-system-aarch64-headless: error while loading shared libraries: libtcmalloc_minimal.so.4: cannot open shared object file: No such file or directory
- 解决方案是指定 LD_LIBRARY_PATH ,因为相关的动态库就在模拟器所在的目录下面。但是还是报错
EglOsGlLibrary
jondy@dashingsoft:~/android/cmdline-tools$ sudo LD_LIBRARY_PATH=./latest/emulator/lib64 latest/emulator/qemu/linux-x86_64/qemu-system-aarch64-headless -avd test24
emulator: WARNING: encryption is off
emulator: feeding guest with passive gps data, in headless mode
EglOsGlLibrary: Could not open GL library libGLESv2.so [libGLESv2.so: cannot open shared object file: No such file or directory]. Trying again with [libGLESv2.so.2]
EglOsGlLibrary: Could not open GL library libGLESv2.so.2 [libGLESv2.so.2: cannot open shared object file: No such file or directory]
EglOsEglDispatcher: Could not open EGL library libEGL.so [libEGL.so: cannot open shared object file: No such file or directory]. Trying again with [libEGL.so.1]
EglOsEglDispatcher: Could not open EGL library libEGL.so.1 [libEGL.so.1: cannot open shared object file: No such file or directory]
- 解决方案是使用选项
-gpu guest
,指定gpu
的模式为guest
jondy@dashingsoft:~/android/cmdline-tools$ sudo LD_LIBRARY_PATH=./latest/emulator/lib64 latest/emulator/qemu/linux-x86_64/qemu-system-aarch64-headless -gpu guest -avd test24
emulator: WARNING: System image is writable
emulator: WARNING: encryption is off
emulator: feeding guest with passive gps data, in headless mode
emulator: INFO: GrpcServices.cpp:315: Started GRPC server at 127.0.0.1:8554, security: Local
WARNING. Using fallback path for the emulator registration directory.
emulator: INFO: EmulatorAdvertisement.cpp:93: Advertising in: /home/jondy/.android/avd/running/pid_2057.ini
emulator: Cold boot: requested by the user