Java – How to read the contents of an apk file programmatically?

How to read the contents of an apk file programmatically?… here is a solution to the problem.

How to read the contents of an apk file programmatically?

I need to programmatically read the contents of the apk file, including the AndroidManifest .xml. I know there are tools such as apktool, dex2jar, aapt that can extract apk content, however, I need to do this through the android app. By the way, my starting point is a valid apk file path.

Solution

First get the apk file from its path through this code

final PackageManager pm = getPackageManager();
                PackageInfo packageInfo = null;
                try {
                    packageInfo = pm.getPackageInfo("PACKAGE NAME HERE", PackageManager.GET_META_DATA);
                } catch (NameNotFoundException e) {
                     TODO Auto-generated catch block
                    e.printStackTrace();
                }

File file = new File(packageInfo.applicationInfo.sourceDir);
                Uri uri = Uri.fromFile(file);

Related Problems and Solutions