Install Guides
Kotlin
you can integrate the ditto sdk into kotlinprojects to develop native apps for the android platform prerequisites following are the minimum requirements that must be met before attempting to install ditto android version 6 0 (marshmallow) minsdk version 23 0 compilesdk version 34 0 java development kit (jdk) version 11 0 setting up your environment include the maven central repository in your gradle file, and then synchronize it with your project from your project level build gradle file located in the app's root directory, ensure the mavencentral() repository is included in the repositories section gradle allprojects { repositories { mavencentral() } } 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 build gradle 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 review permissions used by ditto ( docid\ qu1fowj 6upugzgqrduiw ) proactively check permission status to avoid unnecessary delay between granting access and ditto being able to use the feature ( docid\ qu1fowj 6upugzgqrduiw ) declar ing permissions in the android manifest 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 androidmanifest 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 to override any of these permission limitations in your app, do the following open the androidmanifest xml located in the app/src/main directory of your project within the same \<manifest> tag, just before the \<application> tag, add the relevant permissions you want to configure (location example) androidmanifest 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 for more information, see the official https //developer android com/guide/topics/permissions/overview and 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 using the helper to use the dittosyncpermissions helper class import the dittosyncpermissions helper class in the main activity of your app import live ditto transports dittosyncpermissions call the following in your activity or fragment's oncreate method fun requestpermissions() { val missing = dittosyncpermissions(this) missingpermissions() if (missing isnotempty()) { this requestpermissions(missing, 0) } } refresh permissions after permissions are granted on android, after granting permissions, ditto may not immediately recognize the change, causing a delay in syncing to address this, whenever a relevant permission changes, call refreshpermissions() to quickly check and start syncing with the new permissions this can be done in the onrequestpermissionsresult() activity lifecycle method private val requestpermissionlauncher = registerforactivityresult(activityresultcontracts requestmultiplepermissions()) { this ditto refreshpermissions() } override fun oncreate(savedinstancestate bundle?) { } 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 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 add ditto to your application by placing it in your application oncreate method 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 try { dittologger minimumloglevel = dittologlevel debug val androiddependencies = defaultandroiddittodependencies(applicationcontext) val identity = dittoidentity onlineplayground( androiddependencies, appid = "your app id", token = "your playground token" ) ditto = ditto(androiddependencies, identity) ditto startsync() } catch (e dittoerror) { log e("ditto error", e message!!) } replace your app id and your playground token with your access credentials available from the portal (see docid\ u214qhyymcxbv9jnamrfh )