APK parser for Android

Related tags

Utility APKParser
Overview

APK Parser Maven Central Software License Twitter Follow

Features

  • Retrieve basic apk metas, such as title, icon, package name, version, etc.
  • Parse and convert binary xml file to text
  • Classes from dex file
  • Get certificate metas and verify apk signature

Get apk-parser

Download the latest AAR or grab via Gradle:

compile 'com.jaredrummler:apk-parser:1.0.2'

Usage

The easiest way is to use the ApkParser class, which contains convenient methods to get AndroidManifest.xml, apk meta infos, etc. #####1. Apk meta info ApkMeta contains name(label), packageName, version, sdk, used features, etc.

PackageManager pm = getPackageManager();
ApplicationInfo appInfo = pm.getApplicationInfo("com.facebook.katana", 0);
ApkParser apkParser = ApkParser.create(appInfo);
ApkMeta meta = apkParser.getApkMeta();
String packageName = meta.packageName;
long versionCode = meta.versionCode;
List<UseFeature> usesFeatures = meta.usesFeatures;
List<String> requestedPermissions = meta.usesPermissions;

#####2. Get binary xml and manifest xml file

ApplicationInfo appInfo = getPackageManager().getApplicationInfo("some.package.name", 0);
ApkParser apkParser = ApkParser.create(appInfo);
String readableAndroidManifest = apkParser.getManifestXml();
String xml = apkParser.transBinaryXml("res/layout/activity_main.xml");

#####3. Get dex classes

ApplicationInfo appInfo = getPackageManager().getApplicationInfo("com.instagram.android", 0);
ApkParser apkParser = ApkParser.create(appInfo);
List<DexInfo> dexFiles = apkParser.getDexInfos(); // if size > 1 then app is using multidex
for (DexInfo dexInfo : dexFiles) {
  DexClass[] dexClasses = dexInfo.classes;
  DexHeader dexHeader = dexInfo.header;
}

#####4. Get certificate and verify apk signature

ApplicationInfo appInfo = getPackageManager().getApplicationInfo("com.instagram.android", 0);
ApkParser apkParser = ApkParser.create(appInfo);
if (apkParser.verifyApk() == ApkParser.ApkSignStatus.SIGNED) {
  System.out.println(apkParser.getCertificateMeta().signAlgorithm);
}

#####5. Get intent-filters from apk manifest:

ApkParser parser = ApkParser.create(getPackageManager(), "com.android.settings");
AndroidManifest androidManifest = parser.getAndroidManifest();
for (AndroidComponent component : androidManifest.getComponents()) {
  if (!component.intentFilters.isEmpty()) {
    for (IntentFilter intentFilter : component.intentFilters) {
      // Got an intent filter for activity/service/provider/receiver.
    }
  }
}

#####6. Locales Apk may return different infos(title, icon, etc.) for different region and language, which is determined by Locales. If the locale is not set, the "en_US" locale(Locale.US) is used. You can set the locale like this:

ApkParser apkParser = ApkParser.create(filePath);
apkParser.setPreferredLocale(Locale.SIMPLIFIED_CHINESE);
ApkMeta apkMeta = apkParser.getApkMeta();

The PreferredLocale parameter work for getApkMeta, getManifestXml, and other binary xmls. Apk parser will find best match languages with locale you specified.

If locale is set to null, ApkParser will not translate resource tag, just give the resource id. For example, apk title will be '@string/app_name' instead of 'WeChat'.


APK Parser is based on CaoQianLi's apk-parser

