@@ -58,6 +58,19 @@ METHOD_LOOKUP_DEFINITION(app_check,
58
58
" com/google/firebase/appcheck/FirebaseAppCheck" ,
59
59
APP_CHECK_METHODS)
60
60
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
+
61
74
// clang-format off
62
75
#define JNI_APP_CHECK_PROVIDER_FACTORY_METHODS (X ) \
63
76
X (Constructor, " <init>" , " (JJ)V" )
@@ -159,11 +172,13 @@ bool CacheAppCheckMethodIds(
159
172
FIREBASE_ARRAYSIZE (kNativeJniAppCheckListenerMethods )))) {
160
173
return false ;
161
174
}
162
- return app_check::CacheMethodIds (env, activity);
175
+ return app_check::CacheMethodIds (env, activity) &&
176
+ default_app_check_impl::CacheMethodIds (env, activity);
163
177
}
164
178
165
179
void ReleaseAppCheckClasses (JNIEnv* env) {
166
180
app_check::ReleaseClass (env);
181
+ default_app_check_impl::ReleaseClass (env);
167
182
jni_provider_factory::ReleaseClass (env);
168
183
jni_provider::ReleaseClass (env);
169
184
jni_app_check_listener::ReleaseClass (env);
@@ -359,6 +374,19 @@ AppCheckInternal::~AppCheckInternal() {
359
374
env->DeleteGlobalRef (j_app_check_listener_);
360
375
}
361
376
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
+
362
390
env->DeleteGlobalRef (app_check_impl_);
363
391
}
364
392
0 commit comments