Installing Java SDK
You can integrate the Ditto SDK into Kotlin projects to develop for the Android platform.
To install the Java SDK:
Following are the minimum requirements that must be met before attempting to install Ditto.
- Android version 6.0 (Marshmallow) or later
- minSdk version 23.0 or later
- compileSdk version 31.0 or later
- Java Development Kit (JDK) version 11.0 or later
Confirm that mavenCentral() is in the project-level build.gradle:
In the individual module build.gradle file:
The Android operating system restricts access to some device functionality for the sake of user control and privacy. Therefore, to unlock the full capabilities of Ditto, it is essential to configure your app so that it requests all the permissions that it needs. Then at runtime your app must prompt the user to request certain permissions.
The Ditto Android SDK includes a set of permissions that are required to use all the device features necessary to enable sync. The permissions below will be automatically merged into your app's final manifest.
The tools:targetApi attribute causes the permission to only be requested on devices running the specified API level or higher. This avoids errors on older OS versions that do not recognize the permission.
The android:maxSdkVersion attribute causes that permission to only be requested on devices running the specified SDK level or lower. This avoids asking for more permissions than Ditto needs, however it will prevent your app from being able to request the permission on devices running a newer OS version. To opt out of this behavior and request the permission on all OS versions, see the following snippet:
Note that you may need to add the xmlns:tools="http://schemas.android.com/tools" namespace attribute to your app's root <manifest> tag as shown in the AndroidManifest.xml example above.
This will configure your app's build to ignore the android:maxSdkVersion attribute in our SDK allowing the permission to be requested on any SDK version. This techniquie can be used to tweak any permissions to your liking.
Bluetooth LE and WiFi Aware require the user to allow permissions at runtime. Your apps must ensure all required permissions for sync have been requested from the user. The Android Ditto SDK provides a DittoSyncPermissions helper which makes this easy. Call the following in your Activity or Fragment's onCreate method:
On Android there may be a noticeable delay between when the user grants location access and when Ditto notices the new permission. For this reason it is recommended to call refreshPermissions() whenever a relevant permission might have changed. This will force an immediate check. If a permission has become available your app can begin syncing as quickly as possible.
Add Ditto to your application.
We recommend placing this in your Application.onCreate method. Note that the Context you want to reference here is the Application level Context, rather than whatever Activity you might happen to be instantiating the Ditto instance from initially. This is because you need to ensure that your app is keeping a single Ditto instance alive for the entire lifetime of the application so it's not going out of scope or getting garbage collected.