在Android中获取原始.APK文件的名称 [英] Getting name of original .APK file in Android

查看:346
本文介绍了在Android中获取原始.APK文件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何获取安装了应用程序的原始.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 的简单应用程序(通过 android:label 标记在 AndroidManifest.xml 文件中。此外,假设我为上面的应用程序生成.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文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