Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

adbkit-apkreader

openstf167.8kApache-2.03.2.0

Extracts information from APK files.

adb, adbkit, android, apk, manifest, AndroidManifest

readme

adbkit-apkreader

adbkit-apkreader provides a Node.js API for extracting information from Android APK files. For example, it allows you to read the AndroidManifest.xml of an existing APK file.

Requirements

  • Node.js 4.x or newer. Older versions are not supported.

Getting started

Install via NPM:

npm install --save adbkit-apkreader

Examples

Read the AndroidManifest.xml of an APK

const util = require('util')
const ApkReader = require('adbkit-apkreader')

ApkReader.open('HelloApp.apk')
  .then(reader => reader.readManifest())
  .then(manifest => console.log(util.inspect(manifest, { depth: null })))

API

ApkReader

ApkReader.MANIFEST

A convenience constant with the value 'AndroidManifest.xml'. Can use useful with other API methods in certain circumstances.

ApkReader.open(file)

Alternate syntax to manually creating an ApkReader instance. Currently, only files are supported, but support for streams might be added at some point.

Note that currently this method cannot reject as the file is opened lazily, but this may change in the future and therefore returns a Promise for fewer future compatibility issues. On a related node, calling the constructor directly is still possible, but discouraged.

  • file The path to the APK file.
  • Returns: A Promise that resolves with an ApkReader instance.

reader.readContent(path)

Reads the content of the given file inside the APK.

  • path The path to the file. For example, giving 'META-INF/MANIFEST.MF' as the path would read the content of that file.
  • Returns: A Promise that resolves with a Buffer containing the full contents of the file.

reader.readManifest()

Reads and parses the AndroidManifest.xml file inside the APK and returns a simplified object representation of it.

  • Returns: A Promise that resolves with a JavaScript Object representation of the manifest. See example output below. Rejects on error (e.g. if parsing was unsuccessful).
{ versionCode: 1,
  versionName: '1.0',
  package: 'com.example.hello.helloapp.app',
  usesPermissions: [],
  permissions: [],
  permissionTrees: [],
  permissionGroups: [],
  instrumentation: null,
  usesSdk: { minSdkVersion: 7, targetSdkVersion: 19 },
  usesConfiguration: null,
  usesFeatures: [],
  supportsScreens: null,
  compatibleScreens: [],
  supportsGlTextures: [],
  application:
   { theme: 'resourceId:0x7f0b0000',
     label: 'resourceId:0x7f0a000e',
     icon: 'resourceId:0x7f020057',
     debuggable: true,
     allowBackup: true,
     activities:
      [ { label: 'resourceId:0x7f0a000e',
          name: 'com.example.hello.helloapp.app.MainActivity',
          intentFilters:
           [ { actions: [ { name: 'android.intent.action.MAIN' } ],
               categories: [ { name: 'android.intent.category.LAUNCHER' } ],
               data: [] } ],
          metaData: [] } ],
     activityAliases: [],
     launcherActivities:
      [ { label: 'resourceId:0x7f0a000e',
          name: 'com.example.hello.helloapp.app.MainActivity',
          intentFilters:
           [ { actions: [ { name: 'android.intent.action.MAIN' } ],
               categories: [ { name: 'android.intent.category.LAUNCHER' } ],
               data: [] } ],
          metaData: [] } ],
     services: [],
     receivers: [],
     providers: [],
     usesLibraries: [] } }

reader.readXml(path)

