From 95c350dcd2fd2fb1c38e8350460bb998f649d2c5 Mon Sep 17 00:00:00 2001 From: Matt Carroll Date: Thu, 14 Mar 2019 21:13:03 -0700 Subject: [PATCH] Bugfix: Prevent crash when responding to a platform message after FlutterJNI detaches from native (#28651). (flutter/engine#8170) --- .../flutter/embedding/engine/FlutterJNI.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java index f8405852a5..925e2a8d72 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java @@ -10,6 +10,7 @@ import android.graphics.SurfaceTexture; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.UiThread; +import android.util.Log; import android.view.Surface; import java.nio.ByteBuffer; @@ -494,8 +495,11 @@ public class FlutterJNI { @UiThread public void invokePlatformMessageEmptyResponseCallback(int responseId) { - ensureAttachedToNative(); - nativeInvokePlatformMessageEmptyResponseCallback(nativePlatformViewId, responseId); + if (isAttached()) { + nativeInvokePlatformMessageEmptyResponseCallback(nativePlatformViewId, responseId); + } else { + Log.w(TAG, "Tried to send a platform message response, but FlutterJNI was detached from native C++. Could not send. Response ID: " + responseId); + } } // Send an empty response to a platform message received from Dart. @@ -506,13 +510,16 @@ public class FlutterJNI { @UiThread public void invokePlatformMessageResponseCallback(int responseId, ByteBuffer message, int position) { - ensureAttachedToNative(); - nativeInvokePlatformMessageResponseCallback( - nativePlatformViewId, - responseId, - message, - position - ); + if (isAttached()) { + nativeInvokePlatformMessageResponseCallback( + nativePlatformViewId, + responseId, + message, + position + ); + } else { + Log.w(TAG, "Tried to send a platform message response, but FlutterJNI was detached from native C++. Could not send. Response ID: " + responseId); + } } // Send a data-carrying response to a platform message received from Dart.