firebase-android-sdk icon indicating copy to clipboard operation
firebase-android-sdk copied to clipboard

RuntimeException due to org.json:json dependency

Open Mercandj opened this issue 3 years ago • 2 comments

Environment

  • Android Studio version: Android Studio Bumblebee | 2021.1.1 Patch 3
  • Firebase Component: Database
  • Component version: 20.0.4

Describe the problem

Steps to reproduce:

  • What happened? Crash on the getReference of FirebaseDatabase.getInstance().getReference("whatever").
  • How can we make the problem occur? Add implementation("org.json:json:20220320") + minification
  • Reproduced 100% of the time on any Android device.

How to reproduce the issue?

Stacktrace

Note that the mapping is pointing b9.g to org.json.JSONStringer

2022-04-21 23:08:38.743 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mercandalli.android.apps.sampler, PID: 11865
    java.lang.RuntimeException: Uncaught exception in Firebase Database runloop (20.0.4). If you are not already on the latest version of the Firebase SDKs, try updating your dependencies. Should this problem persist, please file a report at https://github.com/firebase/firebase-android-sdk
        at j4.n$a$a.run(:96)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7870)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.NoSuchMethodError: No virtual method object()Lb9/g; in class Lb9/g; or its super classes (declaration of 'b9.g' appears in /data/app/~~MDSz81k4B6uCZgrZsQ9zyQ==/com.mercandalli.android.apps.sampler-66tdqwTq1qJ-KDvS2fRLdw==/base.apk)

Thank you for your amazing work on firebase, great tool for Android developpers =) Have a nice day ! @Mercandj

Mercandj avatar Apr 28 '22 19:04 Mercandj

Hi, @Mercandj this is due to the fact that the call signature used by your version of org.json does not match the call signature expected by Realtime Database. This is due to the differing versions between the built-in version shipped with Android and the one included in your gradle file. If you remove the dependency, is your app still able to run?

maneesht avatar May 05 '22 21:05 maneesht

Hi @maneesht,

My app is not directly depending on org.json but is depending on external non-android java libraries that are depending on the latest version of org.json (transitive dependency).

Indeed firebase is using the JSONStringer methods that are avaibable on the Android version directly on JSONStringer. But these methods have been moved to the supper class JSONWriter on "org.json:json:20220320"

Screenshot 2022-05-06 at 12 32 43

To fix the issue and keep JSONWriter and JSONStringer with the same signatures, I added these proguard rules and it works 🎉, no more crash

# https://github.com/firebase/firebase-android-sdk/issues/3688
-keep class org.json.** { *; }
-keepclassmembers class org.json.** { *; }

Thks for the help 🙏 .

Other possibilities for the fix, exclude transitive declaration from every libs using org.json via gradle "exclude" keyword. But did not work for me.

So, does not seems to be an issue related to firebase-android-sdk directly. Ok to close it if you want. I hope other dev facing this issue will know that even if the stacktrace is pointing to firebase, the error is caused by the org.json non-android dependency and firebase using org.json methods that have been moved to super class. And so the solution is to disable the minification for org.json. Hopefull google search indexed this issue to help futur devs =)

Mercandj avatar May 06 '22 10:05 Mercandj

Hey @Mercandj, sorry for the delayed response here. I'm glad you were able to resolve your issue. That said, I'll close this issue as resolved now. Thank you for providing this solution!

argzdev avatar May 22 '23 09:05 argzdev