Kotlin
Model Management API
The Model Management API provides fine-grained control over OCR model lifecycle, allowing you to download, load, unload, and manage AI models independently from the scanning process.
Overview
Key Features
- Separate Download & Load: Download models during onboarding, load them when needed
- Pre-download for Offline: Enable offline-first user experiences
- Query Models: Check downloaded/loaded models with detailed metadata
- Update Management: Check for and download model updates
- Memory Management: Explicit load/unload control for optimal memory usage
- Dynamic OCR: Switch between different models at runtime
Benefits
- Better User Experience: Pre-download models during onboarding instead of first use
- Offline Support: Download models when connected, use them offline
- Memory Efficiency: Load only needed models, unload when done
- Update Control: Check and apply model updates programmatically
Quick Start
Singleton Pattern (Recommended)
Initialize once in your MainActivity:
Then use anywhere in your app:
Core Concepts
OCR Modules
Available OCR modules with their supported sizes:
Example:
Initialization
Initialize ModelManager
Call once during app startup:
Check Initialization
Downloading Models
Basic Download
Download with Progress
Download Multiple Models
Cancel Download
Loading & Unloading Models
Load Model
Load a downloaded model into memory:
Check if Model is Loaded
Unload Model
Free memory by unloading a model (file remains on disk):
Get Loaded Model Count
Querying Models
Find Downloaded Models
Get all downloaded models:
Find Specific Model
Get Model Version
Returns the version string of a specific downloaded model.
Useful for debugging, logging, and feature validation.
Example:
Sample Output (if downloaded):
Sample Output (if not downloaded):
Common Use Cases:
Returns: String? (version string like "2025-05-05", or null if not downloaded)
Throws: IllegalArgumentException if modelSize is null
Find Loaded Models
Get all models currently in memory:
Checking for Updates
Check if Update Available
Download Update
Deleting Models
Delete Model
Remove a model from disk (unloads first if loaded):
Using Models for OCR
Dynamic OCR with Explicit Module
Use loaded models for on-device OCR:
Switch Between Models
Lifecycle Callbacks
Implement Listener
Monitor model lifecycle events:
Register Listener
Error Handling
Handle All Exceptions
Exception Types
Memory Management
Unload Models on Low Memory
Load Models Just-In-Time
Complete Examples
Onboarding Flow
Download all required models during app onboarding:
Update Check Flow
Check and download updates:
Model List UI
Display all downloaded and loaded models:
Best Practices
1. Initialize Early
Initialize ModelManager in your Application class:
2. Pre-download During Onboarding
Download models during setup, not during first use:
3. Load Models Just-In-Time
Load models when needed, unload when done:
4. Handle Errors Gracefully
Always handle ModelException:
5. Check Updates Periodically
Check for updates on app start or in settings:
6. Use Lifecycle Callbacks
Monitor model operations for better UX:
API Reference
For complete API documentation including all methods, parameters, and return types, see the Model Management API Reference.
Quick Reference
Initialization:
ModelManager.initialize()- Initialize singletonModelManager.getInstance()- Get singleton instanceModelManager.isInitialized()- Check initialization status
Download:
downloadModel()- Download modelcancelDownload()- Cancel downloadactiveDownloadCount()- Get active download count
Load/Unload:
loadModel()- Load model into memoryunloadModel()- Unload model from memoryisModelLoaded()- Check if loadedloadedModelCount()- Get loaded count
Query:
findDownloadedModels()- Get all downloaded modelsfindDownloadedModel()- Get specific modelfindLoadedModels()- Get all loaded modelscheckModelUpdates()- Check for updates
Delete:
deleteModel()- Delete model from disk
OCR:
makePrediction()- Perform OCR with loaded model
Thread Safety
All suspend functions are main-safe—they handle threading internally:
- ✅ Safe to call from main thread
- ✅ Safe to call from background thread
- ✅ Handle IO operations internally
- ✅ Return results on calling thread
Non-suspending functions are thread-safe and non-blocking:
isModelLoaded()- O(1), thread-safeunloadModel()- Thread-safecancelDownload()- Thread-safeactiveDownloadCount()- Thread-safeloadedModelCount()- Thread-safe
Troubleshooting
Model Not Loading
Problem: ModelNotFoundException when calling loadModel()
Solution: Download the model first:
Download Fails
Problem: NetworkException during download
Solutions:
- Check internet connection
- Verify API key is valid
- Check device has enough storage
- Try again with retry logic
NNAPI Load Failure
Problem: LoadException with NNAPI
Solution: Fallback to CPU:
Out of Memory
Problem: App crashes with OOM
Solution: Unload unused models:
Migration Guide
From Legacy configure() to ModelManager
Old approach:
New approach:
Benefits:
- Download during onboarding, not first use
- Query model status before operations
- Manage multiple models independently
- Better error handling
- Update models without re-initialization