Android Snippets

September 29, 2017

Over the years you aquire little snippets / shell script helpers to get things done.. Here’a collection of a few we use regularily for Android development.

ADB commands

Grab a screenshot

adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > device_screenshot.png

Dump shared preferences

export PACKAGE_NAME=com.fireseed.example
adb shell run-as ${PACKAGE_NAME}  cat shared_prefs/${PACKAGE_NAME}_preferences.xml

Clear app data

export PACKAGE_NAME=com.fireseed.example
adb shell pm clear ${PACKAGE_NAME}

Dump activities

It can be really helpful to know what is actually running and what all the tasks stacks are:

adb shell dumpsys activit

Firebae logging

Turning it on:

export PACKAGE_NAME=com.fireseed.example
adb shell setprop ${APP}
adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSE

Turning it off (for all apps):

adb shell setprop .none.

Grab a device database

Although is normally used for getting a Realm database it can be used for any binary file.
We download the latest version and then open it in the Realm browser (on a mac)

export PACKAGE_NAME=com.fireseed.example
NOT_PRESENT="List of devices attached"
ADB_FOUND=`adb devices | tail -2 | head -1 | cut -f 1 | sed 's/ *$//g'`

if [[ ${ADB_FOUND} == ${NOT_PRESENT} ]]; then
    echo "Make sure a device is connected"
	killall "Realm Browser"

    adb shell "
        run-as ${PACKAGE_NAME} cp /data/data/${PACKAGE_NAME}/files/${DB_NAME} /sdcard/
    mkdir -p "${DESTINATION_PATH}"
    adb pull "/sdcard/${DB_NAME}" "${DESTINATION_PATH}"
    echo "Database exported to ${DESTINATION_PATH}${DB_NAME}"
comments powered by Disqus