NPE在FirebaseFunctions getInstance

时间:2018-11-02 11:25:33

标签: android firebase google-cloud-functions dexguard

调用FirebaseFunctions.getInstance(FirebaseApp.getInstance())时会引发NPE。在调用任何其他Firebase功能之前,应在扩展的App类FirebaseApp.initializeApp(this);中调用onCreate

这是堆栈跟踪:

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)

仅当使用发布配置构建应用程序时才会引发异常,这意味着代码被DexGuard混淆了,但是所有Firebase类都已排除在我的dexguard配置下方:

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

我正在使用:

  • com.google.firebase:firebase-functions:12.0.1(所有其他firebase库也使用相同版本)
  • DexGuard 8.1.15版
  • classpath'com.google.gms:google-services:4.0.1'

在使用调试配置时,一切都像魅力一样工作,知道如何解决这个问题吗?

已经检查了类似的问题,但已过时:Error with Firebase callable functions

编辑-新配置

我尝试使用以下内容更新DexGuard和Firebase的依赖关系:

  • com.google.firebase:firebase-functions:16.1.1(所有其他firebase和gms依赖项已更新为最新的,但play-services-ads为16.0.0)
  • DexGuard版本8.2.20
  • classpath'com.google.gms:google-services:4.0.1'

这是新的堆栈跟踪:

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)

1 个答案:

答案 0 :(得分:3)

对于可能会遇到相同问题的用户,这是由SDK使用null初始化FirebaseOptions并使用DexGuard保护应用程序引起的,因为{projectId“字段为FirebaseOptions.fromResource(Context) / p>

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

已通过在DexGuard配置文件中添加以下行来解决该问题:

-keepresources string/project_id