问题
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
.
回答1:
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
来源:https://stackoverflow.com/questions/29901859/getting-name-of-original-apk-file-in-android