Reads and parses the binary XML file at the given path inside the APK file. Attempts to be somewhat compatible with the DOM API.

  • path The path to the binary XML file inside the APK. For example, giving 'AndroidManifest.xml' as the path would parse the manifest (but you'll probably want to use reader.readManifest() instead).
  • Returns: A Promise that resolves with a JavaScript Object representation of the root node of the XML file. All nodes including the root node have the properties listed below. Rejects on error (e.g. if parsing was unsuccessful).
    • namespaceURI The namespace URI or null if none.
    • nodeType 1 for element nodes, 2 for attribute nodes, and 4 for CData sections.
    • nodeName The node name.
    • For element nodes, the following additional properties are present:
      • attributes An array of attribute nodes.
      • childNodes An array of child nodes.
    • For attribute nodes, the following additional properties are present:
      • name The attribute name.
      • value The attribute value, if possible to represent as a simple value.
      • typedValue May be available when the attribute represents a complex value. See android.util.TypedValue for more information. Has the following properties:
        • value The value, which might null, String, Boolean, Number or even an Object for the most complex types.
        • type A String representation of the type of the value.
        • rawType A raw integer presentation of the type of the value.
    • For CData nodes, the following additional properties are present:
      • data The CData.
      • typedValue May be available if the section represents a more complex type. See above for details.

reader.usingFileStream(path, action)

Opens a readable Stream to the given file inside the APK and runs the given action with it. The APK file is kept open while the action runs, allowing you to process the stream. Once the action finishes, the APK will be automatically closed.

  • path The path to the file. For example, giving 'META-INF/MANIFEST.MF' as the path would open that file.
  • action(stream) A function that processes the stream somehow. MUST return a Promise that resolves when you're done processing the stream. The value that the Promise resolves with will also be the value that usingFileStream() resolves with.
    • stream A readable Stream of the file content. You should either consume or end the stream yourself before resolving the action.
  • Returns: A Promise that resolves with whatever action resolves with.

More information

Contributing

See CONTRIBUTING.md.

License

See LICENSE.

Copyright © The OpenSTF Project. All Rights Reserved.

changelog

Changelog

3.2.0 (2020-02-06)

Fixes

  • Updated dependencies

3.1.2 (2019-01-18)

Fixes

  • Fixed a file reference leak when a ZIP-related error was encountered when reading an APK file. Thanks @harlentan!

3.1.1 (2018-11-11)

Fixes

  • Fixed manifest parsing on applications processed by 360 encryption services, which changes the application key to com.stub.StubApp. Thanks @JChord!

3.1.0 (2018-09-27)

Fixes

  • Fixed parsing of certain APKs that deduplicate items in the string pool.

Enhancements

  • Optional structured debug output can be enabled by passing debug: true to .readManifest() or .readXml().

3.0.2 (2018-07-11)

Fixes

  • Fixed parsing of Chrome 68 Beta APK and other similar APKs with a missing XML namespace. Thanks @zr0827!

3.0.1 (2018-04-20)

Fixes

  • Fixed parsing of long strings. Thanks @mingyuan-xia!

3.0.0 (2017-09-21)

Enhancements

  • Got rid of CoffeeScript.

Breaking changes

  • Dropped support for older Node.js versions. You need at least 4.x or newer now.

2.1.2 (2017-08-21)

Fixes

  • Fixed a RangeError: Index out of range error when parsing newer APKs that use UTF-8 encoding for their string pools. Thanks to @headshot289 and @mingyuan-xia for providing samples that helped isolate the issue.

2.1.1 (2017-03-07)

Enhancements

  • Fixed a readManifest() parsing issue with slightly malformed manifests.

2.1.0 (2017-03-01)

Enhancements

  • Added readContent(path) to read the raw content of any file. Thanks @LegNeato!
  • Exposed usingFileStream(path, action) that allows you to consume the contents of a file as a Stream. Useful for very large files.

Fixes

  • readXml(path) was unable to read any other file than AndroidManifest.xml due to an oversight. You can now read any file with it.

2.0.0 (2017-01-24)

Breaking changes

It was discovered that our previous Zip parser, adm-zip, could not handle all valid Zip formats. We've therefore switched to yauzl which provides an asynchronous interface. Therefore the following breaking API changes were required:

  • Replaced ApkReader.readFile() with a Promise-returning ApkReader.open() which describes it better, and we have no way of supporting the previous synchronous method with the new dependency.
  • Replaced ApkReader.readManifestSync() with a Promise-returning ApkReader.readManifest() as we have no way of supporting the synchronous method with the new dependency.
  • Replaced ApkReader.readXmlSync() with a Promise-returning ApkReader.readXml() as we have no way of supporting the synchronous method with the new dependency.