问题描述
我想弄清楚如何获取安装应用程序的原始 .apk 文件的名称.我知道您可以通过使用 ApplicationInfo
类(此处描述:Android:如何以编程方式获取 apk 文件的名称?)但这不是我想要的.
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.
举个例子,假设我有一个名为 TestApp
的简单应用程序(通过 AndroidManifest.xml
中的 android:label
标签)文件).此外,假设我为上述应用程序生成了一个 .apk 文件,将上述 .apk 文件重命名为 TestApp_JohnDoe.apk
,然后将该应用程序通过电子邮件发送给 John Doe 进行安装.当 John Doe 通过单击 .apk 文件安装应用程序时,我希望能够读取我发送给他的原始 .apk 的文件名 TestApp_JohnDoe.apk
.使用 ApplicationInfo
类,更具体地说,sourceDir
方法为我提供安装目录和应用程序名称,/data/app/TestApp.apk
,其中不是我要找的.我知道可能可以扫描所有可用目录以查找原始 .apk 文件,但我希望避免这种情况.
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.
总而言之,我想以编程方式检索安装程序 .apk 的 .apk 文件名/源目录,例如 /storage/sdcard0/Download/TestApp_JohnDoe.apk
,而不是 .实际安装的应用程序的 apk 文件名/源目录,/data/app/TestApp.apk
.
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
.
推荐答案
可以让应用程序知道在编译时设置的 apk 名称,如果这对您有用
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
这篇关于在 Android 中获取原始 .APK 文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!