---
title: Java
slug: v4-8/install-guides/java
icon: {"faIcon":"fa-brands fa-java"}
docTags: 
createdAt: 2023-07-28T20:16:57.748Z
---

You can integrate the Ditto SDK into Kotlin projects to develop for the Android platform.&#x20;

# Prerequisites

Following are the minimum requirements that must be met before attempting to install Ditto.

- Android version 6.0 (Marshmallow)
- `minSdk` version 23
- `compileSdk` version 34
- Java Development Kit (JDK) version 11

# Setting Up Your Environment

Include the Maven Central repository in your Gradle file, and then synchronize it with your project.&#x20;

:::::WorkflowBlock
::::WorkflowBlockItem
From your project-level `build.gradle`file located in the project's root directory, ensure the `mavenCentral()` repository is included in the  `repositories` section:

:::CodeblockTabs
Gradle

```none
allprojects {
    repositories {
        mavenCentral()
    }
}
```
:::
::::

:::WorkflowBlockItem
Synchronize your project with the Gradle file by clicking **File > Sync Project with Gradle Files**
:::
:::::

# Adding the Ditto SDK Dependency

Add the Ditto SDK to your app's `build.gradle` file in the `dependencies` block with the appropriate version:

:::CodeblockTabs
build.gradle

```javascript
implementation "live.ditto:ditto:4.8.1"
```
:::

# Configuring Permissions

The Android operating system limits access to some device functionality for end-user control and privacy. In order to use this functionality, configure your app to declare and request permissions from end users at runtime:

::::WorkflowBlock
:::WorkflowBlockItem
Review permissions used by Ditto. (Declaring Permissions)
:::

:::WorkflowBlockItem
Proactively check permission status to avoid unnecessary delay between granting access and Ditto being able to use the feature. (Requesting Permissions)
:::
::::

## Declaring Permissions in the Android Manifest&#x20;

Android requires certain permissions to be explicitly requested by the app to access features like Bluetooth Low Energy and Wi-Fi Aware. These permissions must be declared in the app's manifest file and requested from the end user at runtime.

The Ditto SDK's `AndroidManifest.xml` includes all of the necessary permissions for enabling its mesh network capabilities. These permissions will automatically be merged with your app's permissions, so you should be aware of them.

:::CodeblockTabs
AndroidManifest.xml

```xml
<uses-permission android:name="android.permission.BLUETOOTH"
    android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
    android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"
    tools:targetApi="s" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
    tools:targetApi="s" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
    android:usesPermissionFlags="neverForLocation"
    tools:targetApi="s" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
    android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
    android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES"
    android:usesPermissionFlags="neverForLocation"
    tools:targetApi="tiramisu" />
```
:::

Some of these permissions have an `android:maxSdkVersion` attribute which means they are not used on devices running newer versions of Android. This is a best practice to respect users' privacy when those permissions are not necessary.

However, some apps may still need to use one or more of the above permissions across more versions of Android. This can be accomplished by overriding the permission configuration in your app's `AndroidManifest.xml`&#x20;

To override any of these permission limitations in your app, do the following:

:::::WorkflowBlock
:::WorkflowBlockItem
Open the `AndroidManifest.xml` located in the `app/src/main` directory of your project.
:::

::::WorkflowBlockItem
Within the same `<manifest>` tag, just before the `<application>` tag, add the relevant permissions you want to configure (location example):

:::CodeblockTabs
AndroidManifest.xml

```xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
    tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
    tools:remove="android:maxSdkVersion" />
```
:::

Note the additional `tools:remove` attribute. This tells the manifest merger to selectively remove the `android:maxSdkVersion` behavior from the associated permissions, changing them to apply to all Android versions.
::::
:::::

:::hint{type="info"}
For more information, see the official [Permissions on Android](https://developer.android.com/guide/topics/permissions/overview) and [Merge manifest files](https://developer.android.com/build/manage-manifests) documentation.
:::

## Requesting Permissions at Runtime

In addition to being declared in the manifest, some permissions need to be requested from the user at runtime. To ensure your app has the required permissions for sync, use the `DittoSyncPermissions` helper class from the Ditto SDK. This helper class:

- Simplifies the process of requesting end-user permissions at runtime with a single method call (`ensurePermissions`) for all necessary permissions.
- Handles any errors encountered during the permissions request process.&#x20;

### Using the Helper

To use the `DittoSyncPermissions` helper class:

::::WorkflowBlock
:::WorkflowBlockItem
Import the `DittoSyncPermissions` helper class in the main activity of your app:

```java
import live.ditto.transports.DittoSyncPermissions;
```
:::

:::WorkflowBlockItem
Call the following in your `Activity` or Fragment's `onCreate` method:

```java
void requestPermissions() {
   DittoSyncPermissions permissions = new DittoSyncPermissions(this);
   String[] missing = permissions.missingPermissions(permissions.requiredPermissions());
   if (missing.length > 0) {
      this.requestPermissions(missing, 0);
   }
}
```
:::

:::WorkflowBlockItem
Refresh permissions after permissions are granted.

On Android, after permissions are granted, Ditto may not immediately recognize the change, causing a delay in syncing. To address this, whenever a relevant permission changes, call `refreshPermissions()` on your Ditto instance to quickly check and start syncing with the new permissions.  This can be done in the `onRequestPermissionsResult()` activity lifecycle method:

```java
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
   super.onRequestPermissionsResult(requestCode, permissions, grantResults);
   this.ditto.refreshPermissions();
}
```
:::
::::



For more information about requesting permissions in a user-friendly way, refer to the official [Requesting Runtime Permissions](https://developer.android.com/training/permissions/requesting) documentation

# Integrating and Initializing

Using the Application-level `Context`, integrate Ditto into your app's `onCreate` method. 

Most apps require that Ditto run as a *singleton*. A singleton is an implementation ensuring your Ditto instance accesses and shares a single object instance throughout your app.

To store the Ditto instance as a singleton object, create a custom `Application` subclass for your Android app. Include the necessary setup configuration below in the `onCreate` method. 

:::hint{type="info"}
By doing so, you ensure that there is only one instance of Ditto throughout the app's lifecycle, and it remains available for use by any activity or component that needs it. This approach helps in efficient resource management and avoids unnecessary object instantiation and destruction.
:::

:::::WorkflowBlock
::::WorkflowBlockItem
Add Ditto to your application  by placing it in your `Application.onCreate` method.

:::hint{type="warning"}
To ensure your app maintains a single Ditto instance throughout its lifetime and prevents it from going out of scope or getting garbage collected, place Ditto in the Application-level `Context` instead of the `Context` from any activity where you initially instantiated the Ditto instance.
:::

```java
DittoLogger.setMinimumLogLevel(DittoLogLevel.DEBUG);
DittoDependencies androidDependencies = new DefaultAndroidDittoDependencies(this.context);
DittoIdentity identity = new DittoIdentity.OnlinePlayground(
    androidDependencies,
    "YOUR_APP_ID",
    "YOUR_PLAYGROUND_TOKEN"
);
Ditto ditto = new Ditto(androidDependencies, identity);

try {
    ditto.startSync();
} catch(DittoError e) {
    //handle error
}
```
::::

::::WorkflowBlockItem
Replace `YOUR_APP_ID` and `YOUR_PLAYGROUND_TOKEN` with your access credentials available in the portal.

:::hint{type="info"}
For instructions on how to obtain your access credentials, see [Getting Playground Token Credentials](docId\:U214QhYYMCxbV9jnamRFH).
:::
::::
:::::

