DEV Community

Cover image for apk_inspector: read signatures, permissions and components of Android apps from Flutter
Abdeldjalil Chougui
Abdeldjalil Chougui

Posted on Originally published at abdeldjalilchougui.biz

apk_inspector: read signatures, permissions and components of Android apps from Flutter

Every Android app carries a lot of information about itself: who signed it, which permissions it asks for, which of its components other apps can reach, whether it is debuggable, which store installed it. Android exposes most of this through PackageManager, but in pieces. The flags you need change with the API level, signing certificates have two different APIs, and a long app list can run into binder limits if you fetch it in one call.

apk_inspector is a Flutter plugin that collects those pieces into typed Dart objects, for installed apps and for APK files on disk. It adds a plain-language permission catalog and a risk score whose formula is fully visible.

What it does

Listing apps. installedApps(), installedAppsPage() and installedAppsStream() return lightweight AppSummary objects, sorted by package name and fetched in pages, so large devices do not block the UI or hit binder limits.

Icons. PNG bytes drawn from the app's Drawable at the size you ask for, including adaptive icons drawn with the device mask. Load them lazily with iconOf(packageName), or inline with includeIcons: true.

Details for one app. appDetails(packageName) returns an AppDetails object with:

  • version name, long version code, min, target and compile SDK;
  • first install and last update time;
  • the installer, the initiating package and the update owner (getInstallSourceInfo on API 30 and later, getInstallerPackageName below);
  • flags: system, updatedSystem, debuggable, testOnly, allowBackup, usesCleartextTraffic, largeHeap, hasCode;
  • sharedUserId, dataDir, sourceDir, split APK paths and total APK size;
  • requested permissions with their granted state and protection level;
  • activities, services, receivers and providers with their exported, permission and effective enabled state;
  • signing certificates: current signers, past signers from key rotation and multiple signers on API 28 and later, and the older GET_SIGNATURES below. Each certificate reports SHA-256, SHA-1, subject, issuer, validity, serial, signature algorithm and key algorithm.

APK files. inspectApkFile(path) gives the same details for an APK on disk. It also reports which signature schemes (v1, v2, v3, v3.1) the file carries, read from the ZIP central directory and the APK Signing Block.

Pure-Dart helpers. PermissionCatalog, RiskScore, CertificateFingerprint and ApkSignatureSchemes work without the platform side.

No permissions of its own. The plugin manifest declares nothing: no permissions and no <queries>.

How to use it

Add it from GitHub:

dependencies:
  apk_inspector:
    git: https://github.com/AbdeldjalilChougui/apk_inspector
Enter fullscreen mode Exit fullscreen mode

The plugin's minSdk is 21.

Package visibility comes first

From Android 11 (API 30), an app only sees the packages it declares. Without a declaration, installedApps() returns little more than system packages and your own app, and appDetails() fails with notFound for anything else. The plugin declares nothing on purpose, so your app chooses:

  • A narrow <queries> element in the manifest, listing the packages or intents you need. This is the preferred way.
  • QUERY_ALL_PACKAGES, which sees everything. Google Play restricts this permission to apps whose core purpose needs the full list, such as device search, antivirus or app management tools, and you must justify it in the Play Console.

inspectApkFile() needs no visibility declaration, but your app must be able to read the file path.

Dart

import 'package:apk_inspector/apk_inspector.dart';

final inspector = ApkInspector();

Future<void> listApps() async {
  // Streams page by page; icons are loaded separately.
  await for (final app in inspector.installedAppsStream(pageSize: 50)) {
    print('${app.label} ${app.packageName} ${app.versionName}');
  }
}

