firebase / firebase-android-sdk

Firebase Android SDK
https://firebase.google.com
Apache License 2.0
2.29k stars 579 forks source link

Compose preview crashing with "Default FirebaseApp is not initialized" and then with "Unsupported Service: user". #4748

Closed dan-ryan closed 1 year ago

dan-ryan commented 1 year ago

[REQUIRED] Step 2: Describe your environment

Android Studio version: 2022.1.1 Patch 2 Firebase Component: Crashlytics Component version: BOM 31.2.3

[REQUIRED] Step 3: Describe the problem

I've started integrating Jetpack Compose into my Android project, but the previews are crashing in Android Studio when they call Firebase.

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process null. Make sure to call FirebaseApp.initializeApp(Context) first.   
at com.google.firebase.FirebaseApp.getInstance(FirebaseApp.java:179)   
at com.google.firebase.crashlytics.FirebaseCrashlytics.getInstance(FirebaseCrashlytics.java:199)   
at appendLog(LogManager.java:209)

So I manually call FirebaseApp.initializeApp in the preview view:

@Preview
@Composable
fun PreviewTestView() {
    AndroidView(
        modifier = Modifier.fillMaxSize(),
        factory = { context ->
            FirebaseApp.initializeApp(context)
            PreviewView(context).apply {
            }
        }
    )
}

But then it causes this error:

java.lang.AssertionError: Unsupported Service: user
at com.android.layoutlib.bridge.android.BridgeContext.getSystemService(BridgeContext.java:694)
at com.android.layoutlib.bridge.android.ApplicationContext.getSystemService(ApplicationContext.java:931)
at android.content.Context.getSystemService(Context.java:4158)
at androidx.core.os.UserManagerCompat$Api24Impl.isUserUnlocked(UserManagerCompat.java:58)
at androidx.core.os.UserManagerCompat.isUserUnlocked(UserManagerCompat.java:44)
at com.google.firebase.FirebaseApp.<init>(FirebaseApp.java:434)
at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:296)
at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:264)
at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:249)
google-oss-bot commented 1 year ago

I found a few problems with this issue:

argzdev commented 1 year ago

Thanks for reporting, @dan-ryan. I tried to reproduce the same behavior by creating an empty jetpack compose project from the Android Studio project creation template and then add firebase-crashlytics dependency with the following code snippet:

@Composable
fun Greeting(name: String) {
    val context = LocalContext.current

    FirebaseApp.initializeApp(context)
    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    val context = LocalContext.current
    FirebaseApp.initializeApp(context)

    Issue4748Theme {
        Greeting("Android")
    }
}

This seems to work fine though. Am I missing anything? Also any chance you could share a minimal reproducible example? It'll help us investigate this further. Thanks!

dan-ryan commented 1 year ago

Oh sorry, I'm also logging to Crashlytics in my PreviewView class which causes the crash.

        final FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
        crashlytics.log(message);
argzdev commented 1 year ago

Thanks for the quick response! Not sure what the issue is here, I simulated this similar to yours by inserting the code as a callback, but it still works properly.

@Composable
fun Greeting(name: String, method: (context: Context) -> Unit) {
    val context = LocalContext.current

    Text(text = "Hello $name!")
    var test = "test"
    test.apply {
        method(context)
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    val context = LocalContext.current
    FirebaseApp.initializeApp(context)

    Issue4748Theme {
        Greeting(
            "Android",
            method = { context ->
                FirebaseApp.initializeApp(context)
                val crashlytics = FirebaseCrashlytics.getInstance()
                crashlytics.log("message")
            }
        )
    }
}

Are you using an injection framework? Perhaps that's causing the issue. Could you provide a minimal repro for us to investigate this further? Thanks!

dan-ryan commented 1 year ago

"Are you using an injection framework" No.

The most simple fail with "Unsupported Service: user":

@Preview()
@Composable
fun DefaultPreview() {
    val context = LocalContext.current
    FirebaseApp.initializeApp(context)
}

This is weird that you got it working. Can you share what you have in your Gradle file? Maybe one of the other plugins is failing even if I'm not using them directly in Compose.

Here are my Gradle files:

plugins {
    id 'com.google.firebase.appdistribution'
    id 'com.google.firebase.crashlytics'
    id 'com.google.gms.google-services'
    id 'com.google.firebase.firebase-perf'
}

dependencies {
    implementation platform('com.google.firebase:firebase-bom:31.2.3')
    implementation 'com.google.firebase:firebase-crashlytics-ktx'
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-crashlytics-ndk'
    implementation 'com.google.firebase:firebase-perf-ktx'
    implementation 'com.google.firebase:firebase-messaging-ktx'
}
buildscript {
    dependencies {
        classpath 'com.google.gms:google-services:4.3.15'
        classpath 'com.google.firebase:firebase-appdistribution-gradle:3.2.0'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.4'
        classpath 'com.google.firebase:perf-plugin:1.4.2'
    }
}
argzdev commented 1 year ago

I think it might be due to the composeOptions. Can you check your kotlinCompilerExtensionVersion ?

Project build.gradle

buildscript {
    ext {
        compose_version = '1.2.0'
    }
    repositories {
        google()  // Google's Maven repository
        mavenCentral()  // Maven Central repository
    }
    dependencies {
        classpath 'com.google.gms:google-services:4.3.15'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.4'
    }
}
plugins {
    id 'com.android.application' version '7.4.1' apply false
    id 'com.android.library' version '7.4.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
}

Module build.gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'com.google.gms.google-services'
    id 'com.google.firebase.crashlytics'
}

android {
    namespace 'com.argz.issue4748'
    compileSdk 33

    defaultConfig {
        applicationId "com.argz.issue4748"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary 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'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.2.0'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.compose.material3:material3:1.0.0-alpha11'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

    implementation platform('com.google.firebase:firebase-bom:31.2.3')
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-crashlytics-ktx'
}
google-oss-bot commented 1 year ago

Hey @dan-ryan. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot commented 1 year ago

Since there haven't been any recent updates here, I am going to close this issue.

@dan-ryan if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

dan-ryan commented 1 year ago

My Compose stuff was all the latest version of 1.3.3 and compiler of 1.4.3. I didn't have time to look into this further and just did a try-catch. I'll try again later when I have free time.

        try {
            final FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
            crashlytics.log(message);
            if (throwable != null) {
                crashlytics.recordException(throwable);
            }
        } catch (IllegalStateException e) {
            //FirebaseCrashlytics.getInstance will crash for Compose previews.
        }