Skip to content

Instantly share code, notes, and snippets.

@ctrl-freak
Last active November 15, 2024 02:31
Show Gist options
  • Save ctrl-freak/24ac0e61b7cf550a6945 to your computer and use it in GitHub Desktop.
Save ctrl-freak/24ac0e61b7cf550a6945 to your computer and use it in GitHub Desktop.
Retrieve APK from Non-Rooted Android Device through ADB

https://stackoverflow.com/a/18003462/348146

None of these suggestions worked for me, because Android was appending a sequence number to the package name to produce the final APK file name (this may vary with the version of Android OS). The following sequence of commands is what worked for me on a non-rooted device:

  1. Determine the package name of the app, e.g. com.example.someapp. Skip this step if you already know the package name.

    adb shell pm list packages

    Look through the list of package names and try to find a match between the app in question and the package name. This is usually easy, but note that the package name can be completely unrelated to the app name. If you can't recognize the app from the list of package names, try finding the app in Google Play using a browser. The URL for an app in Google Play contains the package name.

  2. Get the full path name of the APK file for the desired package.

    adb shell pm path com.example.someapp

    The output will look something like this: package:/data/app/com.example.someapp-2.apk

  3. Pull the APK file from the Android device to the development box.

    adb pull /data/app/com.example.someapp-2.apk

@AlainMuller
Copy link

Thank you for the info, you can merge the 2 first commands in one using the -f option (which displays associated file) :
adb shell pm list packages -f | grep someapp
-> package:/data/app/com.example.someapp-2/base.apk=com.example.someapp

BTW, a useful trick to quickly find the associated package is to launch the app and find the displayed activity using this command :
adb shell dumpsys activity top | grep ACTIVITY
-> ACTIVITY com.example.someapp/.app.HomeActivity 2dbf6e2 pid=18453

@kwhat
Copy link

kwhat commented Jul 29, 2019

adb pull from the /data directory does not work on modern devices. There is no way around this besides having root.

@andrew-koster-
Copy link

My phone is like a year old and not rooted. adb pull worked fine.

@ctrl-freak
Copy link
Author

I've been doing this successfully regularly on a Samsung A8 and Moto Z3 Play, both on Android 8.0

https://github.com/ctrl-freak/adb-scripts/blob/master/Pull-AndroidPackage.ps1

@sschilz
Copy link

sschilz commented Oct 18, 2019

To extract an app installed from the play store on a Galaxy Tab A Running Android 9
I used the following command from a git bash shell:

$ adb shell "pm list packages -f" | grep video

which returned this data:
package:/data/app/com.samsung.android.videolist-g6xTrW8SV-TC1mpyial9WA==/base.apk=com.samsung.android.videolist

and this command successfully retrieved it:
adb pull /data/app/com.samsung.android.videolist-g6xTrW8SV-TC1mpyial9WA==/base.apk 1 file pulled. 27.9 MB/s (5924177 bytes in 0.202s)

This Android Package Browser was useful because it showed the actual name of the APK was formatted as:
folders+ base name + guid({g6xTrW...})+ "==/base.apk"

meaning the actual filename to be pulled is apparently:
/data/app/com.samsung.android.videolist-g6xTrW8SV-TC1mpyial9WA==/base.apk

@MarkWalters-dev
Copy link

Only partially works on my unrooted android pie. Looks like the permissions on the external sdcard are more strict than the internal. Adoptable storage isn't a good idea without root. Note to self: Next time select "format as portable storage" not "format as internal".

