Working with Android Emulator through Command Line

It took me an entire evening to sort things out with the android emulator issues and configurations. I don’t have Android Studio installed on the machine — kind of redundancy when I won’t use it for Java or Android app development.

I’m using Windows for mobile apps development through Ionic Framework. If you don’t know what it is, please have a read.

I suppose that you have sdk command line tools installed in your local machine. And please add to the environment PATH in order to use the command without going to the sdk tools directory.

Below are the useful commands that I usually use to manage and run the android emulator

View all packages

sdkmanager --list

Update installed packages

sdkmanager --update

If you encounter this error: “Failed to move away or delete existing target file”, please visit the solution from stackoverflow. I have to put the tools folder to a temporary location, and update from there.

C:\Users\Dale Nguyen\temp\tools\bin> .\sdkmanager.bat --sdk_root="C:\Users\Dale Nguyen\sdk-tools" --update

Add system images

Before creating an emulator, you have to add the system-image first. You can view system image list by running view all packages command.

sdkmanager "system-images;android-24;default;x86_64"

Create an android emulator

avdmanager create avd -n emulator_name -k "system-images;android-24;default;x86_64" -g "default"

View a list of created emulators

avdmanager list  avd

Delete an emulator

avdmanager delete avd -n emulator_name

Troubleshooting

  1. Incompatible version of Google play services

One thing that took me a huge amount of time to recognize is there are some projects that require Google Play Services to work with. You will see this error when running into the same issue.

“The version of the Google play services installed on this device is not authentic”

In order to resolve this problem, one needs to create an emulator from a google_apis_playstore system image.

sdkmanager "system-images;android-27;google_apis_playstore;x86"

If the emulator requires an update for the Google Play Services, you can log in by using your Google account and update it.

2. Error: Cannot read property ‘semver’ of null

Another issue that you may encounter is “Error: Cannot read property ‘semver’ of null” . There is also a solution for this from stackoverflow. What I did to solve mine is to edit the emulator.js file from

// <project_root>/platforms/android/cordova/lib/emulator.js

3. Missing fb_app_name and fb_app_id

If you are running android emulator though cordova, you may see this issue. There is a solution from github.

resource string/fb_app_id (aka io.ionic.starter:string/fb_app_id) not found.

I have to manually add the fb_app_name and fb_app_id to string.xml file from:

<project_root>/platforms/android/app/src/main/res/values/strings.xml

4. adb shell error: device unauthorized

The app cannot be installed to your emulator and you will see these errors:

adb shell error: device unauthorized.
This adb server's $ADB_VENDOR_KEYS is not set
Try 'adb kill-server' if that seems wrong.
Otherwise check for a confirmation dialog on your device.

Thanks to Ghassan from StackExchange, there is a way to turn your emulator to developer mode:

Settings -> About emulated device -> click on "Build number" several times

After this, you have to turn off the emulator and run the command again. You can check the connected devices by running adb devices.

C:\Users\Dale Nguyen> adb devices
List of devices attached
emulator-5584 device

You may also want to add the the time out preference to config.xml under android platform (stackoverflow)

<preference name="loadUrlTimeoutValue" value="700000" />

If you are using Ionic Framework, run these commands to restart the emulator

ionic cordova build android 
ionic cordova emulate android --target emulator_name

5. Execution failed for task app:transformClassesWithDesugarForDebug

I fixed this one by simply delete “build/intermediates” directory and run the emulator again.

6. Execution failed for task ‘:app:transformDexArchiveWithExternalLibsDexMergerForDebug’

This is a stubborn error. I have to test a lots of scenarios for it to work. You can try these combination solutions:

  • Method 1
cordova clean android
cordova build android

In \platforms\android\project.properties, replace base number and ads number by “+” only or “11.8.0” for google play services

cordova.gradle.include.1=cordova-plugin-googlemaps/app-tbxml-android.gradle

7. Error: Device communication timed out. Try unplugging & replugging the device

One solution that I found on StackOverFlow is to change the cordova package from “platforms\android\cordova\node_modules\q\q.js

// Line 1849 (Time out after...)
deferred.reject(error, 40000);

If you encounter any issues that involves in Android SDK, please leave a comment ;)

Full Stack Developer / JavaScript Enthusiast/ http://dalenguyen.me