datso / react-native-pjsip

A PJSIP module for React Native.
http://datso.github.io/react-native-pjsip
GNU General Public License v3.0
273 stars 231 forks source link

[Android] Updating targetSdkVersion to 26 crashes the app #150

Closed enisit closed 5 years ago

enisit commented 5 years ago

Google Play does not allow to submit applications with targetSdkVersiong lower than 26, so I had to update it to 26 but after successful build app crashes in runtime. App works fine after installation but after some time spent in sleep mode, when user try to open application, Android fails to get PjSipService back and app just crashes. Any help would be great.

cmendes0101 commented 5 years ago

Did this occur before updating to target sdk of 26?

enisit commented 5 years ago

No, when I change target sdk back to 23 it works fine. Also I noticed that this problem occurs only on Android 8.0, I tested it on Android 7 and 6 and it works fine with target sdk 23 and 26. It may have something to do with garbage collection, because when you open app frequently it works fine, but after some time in background, it crashes when i try to register/create account when app opens it just crashes.

enisit commented 5 years ago

After spending a lot of time on debugging, I found out that starting with API 26, Android will kill services running in background after 60 seconds, except services like FCM an a few more. So Android will call onDestroy method in PjSipService when app goes in background. Problem was that endpoint instance was not destroyed properly and when the app goes back to foreground it just crashes. To solve this you need to add mEndpoint.delete() in onDestroy() method.

 @Override
    public void onDestroy() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            mWorkerThread.quitSafely();
        }
        try {
            if (mEndpoint != null) {
                mEndpoint.libDestroy();
                mEndpoint.delete(); // Add this line 
            }
        } catch (Exception e) {
            Log.w(TAG, "Failed to destroy PjSip library", e);
        }

        super.onDestroy();
    }