Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android crash / NullPointerException #2477

Closed
mikehardy opened this issue Oct 1, 2021 · 14 comments · Fixed by #2478
Closed

Android crash / NullPointerException #2477

mikehardy opened this issue Oct 1, 2021 · 14 comments · Fixed by #2478

Comments

@mikehardy
Copy link
Contributor

mikehardy commented Oct 1, 2021

For some reason, in android release builds on my app (or debug builds if I configure minifyEnabled true for debug buildType) this crashes on this line (I carefully deobfuscated the stack trace, and it's definitely crashing here - no idea why minify triggers it when it's not the classic ClassNotFoundException or NoClassDef etc that proguard would normally throw)

09-30 22:08:02.832  8709  8709 E unknown:ReactNative: Exception in native call
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.get(java.lang.Object)' on a null object reference
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at h5.f.r(:218)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at com.facebook.react.uimanager.z0$m.a(:217)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at com.facebook.react.uimanager.z0$a.run(:915)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at com.facebook.react.uimanager.z0.U(:1026)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at com.facebook.react.uimanager.z0.s(:47)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at com.facebook.react.uimanager.z0$j.c(:1086)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at com.facebook.react.uimanager.f.a(:29)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at com.facebook.react.modules.core.g$d.a(:175)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at com.facebook.react.modules.core.a$a$a.doFrame(:85)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:970)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at android.view.Choreographer.doCallbacks(Choreographer.java:796)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at android.view.Choreographer.doFrame(Choreographer.java:727)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at android.os.Handler.handleCallback(Handler.java:938)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at android.os.Looper.loop(Looper.java:223)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at android.app.ActivityThread.main(ActivityThread.java:7656)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at java.lang.reflect.Method.invoke(Native Method)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
09-30 22:08:02.832  8709  8709 E unknown:ReactNative: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

And the relevant portion of android/app/build/outputs/mapping/debug/mapping.txt showing what h5.f.r is:


com.swmansion.reanimated.layoutReanimation.ReanimatedNativeHierarchyManager -> h5.f:
    java.util.HashMap cleanerCallback -> n
    java.util.HashMap toBeRemoved -> m
    com.facebook.react.uimanager.layoutanimation.LayoutAnimationController mReaLayoutAnimator -> o
    java.util.HashMap mPendingDeletionsForTag -> p
    134:163:void <init>(com.facebook.react.uimanager.ViewManagerRegistry,com.facebook.react.bridge.ReactApplicationContext) -> <init>
    169:178:void updateLayout(int,int,int,int,int,int) -> C
    234:235:void publicDropView(android.view.View) -> G
    238:250:void dropView(android.view.View) -> n
    181:231:void manageChildren(int,int[],com.facebook.react.uimanager.ViewAtIndex[],int[]) -> r

So it's in ReanimatedNativeHierarchyManager::manageChildren on line 218 - right where mPendingDeletionsForTag is dereferenced for a .get with no null check.

I believe that since mPendingDeletionsForTag is initialized to null, and only set to a valid HashMap in one of the constructors but not the other, I must be using the other constructor in my app.

Then when this line is hit, I get a null pointer exception.

I think this whole chunk could be wrapped in an if (mPendingDeletionsForTag != null) { /* ... */ }

Originally posted by @mikehardy in #2437 (comment)

@mikehardy
Copy link
Contributor Author

Please see the linked comment for the exact line of code - I highlighted it on the PR where it went in - #2437

The crash was observed using reanimated v2.3.0-beta.2 (just released) in combination with react-native 0.66.0-rc.4 as I was checking the release build that combo produced.

I was able to reproduce it using my app (which is not terribly fancy) by setting minifyEnabled true, so it should be pretty easy to reproduce, but at the same time I think my analysis of the NullPointerException is pretty easy to follow?

I can try to pull one of the examples here and set it up for repro if needed though

@mikehardy
Copy link
Contributor Author

It occurs to me that the reason this likely crashes on boot is that the first thing my app does is load up a react-navigation tab set which is thus transitively dependent on reanimated

This example would serve but it's pretty stale on react-native + reanimated https://github.com/react-navigation/react-navigation/blob/fa45dde59b0c2e386280f00ad08393881539c16d/example/package.json#L31-L36

