Wednesday, May 01, 2019

Gotcha of the Day: Grabbing Android Dev Screenshots from MacOS

This morning I found myself struggling to extract screenshots from an Android test phone. The device was plugged into a Mac, and when I opened  Finder there was no obvious way to browse the filesystem on the phone. And because this is a test phone, it doesn't have my Google Account. Without this account, screenshots aren't automatically pushed to cloud backup, where I can swipe them from my Desktop.

After some Googling I recalled that if I've got USB Debug Access to an Android phone, then I can trivially pull down screenshots using adb. The recipe is described here and boils down to running the 'screencap' command on the device via an adb shell.

$ adb shell screencap /sdcard/screencap.png ; adb pull /sdcard/screencap.png

There's an analogous screenrecord command which does a video capture of the screen. This is so cool and is crying out for being put to use.

I took my re-found knowledge and updated my ionicassist script. Grabbing dev screenshots, whether I'm on Windows or Mac, is now a breeze.

#!/bin/bash

##
## Scrip to help with ionic dev
##

action=$1 ; shift

case $(uname) in 
  Darwin)
    PLATFORM_TOOLS=$HOME/Library/Android/sdk/platform-tools/
    ;;
  Cygwin)
    PLATFORM_TOOlS=/d/tools/android/platform-tools/
    ;;
esac

ADB=$PLATFORM_TOOLS/adb

case "$action" in
  grab-apk)
    name=$(xmlstarlet sel -N x=http://www.w3.org/ns/widgets  -t -v '/x:widget/x:name' config.xml | sed 's/[^A-Za-z0-9_-]//g')
    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
    ;;

  grab-release-apk)
    name=$(xmlstarlet sel -N x=http://www.w3.org/ns/widgets  -t -v '/x:widget/x:name' config.xml | sed 's/[^A-Za-z0-9_-]//g')
    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/release/app-release.apk ~/dl/$name-$version.apk
    ;;

  screencap)
    if [ -z "$1" ] ; then
      echo "Usage: $(basename $0) screencap {file}"
      exit
    else
      file=$1 ; shift
      $ADB shell screencap /sdcard/screen.png
      $ADB pull /sdcard/screen.png $file
    fi
    ;;


  screenrec)
    if [ -z "$1" -o -z "$2" ] ; then
      echo "Usage: $(basename $0) screencap {file} {duration}"
      exit
    else
      file=$1 ; shift
      duration=$1 ; shift
      $ADB shell screenrecord --verbose --time-limit $duration /sdcard/screen.mp4
      $ADB pull /sdcard/screen.mp4 $file
    fi
    ;;

  *)
    echo "Usage: $(basename $0) {grab-apk|grab-release-apk|screencap}"
    exit 1
    ;;
esac

No comments:

Post a Comment