Getting started¶
Instalação¶
Include dependency in build.gradle. Use the latest published version, available in Maven Central:
dependencies {
// Replace 1.x.x with the latest published version (see the Maven Central badge above)
implementation "org.hisp.dhis:android-core:1.x.x"
...
}
Além disso, precisa incluir este repositório em seu arquivo gradle raiz, se ainda não estiver lá:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Check compatibility here.
Inicialização D2¶
Para começar a usar o SDK, a primeira etapa é inicializar um objecto D2. A classe auxiliar D2Manager oferece métodos estáticos para configurar e inicializar a instânciaD2. Além disso, garante que D2 seja um singleton em todo o aplicativo.
A configuração mínima que deve ser passada ao D2Manager é a seguinte:
D2Configuration configuration = D2Configuration.builder()
.context(context)
.build();
Usando a configuração, pode instanciar D2.
Single<D2> d2Single = D2Manager.instantiateD2(configuration);
Assim que o Single for concluído, pode acessar D2 com o seguinte método:
D2 d2 = D2Manager.getD2();
Se não estiver usando RxJava, pode instanciar D2 de forma bloqueadora:
D2 d2 = D2Manager.blockingInstantiateD2(configuration);
O objecto D2Configuration possui vários campos para configurar o comportamento do SDK.
| Atributo | Requerido | Descrição | Padrão |
|---|---|---|---|
| contexto | verdade | Contexto de aplicação | - |
| nome do aplicativo | falso | Use para criar o cabeçalho "user-agent" | Do Android Manifest |
| appVersion | falso | Use para criar o cabeçalho "user-agent" | Do Android Manifest |
| readTimeoutInSeconds | falso | Tempo limite de leitura para consultas http | 30 segundos |
| connectTimeoutInSeconds | falso | Tempo limite de conexão para consultas http | 30 segundos |
| writeTimeoutInSeconds | falso | Tempo limite de gravação para consultas http | 30 segundos |
| interceptores | falso | Interceptadores para OkHttpClient | Nenhum |
| networkInterceptors | falso | NetworkInterceptors para OkHttpClient | Nenhum |
Google Play Services Support¶
The Google play services have been completely removed to make the SDK work on devices where the play services are not allowed; the SDK was using the ProviderInstaller present in the play service dependency to update the security provider to be protected against SSL exploits
If you're using a SDK version without google play services support, you have two options to update the security provider against SSL exploits, you can use an open-source solution called conscrypt to target devices without the google play services support or you can use play-services-safetynet dependency that's not open-source and required the google play services.
1. Play services Safetynet Dependency¶
If you're targeting devices that support the google play services, the best option is to use the ProviderInstaller included in the play-services-SafetyNet dependency by following steps.
dependencies {
implementation "com.google.android.gms:play-services-safetynet:<version>"
...
}
After adding the play-services-safetynet dependency just create a method that you can used to install the security provider
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());
}
}
For more information about play-services-safetynet, check the official documentation link
2. Open-source solution¶
If you're not targeting devices with the google play services, conscrypt is an alternative to the play-services-safetynet dependency that can be used to update security provider against SSL exploits
dependencies {
implementation 'org.conscrypt:conscrypt-android:<conscrypt-version>'
...
}
Setting up conscrypt is similar to the Google’s provider like in the following code :
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());
}
}
For more information about conscrypt, check the official github repository on this link