Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova error running on android emulator: "android: Command failed with exit code 2"

I'm trying to run my cordova project in an android emulator:

cordova emulate android

The build is successful but the emulator is not starting and I get an error:

Error: android: Command failed with exit code 2

I get the same thing when running cordova requirements:

Requirements check results for android:
Java JDK: installed 1.8.0
Android SDK: installed true
Android target: not installed
android: Command failed with exit code 2
Gradle: installed /opt/android-studio/gradle/gradle-3.2/bin/gradle
Error: Some of requirements check failed

I've upgraded Android SDK Tools to 26.0.1 and android command doesn't work for me anymore. So I installed [email protected] as the release page says and use it in my project:

$ cordova platform ls
Installed platforms:
  android 6.2.1
Available platforms:
  amazon-fireos ~3.6.3 (deprecated)
  blackberry10 ~3.8.0
  browser ~4.1.0
  firefoxos ~3.6.3
  ubuntu ~4.3.4
  webos ~3.7.0

But the error still happens. Does anyone has any ideas why this happens?

like image 683
jetpackpony Avatar asked Apr 11 '17 19:04

jetpackpony


2 Answers

I think that a new version of Android SDK is not compatible with cordova emulation, so I change this:

return superspawn.spawn('android', ['list', 'avds'])

to this:

return superspawn.spawn('android', ['list', 'avd'])

inside

platforms/android/cordova/lib/emulator.js

and your error was fixed. That happens 'cause the command "android list avds" was changed to "android list avd" inside a new SDK. Thanx to Douglas Neves

If after that you'll catch another error like this:

Failed to install ... Failure [INSTALL_FAILED_VERSION_DOWNGRADE]

You'll need to change this:

var command = 'adb -s ' + target + ' install -r "' + apk + '"';

to this:

var command = 'adb uninstall "' + pkgName + '"; adb -s ' + target + ' install -r "' + apk + '"';

in the same file. This code will uninstall app before install it, so version problem will be disappeared.

like image 182
Wishmaster Avatar answered Nov 16 '22 01:11

Wishmaster


you should update latest platform:

cordova platform remove android
cordova platform update android@latest
like image 37
NgocTP Avatar answered Nov 16 '22 02:11

NgocTP