Cordova Build Failure

The other day while I was hacking away on this Ionic-Angular project for work. I was building my android project:

ionic cordova build android -- --production

But I encountered the following unusual error at the end of the build:

...
 BUILD SUCCESSFUL in 9s
  45 actionable tasks: 5 executed, 40 up-to-date
  Command finished with error code 0: /Users/darrenkarlsapalo/git/work/easy-cash/platforms/android/gradlew cdvBuildDebug,-b,/Users/darrenkarlsapalo/git/work/easy-cash/platforms/android/build.gradle
  Built the following apk(s): 
      /Users/darrenkarlsapalo/git/work/easy-cash/platforms/android/app/build/outputs/apk/debug/app-debug.apk
  No scripts found for hook "after_compile".
  No scripts found for hook "after_build".
  [ERROR] An error occurred while running subprocess cordova.
      cordova build android --verbose exited with exit code 1.     Re-running this command with the --verbose flag may provide more information.
  ionic:utils-process onBeforeExit handler: 'process.exit' received +0ms
    ionic:utils-process onBeforeExit handler: running 2 functions +0ms
    ionic:utils-process processExit: exiting (exit code: 1) +31ms

What was unusual was that the production APK build was successful, but there was an error that occurred nonetheless. Why didn’t it work?

You can follow my thought process in investigating this issue. I filed it as a GitHub issue on cordova-useragent repository, where I thought it should be kept in.

Diagnosis

Turns out, it was related to a deprecated naming convention for the identifier of the cordova-useragent plugin.

I discovered the root of the problem through the verbose error logging of the build command:

...
Failed to restore plugin "im.ltdev.cordova.UserAgent" from config.xml. You might need to try adding it again. Error: Error: npm: Command failed with exit code 1 Error output:
npm ERR! code E404
npm ERR! 404 'im.ltdev.cordova.UserAgent' is not in the npm registry.
npm ERR! 404 name can no longer contain capital letters
...

Because the naming convention for the plugin was invalid (which was im.ltdev.cordova.UserAgent), Ionic’s build script failed to regenerate the necessary plugins defined on the package.json. This caused the build failure.

Resolution

There’s two ways to go about addressing the issue. One is the quick workaround, while the other one is the actual fix.

Quick workaround

Remove and re-add the cordova plugin using the following terminal commands:

ionic cordova plugin remove cordova-plugin-useragent
ionic cordova plugin add cordova-plugin-useragent

If the plugin couldn’t be successfully removed, ignore the error. After adding the plugin back, it should add the plugin to your plugins folder, and in turn, your android project.

Actual fix

The actual fix for this problem is to update the plugin with the changes I described, and actually implemented by GitHub user @aaronneon2. A pull request just has to be made and reviewed by the original plugin owner. We’re just not sure if he’s still active.

That’s it! If you want to review or analyze how I debugged this problem, checkout the issue I filed on GitHub!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.