Specs:
Android Studio (Native)
Kotlin
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.
Stacktrace:
https://pastebin.com/Wnszgb3G
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:$password@gmmdb.gioj2hh.mongodb.net/?retryWrites=true&w=majority")
val settings = MongoClientSettings.builder()
.applyConnectionString(connString)
.serverApi(
ServerApi.builder().version(ServerApiVersion.V1).build()
)
.build()
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
collection.insertOne(Document()
.append("_id", ObjectId())
.append("Date", Date.from(Instant.now()))
.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)
}
}
}
gradle:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
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'), 'proguard-rules.pro'
}
}
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 'com.google.android.material:material:1.7.0'
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!