# Only pulls the first apk if multiple matches.  Remove head and use a for loop if you want all.
echo 'apkls() { adb shell pm list packages -f|grep $1;}' >> ~/.bashrc
echo 'apkpull() { adb pull `apkls $1|head -n1|sed "s/package://"|sed "s/\.apk=/.apk /"`.apk;}' >> ~/.bashrc
. ~/.bashrc
mark@p72:/tmp$ apkpull bromite
adb: error: failed to stat remote object '/mnt/expand/7a55b1cc-1941-4bd8-9d92-acab17089911/app/org.bromite.bromite--TapI3VrZImW9gnZXd6MrQ==/base.apk': Permission denied
mark@p72:/tmp$ apkpull termux
/data/app/com.termux-MwJfUSG9B6cWiTp0ai3pkQ==/base.apk: 1 file pulled. 22.1 MB/s (66136654 bytes in 2.848s)

@delrocco
Copy link

Still worked tonight on a Google Pixel 2 with Android 10 / API 29. Developer options and USB debugging enabled.

I needed the .apk from a project we published 3 years ago, and I didn't have the old codebase re-building yet in modern Android Studio. So I just downloaded it from Google Play and pulled it with adb.

@bugfinder123
Copy link

adb shell pm path com.example.someapp and adb pull /data/app/com.example.someapp-2.apk working for me thanks

@beringin2017
Copy link

"Exception occurred while executing 'list': java.lang.SecurityException: Shell does not have permission to access user 150'
anyone know how to fix this?

@ctrl-freak
Copy link
Author

"Exception occurred while executing 'list': java.lang.SecurityException: Shell does not have permission to access user 150' anyone know how to fix this?

From searching for that error, it sounds like you're trying to extract an APK from a Samsung Secure Folder?

I don't have a Samsung device to test, and its outside the scope of the info here as you're involving somewhere protected. I can only suggest:

Confirm user 150 is the Secure Folder:

adb shell pm list users

No idea whether this would work, as I can only see examples online for uninstalling, but could try:

adb shell pm path --user 150 com.example.someapp

If you're able to get the path, then try copying the APK to a shared location, like /sdcard/Android/obb:

adb shell cp /data/app/com.example.someapp-2.apk /sdcard/Android/obb/com.example.someapp-2.apk

Then try pull from there:

adb pull /sdcard/Android/obb/com.example.someapp-2.apk

@renozion
Copy link

folks, how do I download all apk files, in my case I need all apk from phone, is there a way?

@ctrl-freak
Copy link
Author

folks, how do I download all apk files, in my case I need all apk from phone, is there a way?

There'll be a way to script it, but you likely don't want all APKs, as all the system apps would be included.

Simplest would be to pull the list with adb shell pm list packages -f and copy into a spreadsheet. Manipulate with text to columns, etc, then combine adb pull with the file path to create a list of commands. Run them sequentially by copying back into the console window, or save as batch/shellscript and run.

This will give you the opportunity to review the list; you'll likely find you're not interested in maybe two-thirds of what's there.

@renozion
Copy link

folks, how do I download all apk files, in my case I need all apk from phone, is there a way?

There'll be a way to script it, but you likely don't want all APKs, as all the system apps would be included.

Simplest would be to pull the list with adb shell pm list packages -f and copy into a spreadsheet. Manipulate with text to columns, etc, then combine adb pull with the file path to create a list of commands. Run them sequentially by copying back into the console window, or save as batch/shellscript and run.

This will give you the opportunity to review the list; you'll likely find you're not interested in maybe two-thirds of what's there.

I open a ticket on kaspersky and the technician told me he needed all apk stuff, he said:
Allow me to assist you with this request, please provide the Android dumps log with a huge size (Dump usually more than Gb.)
The most generated log file is named android_apps_0

any clue how to do it my friend, can u pls help?

@ctrl-freak
Copy link
Author

any clue how to do it my friend, can u pls help?

Ask support for instructions; they should at least have a link to more details.

@renozion
Copy link

any clue how to do it my friend, can u pls help?

Ask support for instructions; they should at least have a link to more details.

nah man, they show me a way to dump app utility but im on linux so, no good for me.

@ctrl-freak
Copy link
Author

any clue how to do it my friend, can u pls help?

Ask support for instructions; they should at least have a link to more details.

