@@ -58,6 +58,19 @@ METHOD_LOOKUP_DEFINITION(app_check,
5858 " com/google/firebase/appcheck/FirebaseAppCheck" ,
5959 APP_CHECK_METHODS)
6060
61+ // clang-format off
62+ #define DEFAULT_APP_CHECK_IMPL_METHODS (X ) \
63+ X (ResetAppCheckState, " resetAppCheckState" , " ()V" )
64+ // clang-format on
65+
66+ METHOD_LOOKUP_DECLARATION (default_app_check_impl,
67+ DEFAULT_APP_CHECK_IMPL_METHODS)
68+ METHOD_LOOKUP_DEFINITION (
69+ default_app_check_impl,
70+ PROGUARD_KEEP_CLASS
71+ " com/google/firebase/appcheck/internal/DefaultFirebaseAppCheck" ,
72+ DEFAULT_APP_CHECK_IMPL_METHODS)
73+
6174// clang-format off
6275#define JNI_APP_CHECK_PROVIDER_FACTORY_METHODS (X ) \
6376 X (Constructor, " <init>" , " (JJ)V" )
@@ -159,11 +172,13 @@ bool CacheAppCheckMethodIds(
159172 FIREBASE_ARRAYSIZE (kNativeJniAppCheckListenerMethods )))) {
160173 return false ;
161174 }
162- return app_check::CacheMethodIds (env, activity);
175+ return app_check::CacheMethodIds (env, activity) &&
176+ default_app_check_impl::CacheMethodIds (env, activity);
163177}
164178
165179void ReleaseAppCheckClasses (JNIEnv* env) {
166180 app_check::ReleaseClass (env);
181+ default_app_check_impl::ReleaseClass (env);
167182 jni_provider_factory::ReleaseClass (env);
168183 jni_provider::ReleaseClass (env);
169184 jni_app_check_listener::ReleaseClass (env);
@@ -359,6 +374,19 @@ AppCheckInternal::~AppCheckInternal() {
359374 env->DeleteGlobalRef (j_app_check_listener_);
360375 }
361376 if (app_check_impl_ != nullptr ) {
377+ // The Android App Check library holds onto the provider,
378+ // which can be a problem if it tries to call up to C++
379+ // after being deleted. So we use a hidden function meant
380+ // for testing purposes to clear out the App Check state,
381+ // to prevent this. Note, this assumes that the java object
382+ // is a DefaultFirebaseAppCheck (instead of a FirebaseAppCheck)
383+ // which is currently true, but may not be in the future.
384+ // We will have to rely on tests to detect if this changes.
385+ env->CallVoidMethod (app_check_impl_,
386+ default_app_check_impl::GetMethodId (
387+ default_app_check_impl::kResetAppCheckState ));
388+ FIREBASE_ASSERT (!util::CheckAndClearJniExceptions (env));
389+
362390 env->DeleteGlobalRef (app_check_impl_);
363391 }
364392
0 commit comments