Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use Firebase to Desktop #454

Closed Unanswered
xandebianchi asked this question in Q&A
Dec 29, 2023 · 5 comments · 4 replies
Discussion options

Hi, in the Readme we can see the lib can be used for all 4 targets, including Desktop.
But it's not possible to initialize Firebase, since the parameter is Android context.

You must be logged in to vote

Replies: 5 comments 4 replies

Comment options

you can create a context with

import android.app.Application
 val context = Application()
You must be logged in to vote
0 replies
Comment options

I tried this approach below, and I have this bug here when compile to Desktop using Firebase.

Inside commonMain, App.kt:

@OptIn(ExperimentalResourceApi::class)
@Composable
fun App() {
 val context = Application()
 Firebase.initialize(context)
 MaterialTheme {
 var showContent by remember { mutableStateOf(false) }
 val greeting = remember { Greeting().greet() }
 Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) { ...

Inside dektopMain:

fun main() = application {
 Window(onCloseRequest = ::exitApplication, title = "FourWaysProject") {
 App()
 }
}

Error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/android/gms/common/R$string
at com.google.android.gms.common.internal.StringResourceValueReader.(com.google.android.gms:play-services-basement@@18.0.0:3)
at com.google.firebase.FirebaseOptions.fromResource(FirebaseOptions.java:156)
at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:245)
at dev.gitlive.firebase.FirebaseKt.initialize(firebase.kt:24)
at ComposableSingletons$MainKt$lambda-11ドル.invoke(main.kt:12)
at ComposableSingletons$MainKt$lambda-11ドル.invoke(main.kt:10)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:117)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
at androidx.compose.ui.awt.ComposeWindow$setContent5ドル.invoke(ComposeWindow.desktop.kt:161)
at androidx.compose.ui.awt.ComposeWindow$setContent5ドル.invoke(ComposeWindow.desktop.kt:160)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
at androidx.compose.ui.awt.ComposeWindowDelegate.WindowContentLayout(ComposeWindowDelegate.desktop.kt:168)
at androidx.compose.ui.awt.ComposeWindowDelegate.access$WindowContentLayout(ComposeWindowDelegate.desktop.kt:43)
at androidx.compose.ui.awt.ComposeWindowDelegate$setContent3ドル1ドル.invoke(ComposeWindowDelegate.desktop.kt:156)
at androidx.compose.ui.awt.ComposeWindowDelegate$setContent3ドル1ドル.invoke(ComposeWindowDelegate.desktop.kt:155)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
at androidx.compose.ui.awt.ComposeWindowDelegate$setContent3ドル.invoke(ComposeWindowDelegate.desktop.kt:152)
at androidx.compose.ui.awt.ComposeWindowDelegate$setContent3ドル.invoke(ComposeWindowDelegate.desktop.kt:151)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
at androidx.compose.ui.ComposeScene$setContent5ドル.invoke(ComposeScene.skiko.kt:474)
at androidx.compose.ui.ComposeScene$setContent5ドル.invoke(ComposeScene.skiko.kt:473)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:195)
at androidx.compose.ui.platform.Wrapper_skikoKt$setContent2ドル1ドル.invoke(Wrapper.skiko.kt:47)
at androidx.compose.ui.platform.Wrapper_skikoKt$setContent2ドル1ドル.invoke(Wrapper.skiko.kt:46)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:248)
at androidx.compose.ui.platform.Wrapper_skikoKt.provide(Wrapper.skiko.kt:61)
at androidx.compose.ui.platform.Wrapper_skikoKt.access$provide(Wrapper.skiko.kt:1)
at androidx.compose.ui.platform.Wrapper_skikoKt$setContent2ドル.invoke(Wrapper.skiko.kt:46)
at androidx.compose.ui.platform.Wrapper_skikoKt$setContent2ドル.invoke(Wrapper.skiko.kt:45)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
at androidx.compose.runtime.ActualJvm_jvmKt.invokeComposable(ActualJvm.jvm.kt:39)
at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3341)
at androidx.compose.runtime.ComposerImpl.composeContent$runtime(Composer.kt:3274)
at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:588)
at androidx.compose.runtime.Recomposer.composeInitial$runtime(Recomposer.kt:1013)
at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:520)
at androidx.compose.ui.platform.Wrapper_skikoKt.setContent(Wrapper.skiko.kt:45)
at androidx.compose.ui.ComposeScene.setContent$ui(ComposeScene.skiko.kt:470)
at androidx.compose.ui.ComposeScene.setContent$ui$default(ComposeScene.skiko.kt:448)
at androidx.compose.ui.awt.ComposeBridge$setContent3ドル1ドル.invoke(ComposeBridge.desktop.kt:306)
at androidx.compose.ui.awt.ComposeBridge$setContent3ドル1ドル.invoke(ComposeBridge.desktop.kt:305)
at androidx.compose.ui.awt.ComposeBridge.catchExceptions(ComposeBridge.desktop.kt:150)
at androidx.compose.ui.awt.ComposeBridge.access$catchExceptions(ComposeBridge.desktop.kt:64)
at androidx.compose.ui.awt.ComposeBridge$setContent3ドル.invoke(ComposeBridge.desktop.kt:305)
at androidx.compose.ui.awt.ComposeBridge$setContent3ドル.invoke(ComposeBridge.desktop.kt:304)
at androidx.compose.ui.awt.ComposeBridge.initContent(ComposeBridge.desktop.kt:320)
at androidx.compose.ui.awt.WindowComposeBridge$component1ドル.addNotify(WindowComposeBridge.desktop.kt:50)
at java.desktop/java.awt.Container.addNotify(Container.java:2804)
at java.desktop/javax.swing.JComponent.addNotify(JComponent.java:4839)
at androidx.compose.ui.awt.ComposeWindowDelegate$_pane1ドル.addNotify(ComposeWindowDelegate.desktop.kt:87)
at java.desktop/java.awt.Container.addNotify(Container.java:2804)
at java.desktop/javax.swing.JComponent.addNotify(JComponent.java:4839)
at java.desktop/java.awt.Container.addNotify(Container.java:2804)
at java.desktop/javax.swing.JComponent.addNotify(JComponent.java:4839)
at java.desktop/java.awt.Container.addNotify(Container.java:2804)
at java.desktop/javax.swing.JComponent.addNotify(JComponent.java:4839)
at java.desktop/javax.swing.JRootPane.addNotify(JRootPane.java:729)
at java.desktop/java.awt.Container.addNotify(Container.java:2804)
at java.desktop/java.awt.Window.addNotify(Window.java:791)
at java.desktop/java.awt.Frame.addNotify(Frame.java:495)
at java.desktop/java.awt.Window.pack(Window.java:829)
at androidx.compose.ui.util.Windows_desktopKt.setSizeImpl-6HolHcs(Windows.desktop.kt:116)
at androidx.compose.ui.util.Windows_desktopKt.setSizeSafely-hQcJfNw(Windows.desktop.kt:55)
at androidx.compose.ui.window.Window_desktopKt$Window5ドル.invoke(Window.desktop.kt:237)
at androidx.compose.ui.window.Window_desktopKt$Window5ドル.invoke(Window.desktop.kt:174)
at androidx.compose.ui.window.Window_desktopKt$Window12ドル1ドル.invoke(Window.desktop.kt:425)
at androidx.compose.ui.window.Window_desktopKt$Window12ドル1ドル.invoke(Window.desktop.kt:418)
at androidx.compose.ui.window.AwtWindow_desktopKt$AwtWindow3ドル.invoke(AwtWindow.desktop.kt:78)
at androidx.compose.ui.window.AwtWindow_desktopKt$AwtWindow3ドル.invoke(AwtWindow.desktop.kt:76)
at androidx.compose.ui.util.UpdateEffect_desktopKt$UpdateEffect2ドル$performUpdate1ドル.invoke(UpdateEffect.desktop.kt:59)
at androidx.compose.ui.util.UpdateEffect_desktopKt$UpdateEffect2ドル$performUpdate1ドル.invoke(UpdateEffect.desktop.kt:55)
at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2300)
at androidx.compose.runtime.snapshots.SnapshotStateObserver$ObservedScopeMap.observe(SnapshotStateObserver.kt:471)
at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:234)
at androidx.compose.ui.util.UpdateEffect_desktopKt$UpdateEffect2ドル.invoke$performUpdate(UpdateEffect.desktop.kt:55)
at androidx.compose.ui.util.UpdateEffect_desktopKt$UpdateEffect2ドル.invoke(UpdateEffect.desktop.kt:64)
at androidx.compose.ui.util.UpdateEffect_desktopKt$UpdateEffect2ドル.invoke(UpdateEffect.desktop.kt:47)
at androidx.compose.runtime.DisposableEffectImpl.onRemembered(Effects.kt:81)
at androidx.compose.runtime.CompositionImpl$RememberEventDispatcher.dispatchRememberObservers(Composition.kt:1137)
at androidx.compose.runtime.CompositionImpl.applyChangesInLocked(Composition.kt:828)
at androidx.compose.runtime.CompositionImpl.applyChanges(Composition.kt:849)
at androidx.compose.runtime.Recomposer.composeInitial$runtime(Recomposer.kt:1041)
at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:520)
at androidx.compose.ui.window.Application_desktopKt$awaitApplication2ドル1ドル2ドル.invokeSuspend(Application.desktop.kt:219)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771)
at java.desktop/java.awt.EventQueue4ドル.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue4ドル.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.lang.ClassNotFoundException: com.google.android.gms.common.R$string
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 111 more

