Skip to content

FirebaseModelDownloader instance creation fails when using FirebaseModelDownloader.getInstance(app: FirebaseApp) when there is no default FirebaseApp while using a separate FirebaseApp #3321

@dev-gloomyfox

Description

@dev-gloomyfox

[READ] Step 1: Are you in the right place?

Issues filed here should be about bugs in the code in this repository.
If you have a general question, need help debugging, or fall into some
other category use one of these other channels:

  • For general technical questions, post a question on StackOverflow
    with the firebase tag.
  • For general Firebase discussion, use the firebase-talk
    google group.
  • For help troubleshooting your application that does not fall under one
    of the above categories, reach out to the personalized
    Firebase support channel.

[REQUIRED] Step 2: Describe your environment

  • Android Studio version: Android Studio 4.1.1
  • Firebase Component: ml-modeldownloader
  • Component version: gradle 6.5
    ** build.gradle(app)
...
dependencies {
    implementation platform('com.google.firebase:firebase-bom:29.0.3')
    implementation 'com.google.firebase:firebase-installations'
    implementation 'com.google.firebase:firebase-ml-modeldownloader-ktx'
...
}

[REQUIRED] Step 3: Describe the problem

FirebaseModelDownloader instance creation fails when using FirebaseModelDownloader.getInstance(app: FirebaseApp) when there is no default FirebaseApp while using a separate FirebaseApp

Steps to reproduce:

FirebaseModelDownloader instance creation fails when using FirebaseModelDownloader.getInstance(app: FirebaseApp) when there is no default FirebaseApp while using a separate FirebaseApp

E/AndroidRuntime: FATAL EXCEPTION: main
Process: dev.gloomyfox.firebase.test, PID: 10576
java.lang.RuntimeException: Unable to start activity ComponentInfo{dev.gloomyfox.firebase.test/dev.gloomyfox.firebase.test.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process dev.gloomyfox.firebase.test. Make sure to call FirebaseApp.initializeApp(Context) first.

...

at com.google.firebase.ml.modeldownloader.internal.FirebaseMlLogger.getInstance(FirebaseMlLogger.java:93)
at com.google.firebase.ml.modeldownloader.FirebaseModelDownloader.<init>(FirebaseModelDownloader.java:58)

...

Relevant Code:

The reason is that there are objects that use the default FirebaseApp among the properties inside FirebaseModelDownloader.
FirebaseMlLogger (https://github.com/firebase/firebase-android-sdk/blob/master/firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/internal/FirebaseMlLogger.java#L93) and ModelFileManager (https://github.com/firebase/firebase-android-sdk/blob/master/firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/internal/ModelFileManager.java#L62) uses the default FirebaseApp internally, so if there is no default FirebaseApp, it will not work even if a specific FirebaseApp is specified in getInstance.

In most cases there will be a Default FirebaseApp, and apart from the logger and file manager, downloading the model itself works well for the specified firebase project, so I don't think it's a big problem.
However, unless there is a special reason, it is more intuitive for the logger and file manager to use the FirebaseApp specified in the app parameter, and I think it is better because it is not tied to the default FirebaseApp state.

// TODO

// FirebaseMlLogger.java
@NonNull
public static FirebaseMlLogger getInstance(FirebaseApp app) {
    Preconditions.checkArgument(app != null, "Null is not a valid value of FirebaseApp.");
    return (FirebaseMlLogger)FirebaseApp.getInstance(app).get(FirebaseMlLogger.class);
}

// ModelFileManager.java
@NonNull
public static ModelFileManager getInstance(FirebaseApp app) {
    Preconditions.checkArgument(app != null, "Null is not a valid value of FirebaseApp.");
    return (ModelFileManager)FirebaseApp.getInstance(app).get(ModelFileManager.class);
}

// And use the above method in the constructor that receives the FirebaseApp parameter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions