facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
119.32k stars 24.36k forks source link

App crashes on startup with java.lang.IllegalArgumentException: Could not convert class android.os.BinderProxy / android.os.UserHandle #28461

Closed gagangoku closed 4 years ago

gagangoku commented 4 years ago

Please provide all the information requested. Issues that do not follow this format are likely to stall.

Description

I'm creating a React Native chat / messenger app. In order to get the intent the app was launched with (say with a share intent), I am overriding the createReactActivityDelegate method in MainActivity.java

However, this is causing the app to crash repeatably with the following exceptions:

03-30 10:30:50.165  6977  7026 E AndroidRuntime: FATAL EXCEPTION: mqt_native_modules
03-30 10:30:50.165  6977  7026 E AndroidRuntime: Process: com.heloprotocol.helo.app.rn.notificationtester, PID: 6977
03-30 10:30:50.165  6977  7026 E AndroidRuntime: java.lang.IllegalArgumentException: Could not convert class    
03-30 10:30:50.165  6977  7026 E AndroidRuntime:    at com.facebook.react.bridge.Arguments.fromBundle(Unknown Source:152)
03-30 10:30:50.165  6977  7026 E AndroidRuntime:    at d.f.n.M.c(Unknown Source:40)
03-30 10:30:50.165  6977  7026 E AndroidRuntime:    at d.f.n.M.b(Unknown Source:87)
03-30 10:30:50.165  6977  7026 E AndroidRuntime:    at d.f.n.M.a(Unknown Source:0)
03-30 10:30:50.165  6977  7026 E AndroidRuntime:    at d.f.n.G.run(Unknown Source:6)
03-30 10:30:50.165  6977  7026 E AndroidRuntime:    at android.os.Handler.handleCallback(Handler.java:794)
03-30 10:30:50.165  6977  7026 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:99)
03-30 10:30:50.165  6977  7026 E AndroidRuntime:    at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(Unknown Source:0)
03-30 10:30:50.165  6977  7026 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:176)
03-30 10:30:50.165  6977  7026 E AndroidRuntime:    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(Unknown Source:37)
03-30 10:30:50.165  6977  7026 E AndroidRuntime:    at java.lang.Thread.run(Thread.java:764)

and

03-31 12:58:53.844 29352 29396 E AndroidRuntime: FATAL EXCEPTION: mqt_native_modules
03-31 12:58:53.844 29352 29396 E AndroidRuntime: Process: com.heloprotocol.helo.chat.app.rn.notificationtester, PID: 29352
03-31 12:58:53.844 29352 29396 E AndroidRuntime: java.lang.IllegalArgumentException: Could not convert class android.os.BinderProxy
03-31 12:58:53.844 29352 29396 E AndroidRuntime:    at com.facebook.react.bridge.Arguments.fromBundle(Unknown Source:152)
03-31 12:58:53.844 29352 29396 E AndroidRuntime:    at d.f.n.M.c(Unknown Source:40)
03-31 12:58:53.844 29352 29396 E AndroidRuntime:    at d.f.n.M.b(Unknown Source:87)
03-31 12:58:53.844 29352 29396 E AndroidRuntime:    at d.f.n.M.a(Unknown Source:0)
03-31 12:58:53.844 29352 29396 E AndroidRuntime:    at d.f.n.G.run(Unknown Source:6)
03-31 12:58:53.844 29352 29396 E AndroidRuntime:    at android.os.Handler.handleCallback(Handler.java:873)
03-31 12:58:53.844 29352 29396 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 12:58:53.844 29352 29396 E AndroidRuntime:    at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(Unknown Source:0)
03-31 12:58:53.844 29352 29396 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:193)
03-31 12:58:53.844 29352 29396 E AndroidRuntime:    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(Unknown Source:37)
03-31 12:58:53.844 29352 29396 E AndroidRuntime:    at java.lang.Thread.run(Thread.java:764)

I can see the following exception in debug mode:

