OkOne: An Unintrusive OkHttp Performance Optimization Framework for Android
This article introduces OkOne, a lightweight framework built on OkHttp that centralizes client management to reduce redundant instances, demonstrates step‑by‑step integration, shows performance gains through instance and connection reuse, and explores advanced features such as pre‑connect and detailed internal mechanisms.
OkOne is a framework built on top of OkHttp that improves network connection performance in Android apps without invasive changes.
Pain points
In large Android projects many modules or third‑party libraries create their own OkHttpClient instances, or fail to cache a singleton, causing wasted resources and preventing effective use of OkHttp’s connection pool and request queue.
Solution
OkOne collects scattered OkHttpClient.Builder objects, compares their configurations, and automatically reuses a single OkHttpClient for builders with identical settings.
Integration
Integration requires only three steps:
Add the Gradle plugin dependency in the project‑level build.gradle : dependencies { classpath 'com.cdh.okone:gradle:0.1.0' }
Apply the plugin in the app module’s build.gradle : apply plugin: 'plugin.cdh.okone'
Add the OkOne library dependency in the app module’s dependencies block: implementation 'com.cdh.okone:okone:0.1.2'
After these steps, the framework takes effect automatically at runtime.
Effect – Instance Reuse
Three OkHttpClient.Builder instances with different configurations are created in a demo. Each request builds a new OkHttpClient , but OkOne reuses the underlying client when the configurations match, so only three actual client instances exist instead of six.
Effect – Connection Reuse
By adding a custom EventListener to log connection events, the logs show that the first request for a builder performs DNS lookup and TLS handshake, while the second request reuses the existing connection, skipping those steps. Different builder configurations do not share connections.
Additional Features
Toggle global client reuse: OkOne.useGlobalClient = true;
Enable or disable OkOne logging: OkOne.setLogEnable(true);
Create an unmanaged OkHttpClient manually if needed: OkHttpClient client = new OkHttpClient(builder);
Advanced Feature – Pre‑connect
Developers can proactively establish a connection before a request. If successful, the connection is added to OkHttp’s pool, reducing latency for the subsequent request.
OkOne.preBuildConnection(okHttpClient, url, new PreConnectCallback() {
@Override
public void connectCompleted(String url) {
Log.d(TAG, "Pre‑connect succeeded: " + url);
}
@Override
public void connectFailed(Throwable t) {
Log.e(TAG, "Pre‑connect failed", t);
}
});Repeated pre‑connect attempts to the same host are ignored to avoid duplication.
Principle Analysis
OkHttp’s request flow passes through a chain of interceptors; the ConnectInterceptor is responsible for locating or creating a Connection . The core logic resides in ExchangeFinder.find() , which calls findHealthyConnection() and ultimately findConnection() . The method first tries to acquire a pooled connection from ConnectionPool . If none exists, it selects a Route via RouteSelector , creates a RealConnection , and calls connect() to establish the socket. After a successful connection, the new RealConnection is added to the pool.
Implementing pre‑connect involves reproducing these steps: obtain an Address from the target URL, create a RouteSelector to get a Route , check the pool for an existing connection, instantiate a RealConnection , invoke connect() with appropriate timeouts (no retry), and finally put the connection into the pool. If connect() throws, the pre‑connect is considered failed and the socket is discarded.
Full source code is available on GitHub:
https://github.com/chidehang/OkOne
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.