nah man, they show me a way to dump app utility but im on linux so, no good for me.

adb works the same way on Linux and Windows. It's hard to know exactly what they want.

Look into using logcat from a adb shell and run it from before the issue, replicate the problem, and after. You could also use the Developer Options > Bug Report tool (on your device), do an Interactive, replicate your problem, and provide a separate Full.

@renozion
Copy link

Yes

any clue how to do it my friend, can u pls help?

Ask support for instructions; they should at least have a link to more details.

nah man, they show me a way to dump app utility but im on linux so, no good for me.

adb works the same way on Linux and Windows. It's hard to know exactly what they want.

Look into using logcat from a adb shell and run it from before the issue, replicate the problem, and after. You could also use the Developer Options > Bug Report tool (on your device), do an Interactive, replicate your problem, and provide a separate Full.

Yes I am pretty sure it works the same on Linux and windows, but my question is how do I reproduce these steps that they sent me:
_ello,

Thank you for your response. It's greatly appreciated.

Please see step 3 in the link below for how to collect the report;

https://support.kaspersky.com/common/diagnostics/14691#block3

We remain at your disposal.

Thank you and have a nice day!_
Instead of using DumpAndroidApps , i would like to use ADB command, is there any command that does what they are asking?

@ctrl-freak
Copy link
Author

Instead of using DumpAndroidApps , i would like to use ADB command, is there any command that does what they are asking?

No single command, it's likely they're just stringing commands together. There's no way to know exactly what they're doing without trying to decompile the application. At that point you're best off just using someone elses Windows machine.

@renozion
Copy link

All right this is such a pain man, you have no idea, Ive been on this for the last 4 days with their technical customer service and they cant make it work. They need all this:
To process requests, Kaspersky Customer Service might need a report generated by the DumpAndroidApps utility. The report collects the following information from the mobile device:

  1. All APK files from the /data/app and /system/app folders on the mobile device
    
  2. All paths to the APK files on the mobile device are saved to the file AndroidApps.txt.
    
  3. All files:
    
  4.     From the folder /sdcard/Download on the mobile device are copied to the folder android_apps_0/Download on the computer.
    
  5.     From the folder /system/bin on the mobile device are copied to the folder android_apps_0/system_bin on the computer.
    
  6.     From the folder /system/xbin on the mobile device are copied to the folder ndroid_apps_0/system_xbin on the computer.
    

I tried following their tutorial on windows but it is no working aswell. When I ran the adb-setup.exe it did not installed the drivers, then i ran the dumpapputility but the folders were all empty.

@renozion
Copy link

Friends, sorry for any inconvenience, i was able to get the dump file on windows using version 1.3.0 of adb-setup.exe

@ctrl-freak
Copy link
Author

Friends, sorry for any inconvenience, i was able to get the dump file on windows using version 1.3.0 of adb-setup.exe

Glad to hear; if you have trouble in the future, you might find the XDA Developers forums a useful place to work through things like this.

@biblia19
Copy link

biblia19 commented Apr 8, 2024

To extract an app installed from the play store on a Galaxy Tab A Running Android 9 I used the following command from a git bash shell:

$ adb shell "pm list packages -f" | grep video

which returned this data: package:/data/app/com.samsung.android.videolist-g6xTrW8SV-TC1mpyial9WA==/base.apk=com.samsung.android.videolist

and this command successfully retrieved it: adb pull /data/app/com.samsung.android.videolist-g6xTrW8SV-TC1mpyial9WA==/base.apk 1 file pulled. 27.9 MB/s (5924177 bytes in 0.202s)

This Android Package Browser was useful because it showed the actual name of the APK was formatted as: folders+ base name + guid({g6xTrW...})+ "==/base.apk"

meaning the actual filename to be pulled is apparently: /data/app/com.samsung.android.videolist-g6xTrW8SV-TC1mpyial9WA==/base.apk

This still work on Android 14.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment