From 75f91264d180cb002793fcf21ce42d0e9554177c Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Mon, 7 Aug 2023 07:35:05 -0700 Subject: [PATCH] Do not log exceptions from JNI lookups of APIs that are known to be unavailable on older devices (flutter/engine#44357) These exceptions are benign but were being logged every time an app is launched on a device with an Android API level below 26. --- engine/src/flutter/fml/platform/android/jni_util.cc | 6 ++++-- engine/src/flutter/fml/platform/android/jni_util.h | 2 +- .../platform/android/platform_view_android_jni_impl.cc | 6 +++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/engine/src/flutter/fml/platform/android/jni_util.cc b/engine/src/flutter/fml/platform/android/jni_util.cc index 407c0de9e0..deffe2b20b 100644 --- a/engine/src/flutter/fml/platform/android/jni_util.cc +++ b/engine/src/flutter/fml/platform/android/jni_util.cc @@ -185,11 +185,13 @@ bool HasException(JNIEnv* env) { return env->ExceptionCheck() != JNI_FALSE; } -bool ClearException(JNIEnv* env) { +bool ClearException(JNIEnv* env, bool silent) { if (!HasException(env)) { return false; } - env->ExceptionDescribe(); + if (!silent) { + env->ExceptionDescribe(); + } env->ExceptionClear(); return true; } diff --git a/engine/src/flutter/fml/platform/android/jni_util.h b/engine/src/flutter/fml/platform/android/jni_util.h index 0fb2be7c2f..6ece7fbd1b 100644 --- a/engine/src/flutter/fml/platform/android/jni_util.h +++ b/engine/src/flutter/fml/platform/android/jni_util.h @@ -42,7 +42,7 @@ ScopedJavaLocalRef VectorToBufferArray( bool HasException(JNIEnv* env); -bool ClearException(JNIEnv* env); +bool ClearException(JNIEnv* env, bool silent = false); bool CheckException(JNIEnv* env); std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable); diff --git a/engine/src/flutter/shell/platform/android/platform_view_android_jni_impl.cc b/engine/src/flutter/shell/platform/android/platform_view_android_jni_impl.cc index 63363b0aac..013d48ca25 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android_jni_impl.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android_jni_impl.cc @@ -1194,7 +1194,7 @@ bool PlatformViewAndroid::Register(JNIEnv* env) { if (g_image_get_hardware_buffer_method == nullptr) { // Continue on as this method may not exist at API <= 29. - fml::jni::ClearException(env); + fml::jni::ClearException(env, true); } g_image_close_method = env->GetMethodID(g_image_class->obj(), "close", "()V"); @@ -1214,11 +1214,11 @@ bool PlatformViewAndroid::Register(JNIEnv* env) { env->GetMethodID(g_hardware_buffer_class->obj(), "close", "()V"); if (g_hardware_buffer_close_method == nullptr) { // Continue on as this class may not exist at API <= 26. - fml::jni::ClearException(env); + fml::jni::ClearException(env, true); } } else { // Continue on as this class may not exist at API <= 26. - fml::jni::ClearException(env); + fml::jni::ClearException(env, true); } g_compute_platform_resolved_locale_method = env->GetMethodID(