Comments
  • Questions about the library

    Questions about the library

    1. Can it parse Intent-Filters?
    2. How is it in terms of memory? I remember the original library (for PC) was quite memory consuming, which caused OOM sometimes when I tried it on Android: https://github.com/CaoQianLi/apk-parser/issues/19
    3. Can you please put a sample too, and not just a library? Also, can you split the code , so that it would be possible to use via gradle? Maybe via https://jitpack.io/ ?
    4. What about the license? Does it require to put info about the repo in the "about" screen of apps, or something?
    opened by AndroidDeveloperLB 7
  • Request: allow to parse only needed things from the APK

    Request: allow to parse only needed things from the APK

    For example, if all I want to get from the APK is the intent-filters, if you remove the code of ApkParser.parseResourceTable() , it will save a lot of memory and time for parsing the APKs.

    For example, to demonstrate the time difference, I've ran this code on each of the apps that are installed on my device:

                    ApkParser apkParser = ApkParser.create(packageInfo.applicationInfo.publicSourceDir);
                    try {
                        AndroidManifest androidManifest = apkParser.getAndroidManifest();
                        for (AndroidComponent component : androidManifest.getComponents()) {
                             //nothing
                        }
    

    This took 115,992 seconds to run the code on 241 installed apps (both system and user apps), yet it took 22,864 seconds when removing the code I've written about.

    This is about 5 times faster.

    It might even be possible to avoid calling other pieces of code, if all I want to get is the intent filters.

    opened by AndroidDeveloperLB 3
  • Use case of APKParser vs. getPackageManager().getPackageArchiveInfo()

    Use case of APKParser vs. getPackageManager().getPackageArchiveInfo()

    Hi @jaredrummler

    I was wondering what the advantages of are of this library in comparison to getPackageManager().getPackageArchiveInfo(). Can you make a small comparison? Currently I am wondering why APKParser exist. What are the drawbacks of getPackageArchiveInfo?

    question 
    opened by dschuermann 3
  • Added attribute for intent filter as now you can set android:autoverify=true for intent filters

    Added attribute for intent filter as now you can set android:autoverify=true for intent filters

    Added attribute for intent filter as now you can set android:autoverify=true for intent filters from android marshmallow onwards. It is a new feature by google called app links. The documentation is available here https://developer.android.com/training/app-links/index.html

    opened by rongjc 0
  • Question: is it possible to inspect code of APK using this library?

    Question: is it possible to inspect code of APK using this library?

    For example like this website:

    http://www.javadecompilers.com/

    I tried to use the library to get to the dex files, hoping it's the classes, but I got Chinese/Japanese strings:

            thread{
                val appInfo = packageManager.getApplicationInfo("com.whatsapp", 0)
                val apkParser = ApkParser.create(appInfo)
                val dexFiles = apkParser.dexInfos
                for (dexInfo in dexFiles) {
                    val dexClasses: Array<DexClass> = dexInfo.classes
                    val dexHeader: DexHeader = dexInfo.header
                    Log.d("AppLog", "")
                }
            }
    

    image

    DeobfuscationSample.zip

    opened by AndroidDeveloperLB 0
  • Bug and possible fix for parsing APK files manifest data

    Bug and possible fix for parsing APK files manifest data

    Example is of Chrome and Chrome beta:

    chrome and chrome beta.zip

    The issue with those seems that it causes attributes to have namespace missing, as such:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest http://schemas.android.com/apk/res/android:versionCode="398706833" http://schemas.android.com/apk/res/android:versionName="80.0.3987.68" http://schemas.android.com/apk/res/android:compileSdkVersion="29" http://schemas.android.com/apk/res/android:compileSdkVersionCodename="10" package="com.chrome.beta" platformBuildVersionCode="29" platformBuildVersionName="10">
    ...
    

    Here's the full output of parsing Chrome beta using ApkParser.create(file).manifestXml :

    chromeBetaManifestContent.zip

    This is considered as invalid XML.

    However, I've found a way to parse their manifest anyway. And it seems to fix this issue too .

    I made a test, and it seems that I got it right for manifest files of both normal APK files and split APK files. This code is based on this solution on StackOverflow :

    ApkManifestFetcher.zip

    Please check it out.

    opened by AndroidDeveloperLB 1
  • Bug: unable to parse some split APK files

    Bug: unable to parse some split APK files

    Example attached:

    split_delight.zip

    The crash exception:

        java.lang.Error: com.jaredrummler.apkparser.exception.ParserException: Unexpected chunk Type:203
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1173)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
            at java.lang.Thread.run(Thread.java:919)
         Caused by: com.jaredrummler.apkparser.exception.ParserException: Unexpected chunk Type:203
            at com.jaredrummler.apkparser.parser.ResourceTableParser.readChunkHeader(ResourceTableParser.java:204)
            at com.jaredrummler.apkparser.parser.ResourceTableParser.readPackage(ResourceTableParser.java:96)
            at com.jaredrummler.apkparser.parser.ResourceTableParser.parse(ResourceTableParser.java:67)
            at com.jaredrummler.apkparser.ApkParser.parseResourceTable(ApkParser.java:417)
            at com.jaredrummler.apkparser.ApkParser.transBinaryXml(ApkParser.java:269)
            at com.jaredrummler.apkparser.ApkParser.parseManifestXml(ApkParser.java:207)
            at com.jaredrummler.apkparser.ApkParser.getManifestXml(ApkParser.java:126)
            at com.android.apkinstalltest.MainActivity$onCreate$1.run(MainActivity.kt:33)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
            at java.lang.Thread.run(Thread.java:919) 
    

    Looking at the manifest xml string that it tried to build, I can see that its result is:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:dist="http://schemas.android.com/apk/distribution" android:versionCode="11002120" android:versionName="1.1 (6007940)" android:isFeatureSplit="true" android:compileSdkVersion="29" android:compileSdkVersionCodename="10" package="com.google.pixel.livewallpaper" platformBuildVersionCode="11002120" platformBuildVersionName="1.1 (6007940)" split="delight" split="delight">
    	<uses-sdk android:minSdkVersion="28" android:targetSdkVersion="28" />
    	<dist:module dist:onDemand="true" dist:title="resourceId:0x7f0d006b">
    		<dist:fusing dist:include="false" />
    	</http://schemas.android.com/apk/distribution:module>
    	<application android:hasCode="false">
    		<meta-data android:name="com.android.vending.derived.apk.id" android:value="1" />
    	</application>
    </manifest>
    

    But according to some online tools, it should have been:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest android:versionCode="11002120" android:versionName="1.1 (6007940)" android:isFeatureSplit="true" android:compileSdkVersion="29" android:compileSdkVersionCodename="10" package="com.google.pixel.livewallpaper" platformBuildVersionCode="11002120" platformBuildVersionName="1.1 (6007940)" split="delight" split="delight"
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:dist="http://schemas.android.com/apk/distribution">
        <dist:module dist:onDemand="true" dist:title="@2131558507">
            <dist:fusing dist:include="false" />
        </dist:module>
        <application android:hasCode="false">
            <meta-data android:name="com.android.vending.derived.apk.id" android:value="1" />
        </application>
    </manifest>
    

    Somehow it got </http://schemas.android.com/apk/distribution:module> instead of </dist:module> .

    So this makes it a non valid XML content. Also I noticed other differences, such as "resourceId:0x7f0d006b"

    opened by AndroidDeveloperLB 1
  • Request: allow to parse APK file without using File or file-path

    Request: allow to parse APK file without using File or file-path

    Use InputStream , DocumentFile or Uri instead.

    The reason: In the near future, Google will block storage permission, which requires developers to handle files outside of the scope of the current app, to use SAF instead of File and file-path. More information here: https://issuetracker.google.com/issues/128591846

    And yes, it's very devastating for many apps and libraries that need to access the files in a normal way. Even the Android framework isn't ready for it, as there are functions such as getPackageArchiveInfo (here) that uses a File or file-path, and have no other alternative.

    If you want to test your solution without the storage permission and using just the InputStream, here (the app assumes there is a file named "a.apk" on the root path of the normal storage) : https://issuetracker.google.com/issues/132481545#comment5

    opened by AndroidDeveloperLB 0
  • Migrate to GradleMavenPush https://github.com/Vorlonsoft/GradleMavenPush

    Migrate to GradleMavenPush https://github.com/Vorlonsoft/GradleMavenPush

    Migrate to GradleMavenPush https://github.com/Vorlonsoft/GradleMavenPush

    Reasons:

    • Old plugin don't have updates for 4 years
    • Better javadocs generation
    • Better pom file generation
    • Smaller gradle.properties
    • You can easy migrate from Maven Central to JCenter by adding to your code IS_JCENTER = true only and register at https://bintray.com/bintray/jcenter
    opened by AlexanderLS 0
