18

Need some help by retrieving *.apk file name from Android device programmaticaly ? Could any body provide some Android methods doing that or even shell commands run under Android to get the file location , if I know just a part from file name ?

Also if there is a way how to find out where the *.apk file went after downloading from Market or outside Market ?

Thank you in advance.

asteroidg
  • 189
  • 1
  • 1
  • 3
  • Why do you need this information? – Dave Jan 08 '11 at 18:36
  • Perhaps if we knew what you are trying to do it would help in answering the question (since we will have a better idea of what needs to be taken into consideration). – jcuenod Jan 09 '11 at 22:07
  • try this.. http://stackoverflow.com/questions/7608173/android-how-to-get-the-name-of-apk-file-programmatically – Prashant09 Aug 21 '12 at 09:02

5 Answers5

35

List<ApplicationInfo> PackageManager.getInstalledApplications() will give you a list of the installed applications, and ApplicationInfo.sourceDir is the path to the .apk file.

PackageManager pm = getPackageManager();

for (ApplicationInfo app : pm.getInstalledApplications(0)) {
  Log.d("PackageList", "package: " + app.packageName + ", sourceDir: " + app.sourceDir);
}

Outputs something like this:

package: com.tmobile.themechooser, sourceDir: /system/app/ThemeChooser.apk
package: com.tmobile.thememanager, sourceDir: /system/app/ThemeManager.apk
package: com.touchtype.swiftkey, sourceDir: /data/app/com.touchtype.swiftkey-1.apk
package: com.twitter.android, sourceDir: /data/app/com.twitter.android-2.apk
package: fm.last.android, sourceDir: /data/app/fm.last.android-1.apk
John Carter
  • 53,924
  • 26
  • 111
  • 144
  • 3
    What's the difference between `sourceDir` and [`publicSourceDir`](http://developer.android.com/reference/android/content/pm/ApplicationInfo.html#publicSourceDir)? Also, do you know why some .apk's have the "-1" or "-2" in their name? – Tony Chan May 04 '12 at 20:01
  • @Turbo I think the reason is: -1 when is installed for first time and -2 when is not. If you install one app for first time -1 appears if is second, third,... -2 appears. If you install the app for first time and you unistall it and then install again -1 is there – Ivan Apr 12 '13 at 10:09
  • I took the code that @therefromhere posted and put it into a [small app](https://github.com/zabawaba99/AppLister) and built and [apk](https://github.com/zabawaba99/AppLister/releases/tag/v1) for it in case anyone wants to reuse this on different devices. – zabawaba99 Aug 20 '15 at 19:19
18
$ adb shell pm list packages -f
Spudley
  • 166,037
  • 39
  • 233
  • 307
Joe Bowbeer
  • 3,574
  • 3
  • 36
  • 47
3

This gave me path in form /data/app/xxx.apk:

https://developer.android.com/reference/android/content/Context.html#getPackageResourcePath%28%29

NoAngel
  • 1,072
  • 2
  • 18
  • 27
2

You can also do adb bugreport and look at the output. You'll want to look for <package name="com.app.package" codePath="path/to/the/app.apk"...

triad
  • 20,407
  • 13
  • 45
  • 50
0

The apps that you download go to /data/app so using ls you can get a list of the downloaded apks.

jcuenod
  • 55,835
  • 14
  • 65
  • 102
  • Thank you j3frea. That's default location for Android Market App . I saw that if I am downloading app's form outside (in my case from my Server) the app went to sdcard/download . Can I assume that outside apk's will go always to sdcard/download ? Is that default location for every Android Market app or that path could be changed and is not fixed one ? Is there a way to push the download app just to one location always ? Also is there a method to find a file by name in whole device ? – asteroidg Jan 08 '11 at 22:29
  • 1
    File dir = new File("/data/app"); File[] fileList = dir.listFiles(); if (fileList != null) { for ( int i = 0;i – asteroidg Jan 08 '11 at 22:45
  • 3
    Do not make any assumptions on directories that are not explicitly outlined in the API. They are bound to change without further notice. Any assumptions like `sdcard/download` are guaranteed to break at some point. If anything, use directories that are returned by the API, don't use hardcoded paths. And keep in mind that Android Market is not the only way to download apps. – EboMike Jan 09 '11 at 21:11
  • Hi EboMike. I don't wanna make any assumption but I need a solution for my problem and I am very surprised that I wasn't able to find any API to help me . Are there any API to get the *.apk file name from the whole device ? Or even path where they can be stored, which are built together with OS ? Thank you. – asteroidg Jan 10 '11 at 16:28
  • I think that we need to know what you're trying to do in order to help to solve your problem. – jcuenod Jan 10 '11 at 21:24
  • The APK is a package that contains the app, so when you download it not using the market place, it's just an mordinary file downloaded to the default location - `/sdcard/download`. The user can move this file or change the directory. Once the app is installed (by opening the apk with the installer), it's no longer an APK - So in short, you can't guarantee you'll find the apk for the app (Try downloading one, installing it then deleting the apk) – Basic Jan 30 '12 at 20:48