User Tools

Site Tools


doc:appunti:android:apk_backup

This is an old revision of the document!


Backup Android apk da riga di comando

Vedere questo post: How do I get an apk file from an Android device?.

#!/bin/sh
#
# Backup Android apk files to PC, using adb command line.
#
# Copyright (C) 2019 Niccolo Rigacci <niccolo@rigacci.org>
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
 
# Some packages are in paths requiring root privileges.
paths_require_root='/mnt/expand/ /data/app/'
# To pull protected apk, need to copy in normal storage.
tmp_path='/sdcard/app_backup'
 
if [ -z "$1" ]; then
    # Get the list of installed packages (was "pm list packages").
    # Each output line is: package:<path>/<file.apk>=<package.name>
    adb shell cmd package list packages -f | rev | cut -f1 -d= | rev
    echo
    echo "  Usage: $(basename $0) [pakage_name]"
    echo
    exit 0
fi
 
package="$1"
if !(adb shell cmd package list packages | cut -f2 -d: | egrep -q "^${package}$"); then
    echo "ERROR: Package \"${package}\" not found."
    exit 1
else
    # TODO: Shoul select only "Packages:", not of "Hidden system packages:" section.
    # TODO: sub-optimal solution is to use head -n1
    version="$(adb shell dumpsys package "${package}" | egrep '^\s*versionName=' | head -n1 | cut -f2 -d=)"
    # TODO: Problem if path contains spaces.
    package_paths="$(adb shell pm path "$package" | grep '^package:' | cut -f2 -d:)"
    for pm_path in $package_paths; do
        echo "Getting file: $pm_path"
        apk_name="$(basename "$pm_path")"
        dst_filename="${package}-${version}_${apk_name}"
        su_required='false'
        for p in $paths_require_root; do
            if (echo "$pm_path" | egrep -q "^$p"); then
                su_required='true'
            fi
        done
 
        if [ "$su_required" = "true" ]; then
            echo "WARNING: Using root privileges to copy .apk to ${tmp_path}"
            # Check if temporary diretcory exists.
            if ! adb shell cd "${tmp_path}" 2> /dev/null; then
                echo "ERROR: Temporary directory \"$tmp_path\" does not exists."
                exit 1
            fi
            # Copy apk to temporary directory, using "su" privileges.
            if ! adb shell su --command "cp \"$pm_path\" \"${tmp_path}/${dst_filename}\""; then
                echo "ERROR: Cannot copy apk to temporary directory."
                exit 1
            fi
            adb pull "${tmp_path}/${dst_filename}" .
            RET=$?
            adb shell rm "${tmp_path}/${dst_filename}"
        else
            adb pull "$pm_path" "${dst_filename}"
            RET=$?
        fi
        if [ $RET -ne 0 ]; then
            echo "ERROR: \"adb pull\" returned error code $RET for ${dst_filename}"
        else
            echo "INFO: Downloaded file \"${dst_filename}\""
        fi
    done
    exit
fi
doc/appunti/android/apk_backup.1564756415.txt.gz · Last modified: 2019/08/02 16:33 by niccolo