Future<void> details(String packageName) async {
  try {
    final d = await inspector.appDetails(packageName);
    print('target ${d.targetSdk}, installer ${d.installSource?.installer}');
    for (final cert in d.signing?.currentSigners ?? const <CertificateInfo>[]) {
      print('${cert.subject} ${cert.formattedSha256}');
    }
    for (final c in d.exportedComponents.where((c) => !c.isProtected)) {
      print('open ${c.kind.name}: ${c.name}');
    }
    final risk = d.riskScore;
    print('risk ${risk.score}/100');
    risk.factors.forEach(print);
  } on ApkInspectorException catch (e) {
    if (e.code == ApkInspectorErrorCode.notFound) {
      print('Not installed or not visible to this app.');
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Checking that an APK file is signed by a key you expect takes a few lines:

Future<void> apkFile(String path) async {
  final d = await inspector.inspectApkFile(path);
  print('${d.label} ${d.versionName} schemes: ${d.signingSchemes}');
  const pinned =
      '6973d591ad0002d66bcc0d4a30b080bbaec4627dbeee67e59d9c91378176723f';
  final ok = d.signing?.currentSigners
          .any((c) => CertificateFingerprint.matches(c.sha256, pinned)) ??
      false;
  print('signed by the expected key: $ok');
}
Enter fullscreen mode Exit fullscreen mode

How the risk score works

Each requested permission falls into a bucket based only on its protection level: normal is low, dangerous is high, signature-level permissions with the appop flag are specialAccess, the other signature-level permissions are systemOnly, and anything not reported is unknown. When the device reports no level, the plugin falls back to the AOSP level in PermissionCatalog.

RiskScore.of(details) then adds points, and the README gives the whole table:

  • each permission: high 6, specialAccess 10, systemOnly 2, unknown 1, capped at 50 in total;
  • debuggable 15, testOnly 5, usesCleartextTraffic 5, allowBackup 3;
  • targetSdk below 23: 10;
  • each exported component without a permission: 1, capped at 15;
  • sharedUserId set: 2.

The sum is clamped to 0..100, and every point appears in RiskReport.factors. The README is clear about what this number is: a heuristic for sorting and triage, not a malware verdict. Requested permissions count whether or not they are granted, so APK files and installed apps score the same way.

Things to know

  • Android only. On other platforms every call throws ApkInspectorException with the code unavailable.
  • Privacy. The list of installed apps is personal data: it can reveal health conditions, religion, finances or dating habits. The plugin reads package data only on the device and sends nothing anywhere. If your app uploads what it collects, tell users, get consent where the law requires it, and complete the Play Data safety form.
  • exported is what PackageManager resolved. The plugin does not parse the binary manifest, so it cannot tell an explicit android:exported from a default, and intent filters are not reported.
  • Scheme detection is not verification. It reports which signature schemes are present. It does not verify signatures. ZIP64 archives and v4 .idsig files are not handled.
  • APK files have less data. Grant states, install times, installer and dataDir are null, and rotation history depends on the device's parser.
  • Some fields need newer devices. minSdk needs Android 7.0 or later and compileSdk needs Android 12 or later on the device running the plugin. originatingPackage is only revealed to callers with INSTALL_PACKAGES.
  • usesCleartextTraffic is the manifest flag. A network security config can refine it per domain.
  • Where the catalog comes from. Protection levels for the 1016 permissions in PermissionCatalog are generated from the AOSP platform manifest at a pinned commit (Apache License 2.0). The descriptions and groups were written for this package.
  • What was tested. The example app has an emulator integration test. It compares the installed-app list with pm list packages, and compares the app's details and a copied debug APK with apksigner verify --print-certs and aapt2 dump badging run on the host. It was tested on the Android emulator (Pixel 10 system image, API 37). It has not yet been tested on a physical phone, OEM system apps or split APKs.

Why I built it

Reading app metadata on Android looks like one call and turns out to be many. Signing data alone needs GET_SIGNING_CERTIFICATES on newer versions and GET_SIGNATURES on older ones, with key rotation in between. The install source has a new API from Android 11. A full app list has to be paged. And a permission name like SYSTEM_ALERT_WINDOW says little to a user until someone explains it.

I wanted all of that in one typed API, with the same shape for an installed app and for an APK file. I also wanted the risk number to be honest: a published formula where every point can be traced to a factor, instead of a score nobody can check. And because the list of installed apps is sensitive, the plugin declares no permissions and leaves the visibility choice to the app that uses it.

Get the code

The source, the full formula and data sources, the example app and the tests are on GitHub: https://github.com/AbdeldjalilChougui/apk_inspector. It's MIT licensed; the generated protection-level table carries the Apache-2.0 notice of its AOSP source. Issues and pull requests are welcome.


Originally published at abdeldjalilchougui.biz.

Top comments (0)