2020-03-16 17:26:05.214 31063-31063/com.heloprotocol.helo.app.rn.notificationtester E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.heloprotocol.helo.app.rn.notificationtester, PID: 31063
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.heloprotocol.helo.app.rn.notificationtester/com.heloprotocol.helo.app.rn.notificationtester.MainActivity}: java.lang.IllegalArgumentException: Could not convert class android.net.Uri$HierarchicalUri
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3047)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3182)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6898)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.IllegalArgumentException: Could not convert class android.net.Uri$HierarchicalUri
        at com.facebook.react.bridge.Arguments.fromBundle(Arguments.java:302)
        at com.facebook.react.ReactInstanceManager.attachRootViewToInstance(ReactInstanceManager.java:1063)
        at com.facebook.react.ReactInstanceManager.attachRootView(ReactInstanceManager.java:748)
        at com.facebook.react.ReactRootView.attachToReactInstanceManager(ReactRootView.java:568)
        at com.facebook.react.ReactRootView.startReactApplication(ReactRootView.java:399)
        at com.facebook.react.ReactRootView.startReactApplication(ReactRootView.java:363)
        at com.facebook.react.ReactDelegate.loadApp(ReactDelegate.java:102)
        at com.facebook.react.ReactActivityDelegate.loadApp(ReactActivityDelegate.java:88)
        at com.facebook.react.ReactActivityDelegate.onCreate(ReactActivityDelegate.java:83)
        at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:44)
        at com.heloprotocol.helo.app.rn.notificationtester.MainActivity.onCreate(MainActivity.java:38)
        at android.app.Activity.performCreate(Activity.java:7149)
        at android.app.Activity.performCreate(Activity.java:7140)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3027)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3182) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6898) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
2020-03-16 17:26:05.249 31063-31458/com.heloprotocol.helo.app.rn.notificationtester D/OSTracker: OS Event: crash
2020-03-16 17:26:05.258 31063-31063/com.heloprotocol.helo.app.rn.notificationtester I/Process: Sending signal. PID: 31063 SIG: 9

React Native version:

System:
    OS: macOS High Sierra 10.13.6
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
    Memory: 30.39 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 11.13.0 - /usr/local/bin/node
    npm: 6.7.0 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
    Android SDK:
      API Levels: 23, 25, 26, 27, 28
      Build Tools: 23.0.1, 25.0.3, 26.0.2, 26.0.3, 27.0.3, 28.0.2, 28.0.3
      System Images: android-19 | ARM EABI v7a, android-27 | Google Play Intel x86 Atom, android-28 | Intel x86 Atom_64, android-28 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom
  IDEs:
    Android Studio: 3.4 AI-183.6156.11.34.5522156
    Xcode: 10.1/10B61 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.9.0 => 16.9.0
    react-native: 0.61.5 => 0.61.5
  npmGlobalPackages:
    create-react-native-app: 1.0.0
    react-native-ci-tools: 1.1.0
    react-native-cli: 2.0.1
    react-native-git-upgrade: 0.2.7
    react-native-rename: 2.4.1

Steps To Reproduce

MainActivity.java:

package com.heloprotocol.helo.chat.app.rn.notificationtester;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.webkit.WebView;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.heloprotocol.helo.chat.app.rn.notificationtester.ArgumentsConverter;
import com.heloprotocol.helo.chat.app.rn.notificationtester.Util;
import io.branch.rnbranch.*;
import io.branch.rnbranch.RNBranchModule;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ReactActivity {
    private static final String TAG = "ReactNative.MainActivity";
    private static final String SHARE_INTENT_NAME = "receivedShareIntent";
    private static final String OTP_INTENT_NAME = "receivedOtp";
    private ReactContext reactContext;
    private List<WritableMap> buffer = new ArrayList<>();

    private BroadcastReceiver broadcastReceiver =  new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            WritableMap data = ArgumentsConverter.fromBundle(intent.getExtras());
            Log.e(TAG, "newmesage: " + data);
            ReactContext reactContext = MainActivity.this.reactContext;
            if (reactContext != null) {
                reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(OTP_INTENT_NAME, data);
            }
        }
    };

    @Override
    public void onNewIntent(Intent intent) {
        Log.i(TAG, "onNewIntent");
        super.onNewIntent(intent);
        setIntent(intent);
        sendIntentToJS(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        // Enable when you want to debug webview. Super useful stuff
        WebView.setWebContentsDebuggingEnabled(false);

        getReactContext();
        registerReceiver(this.broadcastReceiver, new IntentFilter("otp-broadcast"));
    }

    @Override
    protected ReactActivityDelegate createReactActivityDelegate() {
        return new ReactActivityDelegate(this, getMainComponentName()) {
            @Override
            protected Bundle getLaunchOptions() {
                Log.i(TAG, "getLaunchOptions");
                Intent intent = MainActivity.this.getIntent();
                MainActivity.this.sendIntentToJS(intent);
                Bundle bundle = intent.getExtras();
                return bundle;
            }
        };
    }

    @Override
    protected void onStart() {
        Log.i(TAG, "onStart");
        super.onStart();
        RNBranchModule.initSession(getIntent().getData(), this);
    }

    /**
     * Returns the name of the main component registered from JavaScript. This is used to schedule
     * rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "helo_app_rn";
    }

    private void getReactContext() {
        ReactContext reactContext = getReactNativeHost().getReactInstanceManager().getCurrentReactContext();
        Log.i(TAG, "getReactContext: reactContext: " + reactContext);

        if (reactContext != null) {
            this.reactContext = reactContext;
            processBufferedIntents();
            return;
        }

        Log.i(TAG, "getReactContext: Didnt get reactContext, schedule for later: " + reactContext);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                MainActivity.this.getReactContext();
            }
        }, 500);
    }

    private void sendIntentToJS(Intent intent) {
        try {
            Util.printIntent(intent);
            ReactContext reactContext = this.reactContext;
            Log.i(TAG, "sendIntentToJS: Using reactContext: " + reactContext);

            WritableMap data = Arguments.createMap();
            data.putString("package", defaultIfNull(intent.getPackage(), ""));
            data.putString("action", defaultIfNull(intent.getAction(), ""));
            data.putString("type", defaultIfNull(intent.getType(), ""));
            data.putMap("extras", ArgumentsConverter.fromBundle(intent.getExtras()));
            Log.i(TAG, "sendIntentToJS: data: " + data);

            if (reactContext != null) {
                reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(SHARE_INTENT_NAME, data);
            } else {
                Log.i(TAG, "sendIntentToJS: Buffering: " + data);
                buffer.add(data);
            }
        } catch (Exception e) {
            Log.e(TAG, "sendIntentToJS: Exception in processing intent: ", e);
        }
    }

    private void processBufferedIntents() {
        Log.i(TAG, "processBufferedIntents: Got reactContext: " + this.reactContext);

        if (!buffer.isEmpty()) {
            Log.i(TAG, "processBufferedIntents: Dispatching buffered intent: " + buffer.get(0));
            this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(SHARE_INTENT_NAME, buffer.get(0));
            buffer.clear();
        }
    }

    public static <T> T defaultIfNull(T object, T defaultValue) {
        return object == null ? defaultValue : object;
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.heloprotocol.helo.chat.app.rn.notificationtester">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

    <application android:name=".MainApplication"
                 android:label="@string/app_name"
                 android:usesCleartextTraffic="true"
                 android:icon="@mipmap/ic_launcher"
                 android:roundIcon="@mipmap/ic_launcher_round"
                 android:allowBackup="false"
                 android:theme="@style/AppTheme">

        <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <service android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
        <service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService"/>
        <service android:name=".MyTaskService"/>

        <!-- Firbase -->
        <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
                   android:resource="@mipmap/ic_notification_icon"/>
        <meta-data
                android:name="com.google.firebase.messaging.default_notification_color"
                android:resource="@color/pink"/>
        <meta-data
                android:name="com.google.firebase.messaging.default_notification_channel_id"
                android:value="@string/default_notification_channel_id"/>

        <activity android:name=".MainActivity"
                  android:launchMode="singleTask"
                  android:label="@string/app_name"
                  android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
                  android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>

            <!-- Intent filters for sharing images, videos, excel, text etc. -->
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SEND_MULTIPLE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
                <data android:mimeType="video/*" />
                <data android:mimeType="audio/*" />
                <data android:mimeType="text/*" />
                <data android:mimeType="text/*" />
                <data android:mimeType="application/*" />
            </intent-filter>

            <!-- Branch URI Scheme -->
            <intent-filter>
                <data android:scheme="helochat" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>

            <!-- Branch App Links (optional) -->
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="https" android:host="helochat.app.link" />
                <!-- example-alternate domain is required for App Links when the Journeys/Web SDK and Deepviews are used inside your website.  -->
                <data android:scheme="https" android:host="helochat-alternate.app.link" />
            </intent-filter>
        </activity>

        <!-- Branch init -->
        <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_njQILwjcgib2EbORbq5zQhcprBbOuCgZ" />
        <meta-data android:name="io.branch.sdk.BranchKey.test" android:value="key_test_jeHhknSqbFdpgCL1tzSAcfjfBsnmRfWj" />
        <!-- Branch testing (TestMode "true" to simulate fresh installs on dev environment) -->
        <meta-data android:name="io.branch.sdk.TestMode" android:value="false" />

        <!-- Branch install referrer tracking -->
        <receiver android:name="io.branch.referral.InstallListener" android:exported="true">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>

        <!-- SMS verification api receiver -->
        <receiver android:name="com.heloprotocol.helo.receiver.MySMSBroadcastReceiver" android:exported="true"
                  android:permission="com.google.android.gms.auth.api.phone.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" />
            </intent-filter>
        </receiver>

        <activity android:name="com.facebook.react.devsupport.DevSettingsActivity"/>

        <!-- Pushy Declarations -->
        <!-- Pushy Notification Receiver -->
        <!-- Do not modify - internal BroadcastReceiver that forwards notifications to the internal notification service-->
        <receiver android:name="me.pushy.sdk.react.receivers.PushReceiver" android:exported="false">
            <intent-filter>
                <!-- Do not modify this -->
                <action android:name="pushy.me"/>
            </intent-filter>
        </receiver>

        <!-- Pushy Update Receiver -->
        <!-- Do not modify - internal BroadcastReceiver that restarts the listener service -->
        <receiver android:name="me.pushy.sdk.receivers.PushyUpdateReceiver" android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
            </intent-filter>
        </receiver>

        <!-- Pushy Boot Receiver -->
        <!-- Do not modify - internal BroadcastReceiver that restarts the listener service -->
        <receiver android:name="me.pushy.sdk.receivers.PushyBootReceiver" android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

        <!-- Pushy Socket Service -->
        <!-- Do not modify - internal service -->
        <service android:name="me.pushy.sdk.services.PushySocketService"/>

        <!-- Pushy Job Service (added in Pushy SDK 1.0.35) -->
        <!-- Do not modify - internal service -->
        <service android:name="me.pushy.sdk.services.PushyJobService"
                 android:permission="android.permission.BIND_JOB_SERVICE"
                 android:exported="true"/>
        <!-- End Pushy Declarations -->

        <!-- File provider -->
        <provider android:name="androidx.core.content.FileProvider"
                  android:authorities="com.heloprotocol.helo.chat.app.rn.notificationtester.provider"
                  android:exported="false"
                  android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
        </provider>
    </application>

</manifest>

Expected Results

Expectation is for the app to not crash.

Snack, code example, screenshot, or link to a repository:

https://github.com/gagangoku/helo-app-rn/tree/reproduce

martppa commented 4 years ago

Hi! I am facing the same issue, but in my case when deploying in a Xiaomi device. Have you found any solution so far?

martppa commented 4 years ago

I have found a quick work arround but I think this is not a workful solution.

in initialProps provided by the ActivityDelegate comes a property with key "prpfile"

key: "profile" value: "UserHandle{0}" (toString() output)

if you remove this prop from the bundle the app will start properly.

gagangoku commented 4 years ago

I'm surprised more people are not facing this issue. I forked React Native to fix it : https://github.com/gagangoku/react-native

jagnesh commented 4 years ago

I have found a quick work arround but I think this is not a workful solution.

in initialProps provided by the ActivityDelegate comes a property with key "prpfile"

key: "profile" value: "UserHandle{0}" (toString() output)

if you remove this prop from the bundle the app will start properly. @martppa can you please give exact steps for this fix?

cyzaoj commented 4 years ago

this crash reason:

https://github.com/react-native-community/react-native-camera/pull/1835

stale[bot] commented 4 years ago

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

stale[bot] commented 4 years ago

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.