Tuesday, April 02, 2019

Grabbing an Ionic APK for the Truly Lazy

One of the micro annoyances I encounter with Ionic is the process of sharing an Android debug APK. After running ionic cordova build android I'm left with app-debug.apk under the android platform directory. Copying this file to the correctly named location isn't hard, but it always takes me a few extra heartbeats to do it right. Where's does the APK live again? Should I use _'s or -'s in the final name? What's the version of this app? and so on. Not hard, not even tedious, but always annoying.

I've done this enough that it was time to write a script to do the job. My strategy assumes that the location of the APK is constant and that the destination file name and version can be found in config.xml. Here's the script to grab the APK:

#!/bin/bash

##
## Script to help with ionic dev
##

action=$1

case "$action" in
  grab-apk)
    name=$(xmlstarlet sel -N x=http://www.w3.org/ns/widgets  -t -v '/x:widget/x:name' config.xml)
    version=$(xmlstarlet sel -N x=http://www.w3.org/ns/widgets  -t -v '/x:widget/@version' config.xml)
    cp -v platforms/android/app/build/outputs/apk/debug/app-debug.apk ~/dl/$name-$version.apk
    ;;

  *)
    echo "Usage: {grab-apk}"
    exit 1
    ;;
esac

And here's what it looks like while running:

$ ionicassist grab-apk
`platforms/android/app/build/outputs/apk/debug/app-debug.apk' -> `/home/ben/dl/Foo-3.16.1.apk'

xmlstarlet is the obvious tool to use to grab info from config.xml. I'm always tripped up by the use of namespaces in the XML and XSLT, which meant writing the above script took a bit of extra debugging time. If nothing else, the above code is a helpful reminder to me as to how to navigate a document with namespaces.

Use and enjoy!

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails