How to properly connect my Kotlin android App with Mongodb Atlas Cluster?

Android Studio (Native)
Java Driver 4.5.1

I tried to connect to my MongoDB Atlas Cluster from a native Kotlin Android App and none of the ways I tried worked. It works just fine in my desktop app in C# 6.0. Here is what I did:

1- I’m using java driver 4.5.1, because I cant build the app with mongodb driver versions higher than 4.5.1, it doesn’t work for me. If I try to implement it in build.gradle, for example: implementation 'org.mongodb:mongodb-driver-sync:4.7.2' when I try to build the app I receive this:

Invalid build configuration. Attempt to create a global synthetic for ‘Record desugaring’ without a global-synthetics consumer.


but versions less or equal to 4.5.1 builds fine.

2- After rolling back to version 4.5.1 a new problem occurs, if my connection string contains +srv in it, it throws me an IllegalArgumentException when trying to connect from the connection string, so I have to remove the +srv to “connect” to my Atlas Cluster.

3- If I remove the +srv it goes fine through all connection functions, but it doesn’t connect to anything. If I try to run any database CRUD operation, I’ll receive a TimeoutException after around 30 seconds because I’m actually not logged in to the MongoDB Atlas Cluster.

Here is my connection code:

class Database(client: MongoClient){

    companion object {

        fun connect(username : String?, password: String?) : Database?
            val databaseObject: Database
            lateinit var mongoClient: MongoClient

            if (username == null || password == null)
                return null;

            try {
                val connString = ConnectionString("mongodb://$username:$")
                val settings = MongoClientSettings.builder()

                mongoClient = MongoClients.create(settings)

                val database = mongoClient.getDatabase("GMMDB")
                val collection = database.getCollection("gmm_estoque")
               // after around 30 seconds this function is called it throws me a TimeoutException
               // probably because I'm not really connected to the cluster app
                    .append("_id", ObjectId())
                    .append("Date", Date.from(
                    .append("Code", 192)
                    .append("Name", "Asa de galinha")
                    .append("Marca", "Sadia")
                    .append("Disponibilidade", 1)
            catch (e: Exception) {
                Log.d("AppDebug", e.toString() + " - \n\n Message: " + e.message + " -\n\n StackTrace: " + e.stackTrace)

            return Database(mongoClient)


plugins {
    id ''
    id ''

android {
    namespace 'com.freto.barcodereader'
    compileSdk 33

    defaultConfig {
        applicationId "com.freto.barcodereader"
        minSdk 21
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    viewBinding {
        enabled = true

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), ''
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    kotlinOptions {
        jvmTarget = '1.8'

dependencies {
    implementation 'org.mongodb:mongodb-driver-sync:4.5.1'
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation ''
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

I’ve tried many things, but nothing seems to work, so I need help getting it to work, thank you very much, I enjoy MongoDB very much!

1 Like

Hello @Raique_Familia :wave:,

Welcome to MongoDB Community.

Thank you for your question. Could you give a brief on your use case on what are you trying to build? Is there a reason you are using Java Driver instead of Realm Database?

You can connect Android App to Cluster via Realm App Id. Please refer to Quick Start section of the documentation.

Another way is to connect the app remotely to Atlas Cluster, as explained in Realm Bytes, but it comes with limitations.

I hope the provided information is helpful. Please dont hesitate to ask if you need more assistance.

Cheers, :performing_arts: