Skip to content

fix(android): handle null versionName in isNewBinary()#8397

Open
dawiddominiak wants to merge 1 commit intoionic-team:mainfrom
dawiddominiak:fix/android-bridge-null-versionname
Open

fix(android): handle null versionName in isNewBinary()#8397
dawiddominiak wants to merge 1 commit intoionic-team:mainfrom
dawiddominiak:fix/android-bridge-null-versionname

Conversation

@dawiddominiak
Copy link

Bug

Bridge.isNewBinary() crashes with a NullPointerException on app startup. The culprit is line 437:

versionName = pInfo.versionName;

PackageInfo.versionName is @Nullable on Android. When it's null, line 442 blows up:

if (!versionCode.equals(lastVersionCode) || !versionName.equals(lastVersionName)) {

The variable starts as "" on line 425, but gets overwritten with null from pInfo.versionName. Then .equals() is called on null. Game over.

This happens on sideloaded APKs and some emulator builds where versionName isn't set in the manifest. We hit it in production on a Pixel 6 Pro emulator running Android 12.

Fix

One-line change: null-coalesce pInfo.versionName back to "" (matching the initializer on line 425). The comparison logic stays exactly the same.

versionName = pInfo.versionName != null ? pInfo.versionName : "";

Stack trace from Sentry

NullPointerException: Attempt to invoke virtual method
  'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    at com.getcapacitor.Bridge.isNewBinary(Bridge.java:442)
    at com.getcapacitor.Bridge.loadWebView(Bridge.java:295)
    at com.getcapacitor.Bridge.<init>(Bridge.java:233)
    at com.getcapacitor.Bridge$Builder.create(Bridge.java:1599)
    at com.getcapacitor.BridgeActivity.load(BridgeActivity.java:48)
    at com.getcapacitor.BridgeActivity.onCreate(BridgeActivity.java:42)

PackageInfo.versionName is @nullable on Android. When it returns
null (e.g. on sideloaded APKs or certain emulator builds where
versionName is not set in the manifest), the .equals() call on
line 442 throws a NullPointerException, crashing the app on
startup before the WebView even loads.

Null-coalesce to "" which matches the variable's initializer on
line 425, so the comparison still works correctly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant