Closed dan-ryan closed 1 year ago
I found a few problems with this issue:
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!
Oh sorry, I'm also logging to Crashlytics in my PreviewView class which causes the crash.
final FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
crashlytics.log(message);
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!
"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'
}
}
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'
}
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!
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.
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.
}
[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.
So I manually call FirebaseApp.initializeApp in the preview view:
But then it causes this error: