Back button not working on android emulator

Viewed 977

I try to start an android sdk emulator, but when I press the back button this error appears:

INFO    | Critical: Failed to load https://maps.googleapis.com/maps/api/mapsjs/gen_204?csp_test=true: The 'Access-Control-Allow-Origin' header has a value 'qrc://' that is not equal to the supplied origin. Origin 'qrc://' is therefore not allowed access. (qrc:/html/js/common.js:0, (null))

INFO    | Critical: Failed to load https://maps.googleapis.com/maps/api/mapsjs/gen_204?csp_test=true: The 'Access-Control-Allow-Origin' header has a value 'qrc://' that is not equal to the supplied origin. Origin 'qrc://' is therefore not allowed access. (qrc:/html/js/common.js:0, (null))

I use Ubuntu and try to start the emulator through terminal, I use the avdmanager of the google cmdline-tools (latest version) and this is the AVD that I try to use:

    Name: my_avd_29ii
    Path: /home/user/.android/avd/my_avd_29ii.avd
  Target: Google APIs (Google Inc.)
          Based on: Android 10.0 (Q) Tag/ABI: google_apis/x86
  Sdcard: 512 MB
2 Answers

After looking around I found that there were two ways to push hardware events to the emulator:

First method

This method uses telnet, like so:

telnet 127.0.0.1 5554
auth <auth-key>
event send EV_KEY:KEY_BACK:0
event send EV_KEY:KEY_BACK:1

This is going to simulate the pressing down (0) the releasing (1) of a button.

Second method

This method uses adb, like so:

adb shell input keyevent KEYCODE_BACK

Ok so what?

After trying both, I realized that telnet didn't work but adb did work. I also checked the source code of emulator and saw that telnet relies on the same set of functions that are used by the tool-window. So it's probably a bug, and we can wait for the update to fix it, or...

In the meantime

I downloaded the source code, wrote a janky workaround, and built it. Basically instead of using the keypress method, I use the adb method. If you want here it is. It's quite voluminous so be patient. I included a small script to automatically replace the emulator, just run:

$ chmod +x replace
# ./reaplace path-to-android-sdk

This adds a new flag -use-adb-toolwindow to emulator. When you activate it it will use adb (successfully) for most of keyboard inputs, and home, back, apps and power buttons. Like I said it's a quick fix, but for me it works fine.

Note:

I have no clue where the bug is, so I didn't do any report, hopefuly as more people get this issue it will become clearer.

The best option would (Needs to be done for every emulator you install)

Windows

  1. %USERPROFILE%\.adnroid\avd\<Emulator Name>.avd
  2. Edit config.ini
  3. Change hw.keyboard=no to hw.keyboard=yes

Mac

  1. ~/.adnroid/avd/<Emulator Name>.avd
  2. Edit config.ini
  3. Change hw.keyboard=no to hw.keyboard=yes

The answer is based on this article

Related