@jacow
Copy link

jacow commented Oct 4, 2021

It occurs to me that the reason this likely crashes on boot is that the first thing my app does is load up a react-navigation tab set which is thus transitively dependent on reanimated

This example would serve but it's pretty stale on react-native + reanimated https://github.com/react-navigation/react-navigation/blob/fa45dde59b0c2e386280f00ad08393881539c16d/example/package.json#L31-L36

It also happened on me, i'm also using react nativagtion with bottom tab. Do you already found the solution ?

@ruairioliverwv
Copy link

I was able to get around this error by downgrading to v2.2.2 and building the aar for RN 0.66 from that tag.

Archive.zip

@mikehardy
Copy link
Contributor Author

mikehardy commented Oct 4, 2021

Yes, I proposed PR #2478 and I have build the 2.3.0-beta AAR for react-native-66-hermes locally. In a package-json post-install script I copy that AAR into place (over the existing one) and it runs perfectly. Just waiting for review on #2478. I'll attach the AAR I use here

@mikehardy
Copy link
Contributor Author

mikehardy commented Oct 4, 2021

react-native-reanimated 2.3.0-beta (that is, from dev branch on 2021/09/30) react-native-reanimated-66-hermes.aar.zip (note! it's actually just an .aar file that I renamed by adding .zip to it so GitHub would let me attach it. Rename it after download by just removing .zip from the end of the filename)(note2: grab the updated AAR I attached below 👇 )

    "postinstall": "patch-package && cp patches/react-native-reanimated-66-hermes.aar node_modules/react-native-reanimated/android/",

Szymon20000 pushed a commit that referenced this issue Oct 5, 2021
…imator (#2478)

## Description

mPendingDeletionsForTag is only initialized in some constructor paths, in others it might be null,
so we cannot always blindly dereference it

Fixes #2477

## Test code and steps to reproduce

For me, I'm able to reproduce a crash here anytime minifyEnabled is true in my build (you can set it in debug, to check it), and it crashes on startup for me.
@pang0018
Copy link

pang0018 commented Oct 6, 2021

@mikehardy the zipped aar file cannot unzipped on my mac. See below Any change we get an updated beta soon instead?
Screen Shot 2021-10-06 at 3 37 16 PM

@mikehardy
Copy link
Contributor Author

It's not actually a zip, sorry. It is literally just the .aar file with .zip added to it because GitHub will simply not allow you to attach an .aar (which is...technically a zip 😆 ) but it will allow a .zip. I'll amend my instructions. You can try unzip -l react-native-reaniminated-66-hermes.aar.zip on command line (-l is just "list", it's safe, or should be) to verify if you like

@mikehardy
Copy link
Contributor Author

With sincere apologies I realize the aar I packed up and attached above does not contain the actual class that I fixed in the related PR. Why is that? Because in order to really test the fix I removed those exact classes from the aar and worked on the java file in my own app's source tree. I got it working then of course forgot to put them back in the AAR I put here. So if anyone tried to use it before, you would have crashed on startup with a NoClassDefFoundError on ReanimatedNativeHierarchyManager.

I've put the classes in their rightful home in the AAR here and I'm re-attaching it. I only noticed myself when I went to take my own fix and put it in another app and couldn't realize why it was crashing !?!. Apologies

react-native-reanimated-66-hermes.aar.zip

@StevenJPx2
Copy link

@mikehardy I'm still getting this issue with 2.3.1. Do I downgrade to 2.3.0-beta and apply the patch?

@mikehardy
Copy link
Contributor Author

This was fixed and released in the related PR. You may have a different problem, I would open a new ticket. You may also try react-native-clean-project

@StevenJPx2
Copy link

I found what the issue was. The hardware acceleration requirement for reanimated doesn't play well with Flipper. While using Flipper to debug, it is fine, but when you quit Flipper while the debug version is running, it has an inadvertent crash.

Is this a fixable issue?

@mikehardy
Copy link
Contributor Author

I would definitely open a separate ticket for that with related information (stack trace from adb logcat etc, versions in play, exact steps to reproduce). It is not related to this issue

@mikehardy
Copy link
Contributor Author

separately: wow, subtle interaction there, and I don't use flipper to debug, explains why it does not happen to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants