Skip to content

dafuai/php-apkparser

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 

Repository files navigation

php-apkparser

A small library for parsing Android application package files (APK s).

Requirements

It is possible that these may or may not be necessary, or perhaps require a small modification to work.

  • PHP 5.3.0+ · the use of the 'const' keyword outside of classes
  • iconv · Android packages deal with many languages, and use UTF-16 when applicable

Performance

Horrible.

Usage

Simple

To display some basic information about the package:

<?php

require_once 'lib/parser.php';

$apk = new \APKParser\APK('example.apk');

printf("-name = '%s'\n",              $apk->get_package());
printf("-version = '%s (%s)'\n",      $apk->get_androidversion_name(),  $apk->get_androidversion_code());
printf("-min_sdk_version = '%s'\n",   $apk->get_min_sdk_version());
var_export($apk->get_permissions());

Manifest

You may also get the AndroidManifest.xml DOMDocument object:

$apk      = new \APKParser\APK('example.apk');
$manifest = $apk->get_android_manifest_xml();

printf("-object = '%s'", get_class($manifest));

...or perhaps you wish to get AndroidManifest.xml as a string (why?):

// First method.
$manifest_i = $apk->get_android_manifest_axml()->get_buff();

// Second method.
$manifest_d = $apk->get_android_manifest_xml()->saveXML();

...OR YOU WANT AN ELEMENT ATTRIBUTE VALUE:

$app_name = $apk->get_element('application', 'android:name');
printf("-application_name = '%s%s'", $apk->get_package(), $app_name);

$activities = $apk->get_elements('activity', 'android:name');
var_export($activities);

Resources

Some applications define their manifest attribute values as references:

...
<application android:icon="@drawable/mushrooms" android:label="Mushrooms"/>
...

...which upon APK compilation are converted into resource IDs and now parsed by the parser into hex codes for convenience.

printf("%s", $apk->get_androidversion_name()); // prints '@<hex>', e.g.: '@7f0b000d'

These resource IDs have to be decoded:

$arscobj   = $apk->get_android_resources();
$vn_res_id = substr($apk->get_androidversion_name(), 1);
printf("%s", $arscobj->get_resource_value_by_reference($vn_res_id));

Resource values may be paths to files inside the package:

$app_icon = $apk->get_element('application', 'android:icon'); // @<hex>

$arscobj = $apk->get_android_resources();
var_export($arscobj->get_resource_value_by_reference(substr($app_icon, 1)));
/*
array (
  0 => 'res/drawable-mdpi/mushrooms.png',
  1 => 'res/drawable-hdpi/mushrooms.png',
  2 => 'res/drawable-xhdpi/mushrooms.png',
)
 */

...which you can get as a stream from the package (and then use as you wish):

$icon          = $arscobj->get_resource_value_by_reference(substr($app_icon, 1))[0];
$icon_stream   = $apk->get_file($icon);
$base64_string = base64_encode(stream_get_contents($icon_stream));

printf("data:image/png;base64,%s", $base64_string);

Developers decide whether attribute values will hold references or actual values, which means it is best to cover all cases for reliable results.

Acknowledgements

  • the Androguard project from which parts of the code was ported from

About

A library.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%