开始使用¶
安装¶
在build.gradle中包括依赖项。
依赖 {
实现 "org.hisp.dhis:android-core:1.14.0"
...
}
此外,如果尚无此存储库,则需要将该存储库包含在根gradle文件中:
所有项目{
储存库{
...
专家{url'https://jitpack.io'}
}
}
检查此处的兼容性。
D2初始化¶
In order to start using the SDK, the first step is to initialize a D2 object. The helper class D2Manager offers static methods to setup and initialize the D2 instance. Also, it ensures that D2 is a singleton across the application.
需要传递给D2Manager的最低配置如下:
D2Configuration configuration = D2Configuration.builder()
.context(context)
.build();
Using the configuration you can instantiate D2.
Single<D2> d2Single = D2Manager.instantiateD2(configuration);
完成Single之后,您可以使用以下方法访问D2:
D2 d2 = D2Manager.getD2();
如果不使用 RxJava,可以采用阻塞方式实例化 D2:
D2 d2 = D2Manager.blockingInstantiateD2(configuration);
对象 D2Configuration 有许多字段,用于配置 SDK 的行为。
| 属性 | 需要 | 描述 | 默认 |
|---|---|---|---|
| 语境 | 真正 | 应用环境 | -- |
| 应用程序名称 | 假 | 用于创建“用户代理”标题 | 从Android清单 |
| 应用程序版本 | 假 | 用于创建“用户代理”标题 | 从Android清单 |
| 读取超时秒数 | 假 | 读取HTTP查询超时 | 30秒 |
| connectTimeoutInSeconds | 假 | HTTP查询的连接超时 | 30秒 |
| 写入超时(秒 | 假 | 为HTTP查询写超时 | 30秒 |
| 拦截器 | 假 | OkHttpClient的拦截器 | 没有 |
| 网络拦截器 | 假 | OkHttpClient的NetworkInterceptors | 没有 |
Google Play 服务支持{ #google-play-services-support }¶
已完全移除 Google play 服务,以使 SDK 可在不允许使用 play 服务的设备上运行;SDK 正在使用 play 服务依赖关系中的 "ProviderInstaller "来更新安全提供程序,以防止 SSL 漏洞。
如果你使用的是不支持谷歌游戏服务的 SDK 版本,你有两个选择来更新安全提供商以防止 SSL 漏洞,你可以使用名为 conscrypt 的开源解决方案来针对不支持谷歌游戏服务的设备,或者你也可以使用 play-services-safetynet 依赖项,它不是开源的,需要谷歌游戏服务。
1.播放服务 Safetynet 依赖{ #1-play-services-safetynet-dependency }¶
如果你的目标设备支持 google play 服务,最好的选择是通过以下步骤使用 play-services-SafetyNet 依赖关系中包含的 ProviderInstaller 。
依赖 {
实现 "com.google.android.gms:play-services-safetynet:<version>"
...
}
添加 play-services-safetynet 依赖关系后,只需创建一个用于安装安全提供程序的方法即可
Java public static void initialize(Context context){ try { // .... ProviderInstaller.installIfNeeded(context.getApplicationContext()); // .... } catch (GooglePlayServicesRepairableException e) { Log.e(TAG, e.toString()); } catch (GooglePlayServicesNotAvailableException e) { Log.e(TAG, e.toString()); } catch (NoSuchAlgorithmException e) { Log.e(TAG, e.toString()); } }
有关 `play-services-safetynet` 的更多信息,请查看官方文档 [链接](https://developer.android.com/training/articles/security-gms-provider)
### 2.开源解决方案{ #2-open-source-solution }
如果你的目标设备不使用谷歌播放服务,[consrypt](https://github.com/google/conscrypt) 是 `play-services-safetynet` 依赖关系的替代方案,可用于更新安全提供商以防范 SSL 漏洞利用
```gradle
依赖 {
实现 'org.conscrypt:conscrypt-android:<conscrypt-version>'
...
}
设置 conscrypt 的方法与 Google 提供商类似,如下代码所示:
Java public static void initialize(){ try { Security.insertProviderAt(Conscrypt.newProvider(), 1); } catch(Exception e) { Log.e(TAG, e.toString()); } catch (NoSuchAlgorithmException e) { Log.e(TAG, e.toString()); } } ```
有关 conscrypt 的更多信息,请查看此 链接 上的官方 github 代码库。