如何在不使用 PackageManager 的情况下读取 Android Manifest?

发布于 2024-12-21 22:47:50 字数 1748 浏览 2 评论 0原文

我正在开发一个 Android 应用程序,它需要比 PackageManager 中提供的信息更多的信息(例如意图过滤器)。我创建了一个解析器来读取 AndroidManifest 文件,但我找不到任何已安装应用程序的 AndroidManifest.xml 的位置。我使用的代码是:

int flags = PackageManager.GET_ACTIVITIES
                       //| PackageManager.GET_CONFIGURATIONS
                       //| PackageManager.GET_DISABLED_COMPONENTS
                       //| PackageManager.GET_GIDS
                       //| PackageManager.GET_INSTRUMENTATION
                       //| PackageManager.GET_INTENT_FILTERS //Not needed (see BUG: http://code.google.com/p/android/issues/detail?id=3217)
                       | PackageManager.GET_META_DATA
                       | PackageManager.GET_PERMISSIONS
                       | PackageManager.GET_PROVIDERS
                       | PackageManager.GET_RECEIVERS
                       | PackageManager.GET_RESOLVED_FILTER
                       | PackageManager.GET_SERVICES
                       | PackageManager.GET_SHARED_LIBRARY_FILES
                       | PackageManager.GET_SIGNATURES
                       | PackageManager.GET_UNINSTALLED_PACKAGES
                       | PackageManager.GET_URI_PERMISSION_PATTERNS;
final PackageInfo pkg = pm.getPackageInfo(info.packageName, flags);
File manifest = new File(pkg.applicationInfo.publicSourceDir + "/AndroidManifest.xml");
if (manifest.exists()) {
    //Code never reaches this point
}

根据文档, ApplicationInfo.publicSourceDir 是:

此内容公开部分的位置的完整路径 包(即主要资源包和清单)。为了 非前向锁定应用程序,这与 {@link #sourceDir) 相同。

我做错了什么,或者我该如何做正确的事情?

I am working on an Android application that requires more information than what is available in the PackageManager (such as intent filters). I have created a parser for reading the AndroidManifest file, but I cannot find the location of the AndroidManifest.xml for any installed application. The code I am using is:

int flags = PackageManager.GET_ACTIVITIES
                       //| PackageManager.GET_CONFIGURATIONS
                       //| PackageManager.GET_DISABLED_COMPONENTS
                       //| PackageManager.GET_GIDS
                       //| PackageManager.GET_INSTRUMENTATION
                       //| PackageManager.GET_INTENT_FILTERS //Not needed (see BUG: http://code.google.com/p/android/issues/detail?id=3217)
                       | PackageManager.GET_META_DATA
                       | PackageManager.GET_PERMISSIONS
                       | PackageManager.GET_PROVIDERS
                       | PackageManager.GET_RECEIVERS
                       | PackageManager.GET_RESOLVED_FILTER
                       | PackageManager.GET_SERVICES
                       | PackageManager.GET_SHARED_LIBRARY_FILES
                       | PackageManager.GET_SIGNATURES
                       | PackageManager.GET_UNINSTALLED_PACKAGES
                       | PackageManager.GET_URI_PERMISSION_PATTERNS;
final PackageInfo pkg = pm.getPackageInfo(info.packageName, flags);
File manifest = new File(pkg.applicationInfo.publicSourceDir + "/AndroidManifest.xml");
if (manifest.exists()) {
    //Code never reaches this point
}

According to the documentation, ApplicationInfo.publicSourceDir is:

Full path to the location of the publicly available parts of this
package (i.e. the primary resource package and manifest). For
non-forward-locked apps this will be the same as {@link #sourceDir).

What am I doing wrong, or how can I do this right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

翻身的咸鱼 2024-12-28 22:47:50

这里是代码:

AssetManager am = yourContext.createPackageContext("packageName", 0).getAssets();

XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml");

但是你需要解析它。

Here the code:

AssetManager am = yourContext.createPackageContext("packageName", 0).getAssets();

XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml");

but then you need to parse it.

哆啦不做梦 2024-12-28 22:47:50

虽然花了很长时间,但我想通了。资产(例如清单)被压缩 - 请参阅 这篇文章。如果它没有被压缩,@gwa(或类似的东西)提供的代码就可以达到目的。

It took a long time, but I figured it out. Assets, such as the manifest, are compressed - see this post. If it had not been compressed, the code provided by @gwa (or something similar to it) would have done the trick.

还如梦归 2024-12-28 22:47:50

我认为您的应用程序没有足够的权限来读取它。在已取得 root 权限的设备上尝试一下。

I don't think your app have enough privilege to read it. Try it on an rooted device.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文