Owner
Jared Rummler
ᴇᴍʙʀᴀᴄᴇ ᴛʜᴇ ᴄʜᴀɴɢᴇ ᴏʀ ʙᴇ ʟᴇғᴛ ʙᴇʜɪɴᴅ ^ↀᴥↀ^
Jared Rummler
Apk parser for java

APK parser lib, for decoding binary XML files, getting APK meta info. Table of Contents Features Get APK-parser Usage 1. APK Info 2. Get Binary XML an

Hsiafan 1.1k Dec 18, 2022
Parser and Expression Evaluator for Logic Statements

LogicParser Parser and Expression Evaluator for Logic Statements: The steps behind the whole process of evaluating a given logic statement encompass:

Bărbuț-Dică Sami 1 Dec 8, 2021
🧹 Error correcting parser plugin

Tidyparse The main goal of this project is to speed up the process of learning a new language by suggesting ways to fix source code. Tidyparse expects

breandan 5 Dec 15, 2022
UE Capability parser used by smartphonecombo.it and cacombos.com

UE Capability Parser Warning Work In progress UE Capability parser used by smartphonecombo.it and cacombos.com $ java -jar uecapabilityparser.jar --he

Andrea Mennillo 4 Oct 16, 2022
Scanning APK file for URIs, endpoints & secrets.

