mirror of
https://github.com/QM4RS/FridaBox.git
synced 2026-09-30 04:52:22 +02:00
7.1 KiB
7.1 KiB
JAR File System Improvements
Overview
This document outlines the comprehensive improvements made to the JAR file handling system in the BlackBox framework. The original implementation was basic and prone to errors, while the new system provides robust, efficient, and maintainable JAR file management.
Key Improvements
1. New JarManager Class
- Location:
Bcore/src/main/java/top/niunaijun/blackbox/core/system/JarManager.java - Purpose: Centralized JAR file management with advanced features
- Key Features:
- Singleton pattern for global access
- Async and sync initialization options
- File integrity verification
- Caching with hash-based validation
- Comprehensive error handling
- Progress tracking during file operations
2. JarConfig Configuration Class
- Location:
Bcore/src/main/java/top/niunaijun/blackbox/core/system/JarConfig.java - Purpose: Centralized configuration management
- Key Features:
- JAR file definitions with metadata
- Configurable validation settings
- Performance optimization settings
- Memory-aware buffer sizing
- Retry and timeout configurations
3. JarUtils Utility Class
- Location:
Bcore/src/main/java/top/niunaijun/blackbox/utils/JarUtils.java - Purpose: Enhanced JAR file operations
- Key Features:
- Atomic file operations with temporary files
- Progress tracking during copy operations
- SHA-256 hash calculation
- JAR file integrity verification
- Safe file deletion with retry logic
- Memory-optimized buffer sizing
4. Enhanced NativeCore Integration
- Location:
Bcore/src/main/java/top/niunaijun/blackbox/core/NativeCore.java - Improvements:
- Better error handling in
loadEmptyDex() - Automatic fallback to sync initialization
- Detailed logging for debugging
- Graceful handling of missing JAR files
- Better error handling in
Technical Improvements
Error Handling
- Before: Basic try-catch with
e.printStackTrace() - After: Comprehensive error handling with:
- Detailed error messages
- Graceful fallbacks
- Proper resource cleanup
- Retry mechanisms with exponential backoff
Performance Optimization
- Before: Fixed 4KB buffer size
- After: Memory-aware buffer sizing:
- 8KB for devices with < 256MB RAM
- 16KB for devices with 256MB-512MB RAM
- 32KB for devices with > 512MB RAM
File Integrity
- Before: No integrity checking
- After: Multiple validation layers:
- File size validation
- SHA-256 hash verification
- JAR file structure validation
- Atomic write operations
Caching System
- Before: No caching
- After: Intelligent caching with:
- File reference caching
- Hash-based validation
- Definition metadata storage
- Cache statistics and monitoring
Async Operations
- Before: Synchronous file operations
- After: Async initialization with:
- Non-blocking startup
- Progress tracking
- Fallback to sync if needed
- Thread safety with atomic operations
Configuration Options
JarConfig Settings
// Performance settings
public static final int DEFAULT_BUFFER_SIZE = 8192; // 8KB
public static final int MAX_BUFFER_SIZE = 32768; // 32KB
public static final int MIN_BUFFER_SIZE = 1024; // 1KB
// Retry settings
public static final int MAX_RETRY_ATTEMPTS = 3;
public static final long RETRY_DELAY_MS = 1000; // 1 second
public static final long MAX_RETRY_DELAY_MS = 5000; // 5 seconds
// Validation settings
public static final boolean ENABLE_FILE_HASHING = true;
public static final boolean ENABLE_SIZE_VALIDATION = true;
public static final boolean ENABLE_ASYNC_LOADING = true;
JAR File Definitions
// Empty JAR for fallback DEX loading
public static final JarDefinition EMPTY_JAR = new JarDefinition(
"empty.jar", "empty.apk", 100L,
"Empty JAR for fallback DEX loading", true
);
// JUnit JAR for testing framework support
public static final JarDefinition JUNIT_JAR = new JarDefinition(
"junit.jar", "junit.apk", 1000L,
"JUnit testing framework support", false
);
Usage Examples
Basic Usage
// Get JarManager instance
JarManager jarManager = JarManager.getInstance();
// Initialize asynchronously
jarManager.initializeAsync();
// Get JAR files
File emptyJar = jarManager.getEmptyJar();
File junitJar = jarManager.getJunitJar();
// Check if ready
if (jarManager.isReady()) {
// Use JAR files
}
Advanced Usage
// Get detailed information
String stats = jarManager.getCacheStats();
String jarInfo = jarManager.getJarInfo("empty.jar");
// Clear cache if needed
jarManager.clearCache();
// Force sync initialization
jarManager.initializeSync();
Testing
// Run comprehensive tests
JarManagerTest.testJarManager();
JarManagerTest.testConfiguration();
Migration Guide
From Old System
- Remove old code: The old
initJarEnv()method inBlackBoxSystemhas been replaced - Update imports: Use
JarManagerinstead of direct file operations - Update NativeCore:
loadEmptyDex()now usesJarManager.getEmptyJar()
Backward Compatibility
- All existing functionality is preserved
- JAR files are still copied to the same locations
- File paths remain unchanged
- API compatibility maintained
Benefits
Reliability
- Robust error handling prevents crashes
- File integrity verification ensures data consistency
- Atomic operations prevent corruption
- Retry mechanisms handle transient failures
Performance
- Memory-optimized buffer sizes
- Async initialization reduces startup time
- Intelligent caching reduces I/O operations
- Progress tracking for large files
Maintainability
- Centralized configuration management
- Comprehensive logging for debugging
- Modular design with clear separation of concerns
- Extensive documentation and examples
Scalability
- Configurable settings for different device capabilities
- Memory-aware optimizations
- Efficient resource management
- Extensible architecture for future JAR types
Testing
Automated Tests
JarManagerTestprovides comprehensive testing- Configuration validation
- File operation verification
- Error handling validation
Manual Testing
- Verify JAR files are copied correctly
- Check file integrity and hashes
- Monitor performance and memory usage
- Test error scenarios and recovery
Future Enhancements
Planned Features
- Support for additional JAR file types
- Dynamic JAR loading from network
- Compression and optimization
- Advanced caching strategies
- Performance monitoring and metrics
Extensibility
- Plugin architecture for custom JAR handlers
- Configuration-driven JAR definitions
- Custom validation rules
- Integration with external systems
Conclusion
The improved JAR file system provides a solid foundation for reliable, efficient, and maintainable JAR file management in the BlackBox framework. The modular design, comprehensive error handling, and performance optimizations ensure the system can handle the demands of modern Android applications while maintaining backward compatibility.