3

I am trying to figure out how to get the name of the original .apk file from which an application was installed. I know that you can retrieve the package name and the source directory of the installed application by using the ApplicationInfo class (described here: Android: how to get the name of apk file programmatically?) but this is NOT what I want.

As an example, suppose I have a simple application that is named TestApp (via the android:label tag in the AndroidManifest.xml file). Furthermore, suppose I generate an .apk file for the application above, rename said .apk file to TestApp_JohnDoe.apk, and then email that application to John Doe for installation. When John Doe installs the application by clicking on the .apk file I would like to be able to read the filename of the original .apk, TestApp_JohnDoe.apk, that I sent to him. Using the ApplicationInfo class, more specifically the sourceDir method gives me the install directory and application name, /data/app/TestApp.apk, which is not what I am looking for. I know that it is probably possible to scan all available directories looking for the original .apk file, but I am hoping to avoid this.

In summary, I would like to programatically retrieve the .apk filename/source directory of the installer .apk, such as /storage/sdcard0/Download/TestApp_JohnDoe.apk, as opposed to the .apk filename/source directory of the actual installed application, /data/app/TestApp.apk.

Community
  • 1
  • 1
Willis
  • 5,308
  • 3
  • 32
  • 61
  • What makes you think that the file still exists and is in a place that is readable by your app, for every possible installation path? – CommonsWare Apr 27 '15 at 17:08
  • Well that is what I am wondering - if a provision exists for determining the name of the original .apk file. It could be possible that, even if the original .apk file was deleted, the installation process makes a note of the name of the original .apk file that is then available from within the application; that is what I am trying to determine. – Willis Apr 27 '15 at 17:11
  • First, I am not aware that this information is recorded. Second, in general, there is no requirement that the APK file used for installation have a recognizable filename. Just because *you* named the file `TestApp_JohnDoe.apk` does not mean that the *email client* will name it `TestApp_JohnDoe.apk`, when it makes a temporary copy on external storage for brief use during installation. Put your identifiers *in the app*, whether via compiled in (see Bojan's answer), packaged in `assets/`, etc. – CommonsWare Apr 27 '15 at 17:20

1 Answers1

0

It is possible to let the app know the apk name that was set at compile time, if that is of any use to you

applicationVariants.all { variant ->
    // Change the target apk file name and path
    String apk_path = "$rootProject.projectDir/bin"
    String apk_name = "john_doe.apk"
    project.delete(project.apk_path) //Delete any remains
    String apkFile = String.format("%s/%s", apk_path, apk_name)
    variant.outputs[0].outputFile = new File(apkFile)
    // This can be used in Java runtime by using getString(R.string.apk_name)
    resValue "string", "apk_name", apk_name
}

But what you are asking, I am pretty sure it is not possible

Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59