APKLeaks Scanning APK file for URIs, endpoints & secrets. Installation from Pypi from Source from Docker Usage Options Output Pattern Pattern Argument

Dwi Siswanto 3.5k Jan 9, 2023
Android Shared preference wrapper than encrypts the values of Shared Preferences. It's not bullet proof security but rather a quick win for incrementally making your android app more secure.

Secure-preferences - Deprecated Please use EncryptedSharedPreferences from androidx.security in preferenced to secure-preference. (There are no active

Scott Alexander-Bown 1.5k Dec 24, 2022
Android library which makes it easy to handle the different obstacles while calling an API (Web Service) in Android App.

API Calling Flow API Calling Flow is a Android library which can help you to simplify handling different conditions while calling an API (Web Service)

Rohit Surwase 19 Nov 9, 2021
Gesture detector framework for multitouch handling on Android, based on Android's ScaleGestureDetector

Android Gesture Detectors Framework Introduction Since I was amazed Android has a ScaleGestureDetector since API level 8 but (still) no such thing as

null 1.1k Nov 30, 2022
Use Android as Rubber Ducky against another Android device

Use Android as Rubber Ducky against another Android device

null 1.4k Jan 9, 2023
Android Utilities Library build in kotlin Provide user 100 of pre defined method to create advanced native android app.

Android Utilities Library build in kotlin Provide user 100 of pre defined method to create advanced native android app.

Shahid Iqbal 4 Nov 29, 2022
A util for setting status bar style on Android App.

StatusBarUtil A util for setting status bar style on Android App. It can work above API 19(KitKat 4.4). 中文版点我 Sample Download StatusBarUtil-Demo Chang

Jaeger 8.8k Jan 6, 2023
A logger with a small, extensible API which provides utility on top of Android's normal Log class.

This is a logger with a small, extensible API which provides utility on top of Android's normal Log class. I copy this class into all the little apps

Jake Wharton 9.8k Dec 30, 2022
Java implementation of a Disk-based LRU cache which specifically targets Android compatibility.

Disk LRU Cache A cache that uses a bounded amount of space on a filesystem. Each cache entry has a string key and a fixed number of values. Each key m

Jake Wharton 5.7k Dec 31, 2022
a simple cache for android and java

ASimpleCache ASimpleCache 是一个为android制定的 轻量级的 开源缓存框架。轻量到只有一个java文件(由十几个类精简而来)。 1、它可以缓存什么东西? 普通的字符串、JsonObject、JsonArray、Bitmap、Drawable、序列化的java对象,和 b

Michael Yang 3.7k Dec 14, 2022
gRPC and protocol buffers for Android, Kotlin, and Java.

Wire “A man got to have a code!” - Omar Little See the project website for documentation and APIs. As our teams and programs grow, the variety and vol

Square 3.9k Dec 31, 2022
✔️ Secure, simple key-value storage for Android

Hawk 2.0 Secure, simple key-value storage for android Important Note This version has no backward compatibility with Hawk 1+ versions. If you still wa

Orhan Obut 3.9k Dec 20, 2022
A robust native library loader for Android.

ReLinker A robust native library loader for Android. More information can be found in our blog post Min SDK: 9 JavaDoc Overview The Android PackageMan

Keepsafe 2.9k Dec 27, 2022
A lightning fast, transactional, file-based FIFO for Android and Java.

Tape by Square, Inc. Tape is a collection of queue-related classes for Android and Java. QueueFile is a lightning-fast, transactional, file-based FIFO

Square 2.4k Dec 30, 2022
Joda-Time library with Android specialization

joda-time-android This library is a version of Joda-Time built with Android in mind. Why Joda-Time? Android has built-in date and time handling - why

Daniel Lew 2.6k Dec 9, 2022