aapt_dart

A pure Dart library for interacting with AAPT (Android Asset Packaging Tool) to extract information from APK files.

Pub Version License: MIT

Features

  • APK Information Extraction: Extract comprehensive metadata from APK files

    • Package name, version code, and version name
    • Application label and icon
    • SDK versions (minimum and target)
    • Permissions and features
    • Activities and services
    • Launchable activity detection
    • Native libraries
    • Supported locales and screen densities
    • Debuggable flag detection
  • Custom Logging: Implement your own logger or use the default console logger

  • Exception Handling: Comprehensive error handling with specific exception types

Getting Started

Prerequisites

You need to have AAPT (Android Asset Packaging Tool) installed on your system. AAPT is part of the Android SDK Build Tools.

Installation Options:

  1. Android Studio: AAPT is included in Android Studio's SDK Build Tools

    • Typical location: ~/Library/Android/sdk/build-tools/<version>/aapt (macOS)
    • Windows: %LOCALAPPDATA%\Android\sdk\build-tools\<version>\aapt.exe
    • Linux: ~/Android/Sdk/build-tools/<version>/aapt
  2. Command Line Tools: Download Android SDK Command Line Tools from developer.android.com

Installation

Add this to your package's pubspec.yaml file:

dependencies:
  aapt_dart: ^1.0.0

Then run:

dart pub get

Usage

Basic Example

import 'package:aapt_dart/aapt_dart.dart';

void main() async {
  // Create AAPT client with the path to your AAPT executable
  final aapt = AaptClient(
    aaptExecutablePath: '/path/to/aapt',
  );

  // Extract APK information
  final apkInfo = await aapt.getApkInfo('/path/to/your/app.apk');

  // Display information
  print('Package: ${apkInfo.packageName}');
  print('Version: ${apkInfo.versionName} (${apkInfo.versionCode})');
  print('Label: ${apkInfo.applicationLabel}');
  print('Min SDK: ${apkInfo.sdkVersion}');
  print('Target SDK: ${apkInfo.targetSdkVersion}');

  // Display permissions
  print('\nPermissions:');
  for (final permission in apkInfo.permissions) {
    print('  - $permission');
  }
}

Custom Logger

Implement your own logger for custom logging behavior:

class CustomLogger implements AaptLogger {
  @override
  void debug(String message) {
    // Your custom debug logging
  }

  @override
  void info(String message) {
    // Your custom info logging
  }

  @override
  void error(String message, {StackTrace? stackTrace}) {
    // Your custom error logging
  }
}

void main() async {
  final aapt = AaptClient(
    aaptExecutablePath: '/path/to/aapt',
    logger: CustomLogger(),
  );

  final apkInfo = await aapt.getApkInfo('/path/to/app.apk');
}

Error Handling

The library provides specific exception types for different error scenarios:

try {
  final aapt = AaptClient(
    aaptExecutablePath: '/path/to/aapt',
  );

  final apkInfo = await aapt.getApkInfo('/path/to/app.apk');
} on AaptNotFoundException catch (e) {
  print('AAPT not found at: ${e.aaptPath}');
} on ApkNotFoundException catch (e) {
  print('APK not found at: ${e.apkPath}');
} on ApkParsingException catch (e) {
  print('Failed to parse APK: ${e.message}');
} on AaptException catch (e) {
  print('AAPT error: ${e.message}');
}

Getting AAPT Version

final aapt = AaptClient(
  aaptExecutablePath: '/path/to/aapt',
);

final version = await aapt.getVersion();
print('AAPT Version: $version');

API Reference

AaptClient

The main client for interacting with AAPT.

Constructor

AaptClient({
  required String aaptExecutablePath,
  AaptLogger? logger,
})

Methods

  • Future<ApkInfo> getApkInfo(String apkPath) - Extract information from an APK file
  • Future<String> getVersion() - Get the AAPT version

ApkInfo

Model class containing comprehensive APK information.

Properties

  • String packageName - The package name
  • int versionCode - The version code
  • String versionName - The version name
  • String? applicationLabel - The app label
  • int? sdkVersion - Minimum SDK version
  • int? targetSdkVersion - Target SDK version
  • List<String> permissions - List of permissions
  • List<String> usesFeatures - List of features
  • List<String> activities - List of activities
  • List<String> services - List of services
  • String? launchableActivity - Main launchable activity
  • String? applicationIcon - App icon path
  • bool isDebuggable - Whether the app is debuggable
  • List<String> nativeLibraries - List of native libraries
  • List<String> locales - Supported locales
  • List<String> densities - Supported screen densities

Exceptions

  • AaptException - Base exception for all AAPT errors
  • AaptNotFoundException - AAPT executable not found
  • ApkNotFoundException - APK file not found
  • ApkParsingException - Failed to parse APK information

Examples

For more comprehensive examples, check the example directory.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you encounter any issues or have questions, please file an issue on GitHub.

Libraries

aapt_dart
A Dart library for interacting with AAPT (Android Asset Packaging Tool).