Android Deployment
Prerequisites
- Android NDK r21 or later
- Android SDK API level 21 or higher
- Gradle 6.0 or later
Adding ONNX Runtime to Android Project
Using AAR Package
Add to yourbuild.gradle:
For GPU Support (NNAPI)
Basic Android Usage
Using NNAPI Execution Provider
NNAPI provides hardware acceleration on Android:Loading Models from Assets
Android Build Configuration
For Different ABIs
ProGuard Rules
Add toproguard-rules.pro:
iOS Deployment
Prerequisites
- Xcode 12.0 or later
- iOS 11.0 or later
- CocoaPods or Swift Package Manager
Adding ONNX Runtime to iOS Project
Using CocoaPods
Add to yourPodfile:
Using Swift Package Manager
Add to yourPackage.swift:
Basic iOS Usage (Objective-C)
Swift Usage
Using CoreML Execution Provider
Loading Models from Bundle
Performance Optimization
Model Optimization
- Use ORT format: Convert models to
.ortformat for faster loading - Quantization: Use INT8 quantization for smaller model size
- Graph optimizations: Enable extended optimizations
Android Optimizations
iOS Optimizations
Testing on Emulator/Simulator
Android Emulator
Build for x86_64 ABI when testing on emulator:iOS Simulator
Note: Some execution providers (like CoreML) may have limited functionality on simulator.Best Practices
Resource Management
- Always close sessions and environments when done
- Use try-with-resources (Java) or defer (Swift)
- Monitor memory usage with profiling tools
Model Size
- Keep models under 50MB for better startup performance
- Use quantization to reduce model size
- Consider model splitting for very large models
Battery Consumption
- Use NNAPI/CoreML for hardware acceleration
- Batch inference requests when possible
- Profile power consumption during testing
Troubleshooting
Common Issues
Model loading fails on Android:- Check file permissions
- Verify model is in correct format (
.ortrecommended) - Ensure sufficient storage space
- Test on different Android versions
- Fallback to CPU if NNAPI fails
- Check operator compatibility
- Verify Xcode version compatibility
- Check minimum iOS version
- Clear derived data