5

I need read the content of apk file (including AndroidManifest.xml) programmatically. I know there are some tools like apktool, dex2jar, aapt to extract apk content but, I need to do the same through an Android application. By the way my start point is a valid apk file path.

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
talha06
  • 6,206
  • 21
  • 92
  • 147

2 Answers2

4

See this answer on another question: https://stackoverflow.com/a/34850406/55940

Basically you can use this:

PackageInfo packageInfo = context.getPackageManager().getPackageArchiveInfo(file.getAbsolutePath(), 0);

This would only get the meta data though... You would have to use a ZipInputStream etc to read the content of the file.

Daverix
  • 399
  • 2
  • 16
3

First of all get the apk file from its path by 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);
Nitin Misra
  • 4,472
  • 3
  • 34
  • 52
  • after I get uri from file, how can I read apk content (i.e. manifest file, etc..)? – talha06 Feb 22 '14 at 14:08
  • you could read this thread http://stackoverflow.com/questions/2097813/how-to-parse-the-androidmanifest-xml-file-inside-an-apk-package/4761689#4761689 and this also http://stackoverflow.com/questions/13469147/get-android-apk-file-versionname-or-versioncode-without-installing-apk – Nitin Misra Feb 22 '14 at 14:13
  • So what's happened, any success? – Nitin Misra Feb 22 '14 at 14:42
  • no, it works for installed applications because it uses `PackageManager`. – talha06 Feb 22 '14 at 14:44