Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to get installer file name programmatically?

it is possible to get installer file name which is stored on /sdcard/ download/ ? I want to get from app, apk file name which installed it. There is a easy way to do it especially with non-market app?

like image 584
Wolfie Avatar asked Jan 03 '11 08:01

Wolfie


2 Answers

The better way is definitely to include the version string in the manifest. This is really the only reliable (and professional) method. Android doesn't store the name of the file containing the APK anywhere, so there isn't a way that you can get it.

Theoretically you could probably build something using a FileObserver, that watched file creation in certain directories and every time a file was created with the extension .apk you could open the file, extract the manifest, find the package name of the APK and then store this along with the filename in some persistent storage. Then, when you need get the version information, you can look in the persisten storage to get the file name matching the package name you need.

Of course, this would only work if your app was installed on the device before the other APK files were downloaded/installed. This wouldn't work to find out the filename of your own application.

like image 160
David Wasser Avatar answered Sep 24 '22 03:09

David Wasser


Hmm, I am not sure what you mean by sdcard or download folder, but you could get apk path in system /data directory:

command line:

$ adb shell pm list packages | grep mk.trelloapp
package:mk.trelloapp
$ adb shell pm path mk.trelloapp
package:/data/app/mk.trelloapp-1/base.apk

or from you android application code: (simply run process)

        Process exec = Runtime.getRuntime().exec("pm path mk.trelloapp");

        exec.waitFor();

        InputStream inputStream = exec.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

        StringBuilder builder = new StringBuilder();
        String line = null;
        while( ( line = br.readLine()) != null ) {
            builder.append(line);
        }

        String commandOutput = builder.toString();

Code above should simply give you String containing "package:/data/app/mk.trelloapp-1/base.apk"

When you acquire path in /data directory, you might try to search for same (similar) file in other directories (assuming it was not deleted), since android package is copied to /data directory while instalation.

like image 44
Max Avatar answered Sep 25 '22 03:09

Max