Android Client Network Optimization: Framework Unification, HttpDns Integration, and Performance Enhancements
This article details how an Android team unified multiple network libraries into OkHttp, integrated HttpDns to bypass carrier DNS hijacking, migrated to HTTPS and HTTP/2, and applied a series of performance and security optimizations that markedly improved request speed, reliability, and user experience across diverse mobile network conditions.
Background: With the rapid development of mobile internet, client‑side network optimization has become essential, but challenges such as variable network environments, device performance, and complex protocols cause slow loading and poor user experience.
Framework unification: The team consolidated multiple legacy network libraries (HttpURLConnection, HttpClient, Volley, OkHttp) into a single OkHttp‑based solution, evaluating frameworks on performance and ease of use.
Comparison table: A concise table compares OkHttp, Cronet, and Mars on DNS management, connection handling, concurrency model, I/O model, protocol support, network‑quality monitoring, long‑connection support, cross‑platform capability, and secondary‑development difficulty.
Metrics: The optimization focuses on three core dimensions—speed, weak‑network resilience, and security—measured across stages of DNS resolution, connection creation, data transfer, and connection teardown, with quantitative indicators such as throughput, latency, connection count, and error rate.
HttpDns: Replaces carrier LocalDns with HTTP‑based DNS to avoid hijacking, improve latency, and provide caching, fallback, and security strategies. Sample OkHttp DNS interceptor code is shown:
mOkHttpClient = new OkHttpClient.Builder()
.dns(new Dns() {
@NonNull @Override
public List<InetAddress> lookup(String hostname) {
if (HttpDns.isMatch(hostname)) {
//满足HttpDns解析域名
return lookupHttpDns(hostname);
} else {
//否则查找LocalDns
return lookupLocal(hostname);
}
}
})
.build();Integration with Glide: Demonstrates how to bridge Glide and OkHttp after HttpDns integration, including Gradle dependencies and a GlideModule implementation.
apply plugin: 'kotlin-kapt'
dependencies {
kapt 'com.github.bumptech.glide:compiler:4.7.1'
implementation "com.github.bumptech.glide:okhttp3-integration:4.11.0"
} @GlideModule
public final class OkHttpLibraryGlideModule extends LibraryGlideModule {
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
}
}HTTPS & HTTP/2 migration: Describes moving all requests to HTTPS, handling certificates, debug configuration, and enabling HTTP/2 via ALPN, with relevant OkHttp source snippets.
private void connectTls(ConnectionSpecSelector connectionSpecSelector) throws IOException {
// Success! Save the handshake and the ALPN protocol.
String maybeProtocol = connectionSpec.supportsTlsExtensions()
? Platform.get().getSelectedProtocol(sslSocket)
: null;
protocol = maybeProtocol != null ? Protocol.get(maybeProtocol) : Protocol.http_1_1;
}
private void establishProtocol(ConnectionSpecSelector connectionSpecSelector, int pingIntervalMillis, Call call, EventListener eventListener) throws IOException {
connectTls(connectionSpecSelector);
if (protocol == Protocol.http_2) {
startHttp2(pingIntervalMillis);
}
}Additional optimizations: Prefetching data during cold start, multipart upload for large video files, CDN selection, tuning OkHttp timeout and max‑concurrent requests, and building a custom WebSocket channel for IM scenarios.
Conclusion: Unifying the network stack under OkHttp and applying DNS, TLS, and HTTP/2 optimizations significantly improved performance and security, reducing UnknownHostException rates and user complaints, while acknowledging remaining edge‑case issues that will be addressed in future work.
HaoDF Tech Team
HaoDF Online tech practice and sharing—join us to discuss and help create quality healthcare through technology.
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.