Skip to content

Commit 18367c0

Browse files
authored
Fix crash on gma::Initialize without a Firebase App (#1320)
Initialize Util before using Util::FIndClass. Also add an integration test for initializing gma without a firebase app.
1 parent f8b6a36 commit 18367c0

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

gma/integration_test/src/integration_test.cc

+35-5
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ class FirebaseGmaUITest : public FirebaseGmaTest {
173173
void SetUp() override;
174174
};
175175

176+
class FirebaseGmaMinimalTest : public FirebaseTest {
177+
public:
178+
FirebaseGmaMinimalTest();
179+
~FirebaseGmaMinimalTest() override;
180+
};
181+
176182
// Runs GMA Tests on methods and functions that should be run
177183
// before GMA initializes.
178184
class FirebaseGmaPreInitializationTests : public FirebaseGmaTest {
@@ -228,11 +234,11 @@ void FirebaseGmaTest::SetUpTestSuite() {
228234
}
229235

230236
void FirebaseGmaTest::TearDownTestSuite() {
231-
// Workaround: GMA does some of its initialization in the main
232-
// thread, so if you terminate it too quickly after initialization
233-
// it can cause issues. Add a small delay here in case most of the
234-
// tests are skipped.
235-
ProcessEvents(1000);
237+
// GMA does some of its initialization in the main thread, so if you terminate
238+
// it before initialization has completed, it can cause issues. So wait for
239+
// any pending GMA initialization to be completed before calling terminate.
240+
WaitForCompletion(firebase::gma::InitializeLastResult(),
241+
"gma::InitializeLastResult");
236242
LogDebug("Shutdown GMA.");
237243
firebase::gma::Terminate();
238244
LogDebug("Shutdown Firebase App.");
@@ -301,6 +307,10 @@ firebase::Variant FirebaseGmaTest::GetVariantMap() {
301307
return variant_map;
302308
}
303309

310+
FirebaseGmaMinimalTest::FirebaseGmaMinimalTest() {}
311+
312+
FirebaseGmaMinimalTest::~FirebaseGmaMinimalTest() {}
313+
304314
FirebaseGmaUITest::FirebaseGmaUITest() {}
305315

306316
FirebaseGmaUITest::~FirebaseGmaUITest() {}
@@ -340,6 +350,26 @@ void FirebaseGmaPreInitializationTests::SetUpTestSuite() {
340350

341351
// Test cases below.
342352

353+
TEST_F(FirebaseGmaMinimalTest, TestInitializeGmaWithoutFirebase) {
354+
// Don't initialize mediation in this test so that a later test can still
355+
// verify that mediation has not been initialized.
356+
firebase::gma::DisableMediationInitialization();
357+
LogDebug("Initializing GMA without a Firebase App.");
358+
firebase::InitResult result;
359+
#if defined(ANDROID)
360+
::firebase::gma::Initialize(app_framework::GetJniEnv(),
361+
app_framework::GetActivity(), &result);
362+
#else // !defined(ANDROID)
363+
::firebase::gma::Initialize(&result);
364+
#endif // defined(ANDROID)
365+
EXPECT_EQ(result, ::firebase::kInitResultSuccess);
366+
WaitForCompletion(firebase::gma::InitializeLastResult(), "gma::Initialize");
367+
LogDebug("Successfully initialized GMA.");
368+
369+
LogDebug("Shutdown GMA.");
370+
firebase::gma::Terminate();
371+
}
372+
343373
TEST_F(FirebaseGmaPreInitializationTests, TestDisableMediationInitialization) {
344374
// Note: This test should be disabled or put in an entirely different test
345375
// binrary if we ever wish to test mediation in this application.

gma/src/android/gma_android.cc

+7-6
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,7 @@ Future<AdapterInitializationStatus> Initialize(JNIEnv* env, jobject activity,
285285
env->GetJavaVM(&g_java_vm);
286286
}
287287

288-
// GMA requires Google Play services if the class
289-
// "com.google.android.gms.ads.internal.ClientApi" does not exist.
290-
if (!util::FindClass(env, "com/google/android/gms/ads/internal/ClientApi") &&
291-
google_play_services::CheckAvailability(env, activity) !=
292-
google_play_services::kAvailabilityAvailable) {
288+
if (!util::Initialize(env, activity)) {
293289
if (init_result_out) {
294290
*init_result_out = kInitResultFailedMissingDependency;
295291
}
@@ -298,7 +294,12 @@ Future<AdapterInitializationStatus> Initialize(JNIEnv* env, jobject activity,
298294
return Future<AdapterInitializationStatus>();
299295
}
300296

301-
if (!util::Initialize(env, activity)) {
297+
// GMA requires Google Play services if the class
298+
// "com.google.android.gms.ads.internal.ClientApi" does not exist.
299+
if (!util::FindClass(env, "com/google/android/gms/ads/internal/ClientApi") &&
300+
google_play_services::CheckAvailability(env, activity) !=
301+
google_play_services::kAvailabilityAvailable) {
302+
util::Terminate(env);
302303
if (init_result_out) {
303304
*init_result_out = kInitResultFailedMissingDependency;
304305
}

release_build_files/readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,10 @@ workflow use only during the development of your app, not for publicly shipping
627627
code.
628628

629629
## Release Notes
630+
### Upcoming Release
631+
- Changes
632+
- GMA (Android): Fixed a crash when initializing GMA without a Firebase App.
633+
630634
### 11.3.0
631635
- Changes
632636
- General (Android): Update to Firebase Android BoM version 32.2.0.

0 commit comments

Comments
 (0)