You need to enable JavaScript to run this app.
导航

导入和初始化 SDK

最近更新时间2023.09.27 13:52:35

首次发布时间2022.05.20 17:46:16

本文介绍了如何在 Android 项目中导入和初始化 SDK。

声明应用权限

您需要在 Android 项目的 AndroidManifest.xml 文件中声明以下权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

在项目中导入 SDK

  1. 在项目的 build.gradle(Project:<project_name>) 文件中添加火山引擎的 Maven 仓库地址。

    allprojects {
        repositories {
            maven {
                url 'https://artifact.bytedance.com/repository/Volcengine/'
            }
        }
    }
    
  2. 在项目的 build.gradle(Module:<project_name>.app) 文件中添加以下依赖。您可以参见 版本说明 了解最新的 SDK 版本号。

    dependencies {
        implementation 'com.bytedance.frameworks.baselib:httpdns:1.0.22'
    }
    

初始化 SDK

  1. 在项目中引用 HTTPDNS 类库。

    import com.bytedancehttpdns.httpdns.*;
    
  2. (可选)如果您希望使用固定 IP 地址接入 HTTPDNS 服务端,可以调用 setHttpDnsDomainList 方法,设置 SDK 通过固定 IP 地址接入 HTTPDNS 服务端。SDK 默认使用 httpdns.volcengineapi.com 域名接入 HTTPDNS 服务端。您可以调用 setHttpDnsBackupDomainList 方法把域名设置为备用接入方式。

    说明

    固定 IP 地址接入功能处于公测阶段,暂不对外公开。您可以 提交工单 联系我们获取固定 IP 地址

    ArrayList<String> FixedIpList = new ArrayList<String>();
    FixedIpList.add("xxx.xxx.xxx.1");
    FixedIpList.add("xxx.xxx.xxx.2");
    HttpDns.getService().setHttpDnsDomainList(FixedIpList);
    
  3. 定义一个类,例如 TestHttpDnsDepend,实现 IHttpDnsDepend 接口IHttpDnsDepend 接口定义了初始化 SDK 所需要的上下文信息。您需要将 HTTPDNS 鉴权密钥 中的 Service IDService Key 分别传入getHttpdnsAccountID 方法和 getHttpdnsSecretKey 方法的返回值中。

    说明

    对于 1.0.19 或更高版本的 Android SDK,您可以在控制台创建应用,并把应用的 应用ID 传入 getAppId 的返回值。这样,您就可以区分同一火山引擎账号下的多个应用,并且为应用单独配置自定义解析。参见 应用管理 了解详细信息。如果您的 SDK 版本低于 1.0.19,您需要把 SDK 升级到 1.0.19 或更高版本才能使用 应用管理 功能。

    警告

    示例代码了方便功能演示,直接在返回值传入 Service ID 和 Secret Key。在生产环境中,您需要避免直接传入明文。例如,您可以预先把明文进行编码或加密处理,在传值的时候再对已编码或加密的明文进行解码或解密。同时,您必须对 app 进行代码混淆处理。否则,您的 Service ID 和 Secret Key 可能会被第三方通过反编译的方式获得。

    public class TestHttpDnsDepend implements IHttpDnsDepend {
        private Context mContext;
    
        TestHttpDnsDepend(Context context) {
            mContext = context;
        }
    
        @Override
        public Context getContext() {
            return mContext;
        }
    
        // HTTPDNS 鉴权 Service ID
        @Override
        public String getHttpdnsAccountID() {
            return "xxxxxxx";
        }
    
        // HTTPDNS 鉴权 Secret Key
        @Override
        public String getHttpdnsSecretKey() {
            return "xxxxxxxx";
        }
    
        // 是否使用临时 key 鉴权。该方法已废弃。
        @Override
        @Deprecated
        public boolean isTemporaryAuthentication() {
            return false;
        }
    
        // 使用临时 key 鉴权时的临时 key 时间戳。该方法已废弃。
        @Override
        @Deprecated
        public long getHttpdnsTemporaryKeyTimeStamp() {
            return 0;
        }
    
        // httpdns 预解析域名,不要超过10个
        @Override
        public String[] getPreloadDomains() {
            return new String[]{"www.volcengine.com"};
        }
    
        // 应用 ID,可选参数,如果不需要设置可以返回 null
        @Override
        public String getAppId() {
            return null;
        }
    }
    
  4. 调用 HttpDns 类的构造函数 getService 创建 HttpDns 对象。通过 HttpDns 对象调用 setHttpDnsDepend 方法初始化 SDK。您需要在 setHttpDnsDepend 方法中传入 TestHttpDnsDepend 对象。在创建 TestHttpDnsDepend 对象时,需要传入 getApplicationContext 的返回值。

    // 初始化 SDK
    HttpDns.getService().setHttpDnsDepend(new TestHttpDnsDepend(getApplicationContext()));
    

接下来您可以

选择集成方案