Task :composeApp:run FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':composeApp:run'.

Process 'command 'C:\Program Files\Java\jdk-17\bin\java.exe'' finished with non-zero exit value 1

  • Try:

Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.

BUILD FAILED in 30s
5 actionable tasks: 4 executed, 1 up-to-date
PS D:\project> ./gradlew :composeApp:run
Type-safe project accessors is an incubating feature.

Task :composeApp:run

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
Comment options

To be honest I also don't understand how to do it.
Even import of Application from android package is impossible in commonMain in my project.
Am I missing sth?

Comment options

Has to be in the jvm source set

Comment options

For me it worked this way:

Inside commonMain, App.kt:

fun App() {
 val context = Application().applicationContext
 FirebasePlatform.initializeFirebasePlatform(object : FirebasePlatform() {
 val storage = mutableMapOf<String, String>()
 override fun store(key : String, value : String) = storage.set(key, value)
 override fun retrieve(key : String) = storage[key]
 override fun clear(key : String) { storage.remove(key) }
 override fun log(msg : String) = println(msg)
 })
 val options : FirebaseOptions = FirebaseOptions.Builder()
 .setProjectId("your-projeid")
 .setApiKey("your-apikey")
 .setApplicationId("your-appId")
 .build()
 Firebase.initialize(context, options)
 MaterialTheme{
 //your-compose-content
 }
}

Inside dektopMain:
fun main() = application { Window(onCloseRequest = ::exitApplication, title = "FourWaysProject") { App() } }

You must be logged in to vote
1 reply
Comment options

I assume this works for you in common main because you only have android and jvm targets?

Comment options

But now I have another problem, when I log the user into "FirebaseAuth", and it gives an error in "coroutines".
This happens whenever I run it on desktop, on Android it works normally

Inside commonMain, App.kt:

val auth = FirebaseAuth.getInstance()
 auth.signInWithEmailAndPassword("teste@email.teste", "123456")

Error:
Exception in thread "OkHttp https://securetoken.googleapis.com/..." java.lang.IllegalStateException: Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher, e.g. 'kotlinx-coroutines-android' and ensure it has the same version as 'kotlinx-coroutines-core'
at kotlinx.coroutines.internal.MainDispatchersKt.throwMissingMainDispatcherException(MainDispatchers.kt:81)
at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:112)
at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:96)
at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:319)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:25)
at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:110)
at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
at com.google.firebase.auth.FirebaseAuth.setUser(FirebaseAuth.kt:147)
at com.google.firebase.auth.FirebaseAuth.access$setUser(FirebaseAuth.kt:104)
at com.google.firebase.auth.FirebaseAuth$enqueueRefreshTokenCall1ドル.onResponse(FirebaseAuth.kt:332)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:203)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:840)
Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@1dec9e31, Dispatchers.Main[missing]]

You must be logged in to vote
1 reply
Comment options

That issue is known: GitLiveApp/firebase-java-sdk#6
Prehaps you could find out the cause?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /