Android Emulator does not start on macOs Fix

/ April 29, 2021/ Android, macOS, Programming

(Last Updated On: April 29, 2021)

Or How to Fix android emulator HV_ERROR that triggers “The emulator process for avd was killed” problem.

I got this problem after update my macOs to Big Sur 11.3 but this can also happen in other macOs versions. After I wanted to start the emulator from Android Studio like always this popup was presented to me:

AVD was Killed Error Popup

Failing Intents

I was a good couple of hours (if not more) trying to find a fix for this without success, I tried:

  • Reinstall the emulator in Android Studio SDK manager
  • Reinstall HAXM
  • Change the emulator graphics to just software/hardware
  • Update everything Android
Ad:

Console output

Nothing worked. Since I did not knew what was trigger the problem I went to where the emulator executable is in my mac: ~/Library/Android/sdk/emulator and I started the emulator manually with. ./emulator -avd Pixel_3a_API_30. This was the output

emulator: Android emulator version 30.5.5.0 (build_id 7285888) (CL:N/A)
handleCpuAcceleration: feature check for hvf
cannot add library /Users/saninn/Library/Android/sdk/emulator/qemu/darwin-x86_64/lib64/vulkan/libvulkan.dylib: failed
added library /Users/saninn/Library/Android/sdk/emulator/lib64/vulkan/libvulkan.dylib
HVF error: HV_ERROR
qemu-system-x86_64: failed to initialize HVF: Invalid argument
HAX is working and emulator runs in fast virt mode.
qemu-system-x86_64: Back to HAX accelerator
emulator: INFO: GrpcServices.cpp:301: Started GRPC server at 127.0.0.1:8554, security: Local

The important part there is:

HVF error: HV_ERROR
qemu-system-x86_64: failed to initialize HVF: Invalid argument

After searching a little big more I found a reddit post about virtual machines where states the hidden problem:

Apple won’t let the QEMU binary run with -accel hvf unless the binary is signed. Every time hv_vm_create() runs, it returns an HV_ERROR.

https://www.reddit.com/r/VFIO/comments/kdhgni/qemu_hvf_support_for_mac_os_x_bug_sur_hv_error/
Ad:

Finally a fix

So, that’s it! we have to manually sign the binary!

To fix the problem we have to go to where our emulator app lives, in my case I went to ~/Library/Android/sdk/emulator/qemu/darwin-x86_64 and created a new file called app.entitlements with this content:

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0"> <dict> <key>com.apple.security.hypervisor</key> <true/> </dict> </plist>

After we sign it with codesign -s -- --entitlements app.entitlements --force qemu-system-x86_64

That’s it! your emulator should be working again!

Ad:
Spread the love
Subscribe
Notify of
guest
7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Dani
Dani
2 years ago

Awesome post, I was having the same issue. Thanks for sharing.

nit: the signing has an extra dash

codesign -s - --entitlements app.entitlements --force qemu-system-x86_64
Brandon
Brandon
2 years ago

Sweet! Thanks for this fix!

Pedro
Pedro
2 years ago

“error: The specified item could not be found in the keychain.”

Something to help about this error?

wellington
wellington
Reply to  Pedro
2 years ago

corret : “codesign -s – –entitlements app.entitlements –force qemu-system-x86_64″…

Last edited 2 years ago by wellington
mahshad
mahshad
Reply to  wellington
1 year ago

Try this, worked for me:
codesign -s "$(security find-identity -v -p codesigning | awk -F '"' 'NR==1{print $2}')" --entitlements app.entitlements --force qemu-system-x86_64

jords
jords
1 year ago

You’re a legend! Helped a lot thanks!!