GitLiveApp / firebase-java-sdk

A pure java port of the Firebase Android SDK
Apache License 2.0
19 stars 6 forks source link

JVM crash on init #19

Closed dmazgalin closed 1 month ago

dmazgalin commented 1 month ago

Hi Folks, I was trying to make a multiplatform app. At the moment have some issues with Firebase init.

Have following code:

    val options = FirebaseOptions(
        applicationId = "***",
        projectId = "***",
        apiKey = "***"
    )

    Firebase.initialize(Application(), options, "Sample")

after this line the app is showing in console following stack trace.

FirebaseApp Device unlocked: initializing all Firebase APIs for app Sample kotlinx.serialization.json.internal.JsonDecodingException: Cannot read Json element because of unexpected end of the input at path: $ JSON input: at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24) at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:32) at kotlinx.serialization.json.internal.AbstractJsonLexer.fail(AbstractJsonLexer.kt:598) at kotlinx.serialization.json.internal.AbstractJsonLexer.fail$default(AbstractJsonLexer.kt:596) at kotlinx.serialization.json.internal.JsonTreeReader.read(JsonTreeReader.kt:107) at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeJsonElement(StreamingJsonDecoder.kt:50) at kotlinx.serialization.json.JsonElementSerializer.deserialize(JsonElementSerializers.kt:50) at kotlinx.serialization.json.JsonElementSerializer.deserialize(JsonElementSerializers.kt:27) at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:69) at kotlinx.serialization.json.Json.decodeFromString(Json.kt:107) at kotlinx.serialization.json.Json.parseToJsonElement(Json.kt:136) at com.google.firebase.auth.FirebaseAuth.(FirebaseAuth.kt:153) at com.google.firebase.auth.FirebaseAuthRegistrar.lambda$getComponents$0(FirebaseAuthRegistrar.java:21) at com.google.firebase.components.ComponentRuntime.lambda$discoverComponents$0(ComponentRuntime.java:160) at com.google.firebase.components.Lazy.get(Lazy.java:53) at com.google.firebase.components.ComponentRuntime.doInitializeEagerComponents(ComponentRuntime.java:322) at com.google.firebase.components.ComponentRuntime.initializeEagerComponents(ComponentRuntime.java:312) at com.google.firebase.FirebaseApp.initializeAllApis(FirebaseApp.java:607) at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:300) at dev.gitlive.firebase.FirebaseKt.initialize(firebase.kt:27) at com.sample.ComposableSingletons$ApplicationKt$lambda-3$1.invoke(Application.kt:67)

nbransby commented 1 month ago

See https://github.com/GitLiveApp/firebase-kotlin-sdk?tab=readme-ov-file#multiplatform

dmazgalin commented 1 month ago

@nbransby Additional initialization was done with following code

fun initFirebaseJVM(storage: LocalPreferencesRepository) { FirebasePlatform.initializeFirebasePlatform(object : FirebasePlatform() { override fun store(key: String, value: String) = storage.setString(key, value) override fun retrieve(key: String) = storage.getString(key) override fun clear(key: String) { storage.setString(key, "") }

    override fun log(msg: String) = println(msg)
})

}

I was following full guidance for implementing it. Still doesn#t work

nbransby commented 1 month ago

If you analyse the stack trace it would appear the your override fun retrieve(key: String) = storage.getString(key) is not returning the expected value

image

dmazgalin commented 1 month ago

@nbransby Yes you are right. The problem was when I wrote custom implementation for storing the preferences.

Retrive method should return null. Otherwise it is crashing with the stacktrace above.

Proper implementation gives no error. override fun retrieve(key: String): String? { println("Get key $key, value ${storage.getStringOrNull(key)}") return storage.getStringOrNull(key) }

dmazgalin commented 1 month ago

But I faces up another errow with Installations:

FirebaseApp Device unlocked: initializing all Firebase APIs for app Sample Get key com.google.firebase.auth.FIREBASE_USER[Sample], value null Get key last-used-date, value null CrossProcessLock encountered error while creating and acquiring the lock, ignoring java.lang.NoSuchMethodError: 'java.io.File android.content.Context.getFilesDir()' at com.google.firebase.installations.CrossProcessLock.acquire(CrossProcessLock.java:49) at com.google.firebase.installations.FirebaseInstallations.getPrefsWithGeneratedIdMultiProcessSafe(FirebaseInstallations.java:475) at com.google.firebase.installations.FirebaseInstallations.doRegistrationOrRefresh(FirebaseInstallations.java:361) at com.google.firebase.installations.FirebaseInstallations.lambda$getId$1(FirebaseInstallations.java:244) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at com.google.firebase.concurrent.CustomThreadFactory.lambda$newThread$0(CustomThreadFactory.java:47) at java.base/java.lang.Thread.run(Thread.java:842)

Seems src/main/java/android/content/Context.kt is missing the required method.

nbransby commented 1 month ago

installations not available in the java sdk as no one has attempted to port it yet, not quite sure why it is mentioned in the readme - I think because the android library is included at compile time but no ones worked on it operating correctly at runtime, I'll update the readme