OkHttp interceptors run on every network request and response. They add extra processing time, which can affect frame rate if used on the main thread. Proper use keeps UI smooth at 60fps by offloading work to background threads. Interceptors can increase memory use slightly by holding request/response data temporarily. Battery use may rise if interceptors perform heavy tasks like logging or encryption.
OkHttp interceptors in Android Kotlin - Build, Publish & Deploy
- Use interceptors only for necessary tasks like adding headers or caching.
- Avoid heavy computation or blocking calls inside interceptors.
- Perform logging asynchronously or limit log detail in production builds.
- Use network interceptors for modifying requests/responses without UI thread impact.
- Reuse OkHttpClient instances with interceptors to avoid overhead.
OkHttp library adds a moderate size (~1MB) to the app bundle. Adding interceptors does not significantly increase size unless you include large dependencies (e.g., complex logging libraries). Startup time impact is minimal if OkHttpClient is initialized lazily or during app idle time. Avoid creating multiple OkHttpClient instances with interceptors to reduce startup overhead.
OkHttp is Android-specific; iOS uses NSURLSession with different interception patterns. Android interceptors allow easy request/response modification and logging. iOS requires custom URLProtocol subclasses or delegate methods for similar interception. App performance tuning differs: Android apps must manage OkHttp interceptor impact, while iOS apps optimize NSURLSession delegate usage.
- Google Play: Ensure interceptors do not collect or transmit user data without consent to comply with privacy policies.
- Data Security: Use interceptors to enforce HTTPS and certificate pinning to meet security guidelines.
- Battery and Performance: Avoid excessive background network activity in interceptors to prevent app suspension or rejection.
- Privacy: Disclose any data logging or analytics done via interceptors in your privacy policy.
Likely causes include heavy or blocking operations inside OkHttp interceptors running on the main thread. For example, synchronous logging, encryption, or network calls inside interceptors can delay responses and freeze UI. Also, creating new OkHttpClient instances with interceptors for every request increases startup time. To fix, move heavy work off the main thread, reuse OkHttpClient, and simplify interceptors.