Using ADB to Pull APKs off Device

ADB Help for pull and shell

It is sometimes helpful to pull an APK from a working device so you can install it on a different device. These commands should work on an emulator, phone, tablet, or other Android device. You just need to be able to connect with ABD.

  • Connect to device with ADB
  • View installed apps
  • Find path for APK
  • Pull/Download APK

View Installed Apps

This will display a list of all the installed packages.

adb shell pm list packages 

Find path for specific App/APK

Replace com.android.apk with the app of interest.

adb shell pm path com.android.apk

Pull APK to local machine

Pull/Download the APK of interest to your local machine with the following command. Change the path “/data/app/…” to the path returned from the previous command.

adb shell pull /data/app/info/base.apk

You can view the following link for more information.

https://stackoverflow.com/questions/4032960/how-do-i-get-an-apk-file-from-an-android-device

Advanced Tricks

What if you need to get an APK off a secondary profile, or would like to download all the APKs off a system? And what about split APKs?

Multiple User Profiles

Run the following command to list the users.

adb shell pm list users

Example return

Users:
        UserInfo{0:User:a41} running
        UserInfo{11:User:439} running

In this case our second user id is 11. To get a list of APKs installed for our second user we would specify the –user= option

adb shell pm list packages --user=11

To get the path for the app we would run it with

adb shell pm path --user=11 com.android.apk

Split APKs

Split APKs can be slightly more difficult to manage, mainly due to the fact that there are multiple APKs to keep track of.

When you run the “pm path” command, it should return multiple APKs. Use the pull command like normal, but download each APK.

You’ll need to use a split APK installer to install all the APKs.

PowerShell script for Pulling/Downloading all APKs on Device

The following PowerShell script will download all APKs for a specific user and put them in their own folders.

  • Copy the contents to a .ps1 file
  • Enable ps1 execution policy if not already enabled
  • Run PowerShell script.

This script will pull all the APKs off of a device and put them in the current folder.
It will also download split APKs.

# adbapkbackup uses adb to get a list of all the APKs you have on a phone and then
# Creates folders for each app and downloads the APKs for those apps.

# Copy and save code as a ps1 file

# Enable ps1 scripts to run on your computer by launching an Admin promopt and running
# set-executionpolicy remotesigned

# If you are in a secondary profile, add and/or modify
#  "--user 15"
# to your user id
# adb shell pm list users

# If in secondary profile, add "--user 15" after packages before > apklist.txt
adb shell pm list packages --user 15 > apklist.txt

 $apks = ((Get-Content .\apklist.txt)) -replace 'package:',''

 ForEach ($apk in $apks) {
    echo "APK is $apk"
    md $apk
    # If in secondary profile, add "--user 15" after path, before $file
    adb shell pm path $apk
    $filepath = ((adb shell pm path --user 15 $apk | % {$_.replace("package:","")}))
    ForEach ($lapk in $filepath | % {$_.replace("package:","")}) {
        echo "pulling $lapk $apk"
        adb pull $lapk $apk
    }
 }

no permissions (user in plugdev group; are your udev rules wrong?)

While trying to use ADB on a Linux Computer, I received the following error from “adb devices”

no permissions (user in plugdev group; are your udev rules wrong?);

Not exactly sure what causes the above error, but the information in the following Stack Exchange link was helpful.

https://stackoverflow.com/questions/53887322/adb-devices-no-permissions-user-in-plugdev-group-are-your-udev-rules-wrong

I was able to get around the issue by putting the USB option to “File Transfer Mode” from the notifications menu

How to Restore Pixel to Factory Image

Restoring a Pixel to the factory image is a pretty straight forward operation if you are familiar with fastboot and adb. This guide assumes you have fastboot already installed and setup in your user path. If not you can refer to the following link for more information.

WARNING – THESE STEPS WILL DELETE ALL USER DATA OFF THE DEVICE.

https://developers.google.com/android/images#instructions

1. Download OTA Image

Go to the following link and download the Factory Image for your device

https://developers.google.com/android/images

Extract the file and then open a terminal or command prompt in that directory.

2. Boot up Pixel in recovery

You can do this with “adb reboot recovery” or with the volume key to boot into the Android boot menu.

3. Flash Image

On Windows you can flash the firmware with

flash-all.bat

Or on Linux

./flash-all.sh

Should take it a couple of minutes to complete.

Next we can lock bootloader with

fastboot flashing lock

You will need to confirm the lock on your phone.

Add ADB path to Windows Environment Variables

From the start menu search for “Edit environment variables for your account”

Click Path and then Edit.

Edit Environment Variables

Then click new and paste in the path to the ADB directory which should be

%USERPROFILE%\AppData\Local\Android\sdk\platform-tools

And hit OK

Adding ADB path

You should be good now. You’ll need to relaunch and Command Prompts you have open, but you should be able to run adb without having to navigate to the adb folder.

TWRP – ADB Sideload

Go to https://twrp.me/Devices and download the correct version TWRP for your device.

Boot into boot loader menu

Plug you phone into a computer that has fastboot installed and run

sudo fastboot boot Downloads/twrp-3.3.0-0-sailfish.img

Note you may need to unlock your boot loader.

Your phone should now boot up into TWRP

Boot into TWRP

Swipe to Allow Modifications

Select Advanced

Select advanced

Then hit ADB Sideload.

Swipe to start the sideload and then sideload from your computer

adb sideload image.zip

More detailed explaination
https://www.cyanogenmods.org/adb-sideload-twrp/

Extract Android backup

https://stackoverflow.com/questions/18533567/how-to-extract-or-unpack-an-ab-file-android-backup-file

Install Android Backup Toolkit

wget https://downloads.sourceforge.net/project/adbextractor/android-backup-tookit-20180521.zip
unzip android-backup-tookit-20180521.zip
cd android-backup-tookit/android-backup-extractor/android-backup-extractor-20180521-bin

Unpack backup. Changes it from an ab to a tar file

java -jar abe.jar unpack ~/path/to/backup.ab ~/path/to/backup.tar

After thats complete, you can untar it. Change Desktop to the path you want to extract to.

tar -xvf ~/path/to/backup.tar -C ~/Desktop/

Backup Android with adb on Windows

Install SDK

Install Android Studio. This will install the SDK and tools like adb and fastboot

Download from here

To run adb, you’ll need to be in the same directory as it, so open a Command Prompt and cd to the following directory.

cd %APPDATA%..\Local\Android\Sdk\platform-tools

Full directory path is

C:\Users\UserName\AppData\Local\Android\Sdk\platform-tools

Backup Android

On your Android device you will need to enable USB debugging from the developer options. Enable the developer options if you have not already.

When you plug your phone in it should ask you to Allow USB debugging. Hit OK to enable it.

Now run the backup command from your computer. Replace UserName with your computers User Name

adb backup -apk -obb -shared -all -f \Users\UserName\Desktop\android-backup.ab

On your phone you should get a window that shows Full Backup

Add optional password for backup and hit “Back up my data on phone”

Now Wait for it to finish…

Restore Backup

adb restore C:\Users\UserName\android-backup.ab

Should get a restore window on your phone, hit restore and wait.

Errors

error: device unauthorized.
This adb server's $ADB_VENDOR_KEYS is not set
Try 'adb kill-server' if that seems wrong.
Otherwise check for a confirmation dialog on your device.

USB Debugging is not allowed for computer. Try replugging the USB cable and accepting the USB Debugging.