java 从 Android 中的 apk 文件读取活动名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5728903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:30:26  来源:igfitidea点击:

Reading Activity names from apk file in Android

javaandroidandroid-emulatorapk

提问by Smith

I want to extract all the activities names from an android apk file. Any idea how possibly it can be done?

我想从 android apk 文件中提取所有活动名称。知道怎么可能做到吗?

Thanx Kaps

感谢卡普斯

回答by Albin

If you just want to list them from the command line you can use

如果您只想从命令行列出它们,您可以使用

aapt dump xmltree <apk-file> AndroidManifest.xml

The format is a bit perplexing at first, but all the info is there.

格式起初有点令人困惑,但所有信息都在那里。

You need the Android SDK installed to do this, of course.

当然,您需要安装 Android SDK 才能执行此操作。

回答by David Snabel-Caunt

You can use the PackageManager method getPackageArchiveInfoto retrieve the information from an APK file. Assuming your APK lives in the root of your SD card, you can use this code:

您可以使用 PackageManager 方法getPackageArchiveInfo从 APK 文件中检索信息。假设您的 APK 位于 SD 卡的根目录中,您可以使用以下代码:

    String apkPath = Environment.getExternalStorageDirectory().getAbsolutePath() 
                   + "/MyApp.apk";
    PackageManager pm = getPackageManager();

    PackageInfo info = pm.getPackageArchiveInfo(apkPath, 
                           PackageManager.GET_ACTIVITIES);
    //Log.i("ActivityInfo", "Package name is " + info.packageName);

    for (android.content.pm.ActivityInfo a : info.activities) {
        Log.i("ActivityInfo", a.name);
    }

There is plenty of additional information you can find in the ActivityInfo and PackageInfo objects.

您可以在 ActivityInfo 和 PackageInfo 对象中找到大量附加信息。

回答by Smith

You could use a decompiler like Dedexerand then you can take a look at the manifest.

您可以使用像Dedexer这样的反编译,然后您可以查看清单。

回答by user1179614

其实不用得到apk的路径,直接在activity内调用

其实不用得到apk的路径,直接在活动内调用

(English translation) You don't have to use the full path to the APK, just call the following directly from inside an Activity.

(英文翻译)您不必使用 APK 的完整路径,只需直接从 Activity 内部调用以下内容即可。

PackageManager manager = getPackageManager();
PackageInfo packageInfo = manager.getPackageInfo("your package name", PackageManager.GET_UNINSTALLED_PACKAGES);