androidfirebasegoogle-cloud-functionsdexguard

NPE on FirebaseFunctions getInstance


When invoking FirebaseFunctions.getInstance(FirebaseApp.getInstance()) an NPE is thrown. FirebaseApp.initializeApp(this); is called inside extended App class onCreate before calling any other firebase functionality.

This is the stacktrace:

Fatal Exception: java.lang.NullPointerException
       at com.google.firebase.functions.internal.Preconditions.checkNotNull(Unknown Source:876)
       at com.google.firebase.functions.FirebaseFunctions.<init>(Unknown Source:77)
       at com.google.firebase.functions.FirebaseFunctions.getInstance(Unknown Source:141)
       at com.google.firebase.functions.FirebaseFunctions.getInstance(Unknown Source:159)

The exception is thrown only when building the application with release configuration, this means the code gets obfuscated with DexGuard but all firebase classes have been excluded from the obfuscation, below my dexguard config:

...
-keep class com.google.** { *; }
-keep class android.** { *; }
-keep class com.firebase.** { *; }
-keep class com.android.** { *; }
...

I'm using:

When using debug configuration everything works like charm, any idea how to solve this?

Already checked this similar question but it's outdated: Error with Firebase callable functions

EDIT - New configuration

I tried updating both DexGuard dependency and Firebase one with the ones below:

This is the new stacktrace:

Fatal Exception: java.lang.NullPointerException: null reference
       at com.google.firebase.functions.a.a.a(Unknown Source:30)
       at com.google.firebase.functions.FirebaseFunctions.(Unknown Source:77)
       at com.google.firebase.functions.FirebaseFunctions.getInstance(Unknown Source:141)
       at com.google.firebase.functions.FirebaseFunctions.getInstance(Unknown Source:154)

Solution

  • For the ones who may face the same problem, it was caused by the "projectId" field being null when the FirebaseOptions were initialised by the sdk using FirebaseOptions.fromResource(Context) and the app was protected with DexGuard

    FirebaseOptions {
        applicationId = ***********************, 
        apiKeyapiKey = ***********************, 
        databaseUrl = ***********************, 
        gcmSenderId = ***********************, 
        storageBucket = ***********************, 
        projectId = null
    } 
    

    The problem has been solved by adding the following line to the DexGuard config file:

    -keepresources string/project_id