Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read content of apk file programmatically?

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.

like image 485
talha06 Avatar asked Feb 22 '14 13:02

talha06


People also ask

How do I view the contents of an APK file?

Viewing the contents of an APK file: Since APK files come in compressed ZIP format, any ZIP decompression tool can open it. So, for viewing the contents of an APK file, all you have to do is rename its extension to . zip and open it. Or, you can open it directly through an open dialogue box of a zip application.

Can we extract source code from APK?

Expand com folder and then expand Package name folder you will get all the java files. 10.To open Manifest file and resources just open them in android studio. In this manner we can extract source code in just 10 simple steps.

Can you scan APK?

Scanning the APK To use, do the following: Open the site. Click on Choose File, and in the browser dialogue box, select your file. Click on Scan it! to get your results.


2 Answers

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);
like image 122
Nitin Misra Avatar answered Oct 29 '22 02:10

Nitin Misra


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.

like image 40
Daverix Avatar answered Oct 29 '22 03:10

Daverix