aapt_dart 1.0.0
aapt_dart: ^1.0.0 copied to clipboard
A pure Dart library for extracting APK information using AAPT (Android Asset Packaging Tool). Extract package details, permissions, activities, and more from APK files.
example/aapt_dart_example.dart
import 'dart:io';
import 'package:aapt_dart/aapt_dart.dart';
void main() async {
// Example 1: Basic usage with default logger
await basicExample();
// Example 2: Using custom logger
await customLoggerExample();
// Example 3: Error handling
await errorHandlingExample();
// Example 4: Getting AAPT version
await versionExample();
}
/// Basic example showing how to extract APK information
Future<void> basicExample() async {
print('\n=== Basic Example ===\n');
// Create AAPT client
// Replace with your actual AAPT executable path
final aapt = AaptClient(
aaptExecutablePath: '/path/to/aapt',
);
try {
// Extract APK information
final apkInfo = await aapt.getApkInfo('/path/to/your/app.apk');
// Display basic information
print('Package Name: ${apkInfo.packageName}');
print('Version Code: ${apkInfo.versionCode}');
print('Version Name: ${apkInfo.versionName}');
print('Application Label: ${apkInfo.applicationLabel}');
print('Min SDK: ${apkInfo.sdkVersion}');
print('Target SDK: ${apkInfo.targetSdkVersion}');
print('Launchable Activity: ${apkInfo.launchableActivity}');
print('Is Debuggable: ${apkInfo.isDebuggable}');
// Display permissions
print('\nPermissions (${apkInfo.permissions.length}):');
for (final permission in apkInfo.permissions) {
print(' - $permission');
}
// Display features
print('\nFeatures (${apkInfo.usesFeatures.length}):');
for (final feature in apkInfo.usesFeatures) {
print(' - $feature');
}
// Display activities
print('\nActivities (${apkInfo.activities.length}):');
for (final activity in apkInfo.activities) {
print(' - $activity');
}
// Display services
print('\nServices (${apkInfo.services.length}):');
for (final service in apkInfo.services) {
print(' - $service');
}
// Display native libraries
if (apkInfo.nativeLibraries.isNotEmpty) {
print('\nNative Libraries:');
for (final lib in apkInfo.nativeLibraries) {
print(' - $lib');
}
}
// Display locales
if (apkInfo.locales.isNotEmpty) {
print('\nLocales:');
for (final locale in apkInfo.locales) {
print(' - $locale');
}
}
// Display densities
if (apkInfo.densities.isNotEmpty) {
print('\nDensities:');
for (final density in apkInfo.densities) {
print(' - $density');
}
}
} catch (e) {
print('Error: $e');
}
}
/// Example with custom logger implementation
class CustomLogger implements AaptLogger {
@override
void debug(String message) {
// Custom debug logging - e.g., write to file
print('[CUSTOM DEBUG] $message');
}
@override
void info(String message) {
print('[CUSTOM INFO] $message');
}
@override
void error(String message, {StackTrace? stackTrace}) {
stderr.writeln('[CUSTOM ERROR] $message');
if (stackTrace != null) {
stderr.writeln('Stack trace: $stackTrace');
}
}
}
Future<void> customLoggerExample() async {
print('\n=== Custom Logger Example ===\n');
final aapt = AaptClient(
aaptExecutablePath: '/path/to/aapt',
logger: CustomLogger(),
);
try {
final apkInfo = await aapt.getApkInfo('/path/to/your/app.apk');
print('Successfully extracted info for: ${apkInfo.packageName}');
} catch (e) {
print('Error: $e');
}
}
/// Example showing error handling
Future<void> errorHandlingExample() async {
print('\n=== Error Handling Example ===\n');
try {
// This will throw AaptNotFoundException
final aapt = AaptClient(
aaptExecutablePath: '/invalid/path/to/aapt',
);
await aapt.getApkInfo('/path/to/app.apk');
} on AaptNotFoundException catch (e) {
print('AAPT executable not found: ${e.aaptPath}');
} on ApkNotFoundException catch (e) {
print('APK file not found: ${e.apkPath}');
} on ApkParsingException catch (e) {
print('Failed to parse APK: ${e.apkPath}');
print('Reason: ${e.message}');
} on AaptException catch (e) {
print('AAPT error: ${e.message}');
} catch (e) {
print('Unexpected error: $e');
}
}
/// Example showing how to get AAPT version
Future<void> versionExample() async {
print('\n=== AAPT Version Example ===\n');
try {
final aapt = AaptClient(
aaptExecutablePath: '/path/to/aapt',
);
final version = await aapt.getVersion();
print('AAPT Version: $version');
} catch (e) {
print('Error getting version: $e');
}
}