Uploaded image for project: 'Realm Kotlin'
  1. Realm Kotlin
  2. RKOTLIN-1107

Got lateinit property filesDir has not been initialized - RealmInitializer$Companion.getFilesDir(RealmInitializer.kt:34). Realm 2.0.0.

      How frequently does the bug occur?

      Always

      Description

      I am using Realm-kotlin Android version 2.0.0. I add Realm follow the https://www.mongodb.com/docs/atlas/device-sdks/sdk/kotlin/install/#std-label-kotlin-install-android. I did notice that this is for version 1.16.0 but cannot find instruction for version 2.0.0 ( I found one in the example but it used the kotlin multiplatform) and got error when create a Realm by this:

          lateinit var realm: Realm
      
          suspend fun createRealm(realmName: String) {
              val downloadResult = langDownloader.download(
                  "link_to_realm",
                  realmName
              )
              
              delay(5000)
      
              when(downloadResult) {
                  is DownloadLangResult.Success -> {
                      val configuration =
                          RealmConfiguration.Builder(setOf(StringResourceRealm::class))
                          .directory(downloadResult.directory)
                          .name(downloadResult.name)
                          .build()
      
                      realm = Realm.open(configuration)
                  }
      
                  is DownloadLangResult.Failed -> {
                      Toast.makeText(
                          context,
                          downloadResult.exception.message ?: "Pull language failed",
                          Toast.LENGTH_SHORT
                      ).show()
                      return
                  }
              }
          }
      

      I have make sure that the downloaded realm file is exists in the app's /files directory.
      I get the same error when use
      ``{{ RealmConfiguration.create(setOf(StringResourceRealm::class)) }}``

      Stacktrace & log output

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      kotlin.UninitializedPropertyAccessException: lateinit property filesDir has not been initialized
                                                       	at io.realm.kotlin.internal.RealmInitializer$Companion.getFilesDir(RealmInitializer.kt:34)
                                                       	at io.realm.kotlin.internal.platform.SystemUtilsAndroidKt.appFilesDirectory(SystemUtilsAndroid.kt:30)
                                                       	at io.realm.kotlin.RealmConfiguration$Builder.<init>(RealmConfiguration.kt:54)
                                                       	at com.staff.dynamic_langs.RealmDatabaseManager.createRealm(RealmDatabaseManager.kt:48)
                                                       	at com.staff.dynamic_langs.RealmDatabaseManager$createRealm$1.invokeSuspend(Unknown Source:15)
                                                       	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                       	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
                                                       	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
                                                       	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
                                                       	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
                                                       	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
                                                       	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
                                                       	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
                                                       	Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@3cce21c, Dispatchers.IO]
      

      Can you reproduce the bug?

      Always

      Reproduction Steps

      Project gradle:

      plugins {
          ...
          id("io.realm.kotlin") version "2.0.0"               apply true
      }
      
      tasks.create<Delete>("cleanRP") {
          group = "rp"
          delete(
              fileTree(rootProject.buildDir)
          )
      }
      
      

      App gradle:

      plugins {
          ...
          id("io.realm.kotlin")
      }
      
      dependencies {
           ,,,,
           // Realm
           Implementation("io.realm.kotlin:library-base:2.0.0")
      }
      
      

      How I create Realm:

      class RealmDatabaseManager @Inject constructor(
          val langDownloader: LangDownloader,
          val context: Context,
      ) {
          lateinit var realm: Realm
      
          suspend fun createRealm(realmName: String) {
              val downloadResult = langDownloader.download(
                  "scl/fi/p69qe33ylr5zr2e65f98z/vi.realm?rlkey=6fhjau9lgibbpr093gfb7zji0&st=smrxsn09&dl=0",
                  realmName
              )
              
              delay(5000)
      
              when(downloadResult) {
                  is DownloadLangResult.Success -> {
                      val configuration =
                          RealmConfiguration.Builder(setOf(StringResourceRealm::class))
                          .directory(downloadResult.directory)
                          .name(downloadResult.name)
                          .build()
      
                      realm = Realm.open(configuration)
                  }
      
                  is DownloadLangResult.Failed -> {
                      Toast.makeText(
                          context,
                          downloadResult.exception.message ?: "Pull language failed",
                          Toast.LENGTH_SHORT
                      ).show()
                      return
                  }
              }
          }
      }
      
      @AndroidEntryPoint
      class HomeActivity : BaseViewBindingActivity3<ActivityHomeBinding>(), HomeActivitySearchHolder {
             override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              
              ...
              val realmManager = RealmDatabaseManager(
                  LangDownloader(
                      downloader = DownloadLangClient().client,
                      context = this@HomeActivity,
                  ),
                  context = this@HomeActivity,
              )
              realmManager.createRealm("vi.realm")
          }
      
      

      Version

      2.0.0

      What Atlas App Services are you using?

      Local Database only

      Are you using encryption?

      No

      Platform OS and version(s)

      Samsung A15 OneUI 6.1 and Android Emulator Pixel 8 API 34

      Build environment

      Android Studio version: Android Studio Koala | 2024.1.1
      Android Build Tools version: 8.5
      Gradle version: 8.7

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: