Introduction
This topic provides a detailed API upgrade guide for the HTTPDNS Android software development kit (SDK). This guide helps you migrate smoothly from an older version to the latest version and includes the following information:
Version compatibility overview: A comprehensive summary of the compatibility between versions from
1.xto2.6.x. This helps you quickly identify incompatible version upgrades.Incompatible version upgrade guide: Detailed upgrade steps, code examples, and a mapping table of old and new APIs for each incompatible SDK upgrade.
Version compatibility overview
The HTTPDNS Android SDK has evolved through several major versions. The following table shows the compatibility between versions from 1.x.x to 2.6.x.
SDK version | Compatibility |
1.x.x - 2.3.x | Fully compatible |
2.3.5 - 2.4.0 | Initialization interface adjusted |
2.4.0 - 2.5.0 | Fully compatible |
2.5.0 - 2.6.6 | API level upgraded (Android 4.4) |
Guide to upgrading the SDK to version 2.4.0 or later
Version 2.4.0 is a major milestone for the HTTPDNS Android SDK. This version includes a complete architecture refactoring and interface optimization. The goals of this upgrade are:
Unified configuration pattern: This version changes the configuration pattern from scattered runtime configurations to a unified initialization configuration that uses the Builder pattern. This resolves configuration timing issues and improves SDK stability.
Standardized resolution interfaces: The resolution interface architecture is redesigned to provide three resolution patterns: synchronous blocking, asynchronous, and synchronous non-blocking. These patterns meet the needs of different scenarios.
Modular design: Features such as Log Management are separated into dedicated classes. This achieves better separation of responsibilities and improves code maintainability.
Performance optimization: Resolution performance and resource utilization efficiency are improved through interface optimizations and internal implementation enhancements.
This is a major, incompatible change. Although the changes are significant, you only need to modify the interfaces used by your application. Follow the upgrade steps and the API mapping table below to complete this upgrade.
Detailed upgrade steps
Update the dependency version
dependencies { implementation 'com.aliyun.ams:alicloud-android-httpdns:${httpdnsVersion}' }NoteYou can obtain
httpdnsVersionfrom the Android SDK Release Notes.Refactor the initialization code
Before the upgrade
// Old version: Scattered configuration HttpDnsService httpdns = HttpDns.getService(this, "your_account_id"); httpdns.setHTTPSRequestEnabled(true); httpdns.setExpiredIPEnabled(true); httpdns.setCachedIPEnabled(true); httpdns.setPreResolveAfterNetworkChanged(true); httpdns.setTimeoutInterval(3000); httpdns.setLogEnabled(true); httpdns.setLogger(new ILogger() { @Override public void log(String message) { Log.d("HTTPDNS", message); } }); httpdns.setDegradationFilter(new DegradationFilter() { @Override public boolean shouldDegradeHttpDNS(String host) { return "example.com".equals(host); } }); // If you use the IP ranking feature List<IPRankingBean> ipProbeList = new ArrayList<>(); ipProbeList.add(new IPRankingBean("example.com", 80)); httpdns.setIPProbeList(ipProbeList);After the upgrade
List<IPRankingBean> ipRankingList = new ArrayList<>(); ipRankingList.add(new IPRankingBean("example.com", 80)); // New version: Unified configuration with the Builder pattern InitConfig config = new InitConfig.Builder() .setContext(this) // Required .setEnableHttps(true) // Replaces setHTTPSRequestEnabled .setEnableExpiredIp(true) // Replaces setExpiredIPEnabled .setEnableCacheIp(true) // Replaces setCachedIPEnabled .setPreResolveAfterNetworkChanged(true) // Replaces setPreResolveAfterNetworkChanged .setTimeoutMillis(3000) // Replaces setTimeoutInterval .setIPRankingList(ipRankingList) // Replaces setIPProbeList .setNotUseHttpDnsFilter(new NotUseHttpDnsFilter() { @Override public boolean notUseHttpDns(String host) { return "example.com".equals(host); } }) .buildFor("your_account_id"); HttpDnsService httpdns = HttpDns.getService("your_account_id", config); // Log Management is now separate HttpDnsLog.enable(true); // Replaces setLogEnabled HttpDnsLog.setLogger(new ILogger() { // Replaces setLogger @Override public void log(String message) { Log.d("HTTPDNS", message); } }); } }
Update the resolution interfaces
Before the upgrade
// Synchronous non-blocking, IPv4, single IP String ip = httpdns.getIpByHostAsync(host); // Synchronous non-blocking, IPv4, all IPs String[ ] ips = httpdns.getIpsByHostAsync(host); // Synchronous non-blocking, IPv6, single IP String ipv6 = httpdns.getIPv6ByHostAsync(host); // Synchronous non-blocking, IPv4 and IPv6, all IPs HTTPDNSResult result = httpdns.getAllByHostAsync(host);After the upgrade
// Synchronous non-blocking, IPv4, all IPs HTTPDNSResult httpDnsResult = httpdns.getHttpDnsResultForHostSyncNonBlocking(host, RequestIpType.v4) String[ ] ips = httpDnsResult.getIps(); // Synchronous non-blocking, IPv6, all IPs HTTPDNSResult httpDnsResult = httpdns.getHttpDnsResultForHostSyncNonBlocking(host, RequestIpType.v6) String[ ] ipv6s = httpDnsResult.getIpv6s(); // Synchronous non-blocking, IPv4 and IPv6, all IPs HTTPDNSResult httpDnsResult = httpdns.getHttpDnsResultForHostSyncNonBlocking(host, RequestIpType.v6) String[ ] ips = httpDnsResult.getIps(); String[ ] ipv6s = httpDnsResult.getIpv6s();
Upgrade the interface for setting the HTTPDNS blacklist
Before the upgrade
InitConfig config = new InitConfig.Builder() .setDegradationFilter(new DegradationFilter() { @Override public boolean shouldDegradeHttpDNS(String host) { return "example.com".equals(host); } }) .build();After the upgrade
InitConfig config = new InitConfig.Builder() .setNotUseHttpDnsFilter(new NotUseHttpDnsFilter() { @Override public boolean notUseHttpDns(String host) { return "example.com".equals(host); } }) .build();
If your application does not use any incompatible APIs, you can skip these steps.
API upgrade mapping table
API categorization | Before upgrade | After upgrade |
Enable HTTPS request | ||
Enable expired IP | ||
Enable local cache | ||
Pre-resolve on network transition | InitConfig.Builder.setPreResolveAfterNetworkChanged(boolean) | |
Set resolution timeout | ||
Enable IP ranking feature | ||
Set global parameters for custom resolution | ||
Control log output | ||
Set log callback interface | ||
Enable IP ranking (old version) | ||
Synchronous non-blocking interface, IPv4 resolution, returns a single IP | getHttpDnsResultForHostSyncNonBlocking(String, RequestIpType.v4) | |
Synchronous non-blocking interface, IPv4 resolution, returns a list of IPs | getHttpDnsResultForHostSyncNonBlocking(String, RequestIpType.v4) | |
Synchronous non-blocking interface, IPv6 resolution, returns a single IP | getHttpDnsResultForHostSyncNonBlocking(String, RequestIpType.v6) | |
Synchronous non-blocking interface, IPv6 resolution, returns a list of IPs | getHttpDnsResultForHostSyncNonBlocking(String, RequestIpType.v6) | |
Synchronous non-blocking, IPv4 and IPv6 resolution, returns a list of IPs | getHttpDnsResultForHostSyncNonBlocking(String, RequestIpType.both) | |
Synchronous non-blocking interface, custom resolution, returns a list of IPv4 addresses | getHttpDnsResultForHostSyncNonBlocking(String, RequestIpType, Map<String, String>, String) | |
Synchronous non-blocking, custom resolution, returns a list of IPv4 addresses | getHttpDnsResultForHostAsync(String, Map<String, String> , String) | getHttpDnsResultForHostSyncNonBlocking(String, RequestIpType, Map<String, String>, String) |
Summary
The HTTPDNS Android SDK has evolved through several major versions. Version 2.4.0 introduced incompatible upgrades. This guide provides a complete migration path. Follow the migration path and best practices in this guide to safely and efficiently upgrade your version of the HTTPDNS Android SDK.