Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on gma::Initialize without a Firebase App #1320

Merged
merged 22 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions gma/integration_test/src/integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ class FirebaseGmaUITest : public FirebaseGmaTest {
void SetUp() override;
};

class FirebaseGmaMinimalTest : public FirebaseTest {
public:
FirebaseGmaMinimalTest();
~FirebaseGmaMinimalTest() override;
};

// Runs GMA Tests on methods and functions that should be run
// before GMA initializes.
class FirebaseGmaPreInitializationTests : public FirebaseGmaTest {
Expand Down Expand Up @@ -228,11 +234,11 @@ void FirebaseGmaTest::SetUpTestSuite() {
}

void FirebaseGmaTest::TearDownTestSuite() {
// Workaround: GMA does some of its initialization in the main
// thread, so if you terminate it too quickly after initialization
// it can cause issues. Add a small delay here in case most of the
// tests are skipped.
ProcessEvents(1000);
// GMA does some of its initialization in the main thread, so if you terminate
// it before initialization has completed, it can cause issues. So wait for
// any pending GMA initialization to be completed before calling terminate.
WaitForCompletion(firebase::gma::InitializeLastResult(),
"gma::InitializeLastResult");
LogDebug("Shutdown GMA.");
firebase::gma::Terminate();
LogDebug("Shutdown Firebase App.");
Expand Down Expand Up @@ -301,6 +307,10 @@ firebase::Variant FirebaseGmaTest::GetVariantMap() {
return variant_map;
}

FirebaseGmaMinimalTest::FirebaseGmaMinimalTest() {}

FirebaseGmaMinimalTest::~FirebaseGmaMinimalTest() {}

FirebaseGmaUITest::FirebaseGmaUITest() {}

FirebaseGmaUITest::~FirebaseGmaUITest() {}
Expand Down Expand Up @@ -340,6 +350,26 @@ void FirebaseGmaPreInitializationTests::SetUpTestSuite() {

// Test cases below.

TEST_F(FirebaseGmaMinimalTest, TestInitializeGmaWithoutFirebase) {
// Don't initialize mediation in this test so that a later test can still
// verify that mediation has not been initialized.
firebase::gma::DisableMediationInitialization();
LogDebug("Initializing GMA without a Firebase App.");
firebase::InitResult result;
#if defined(ANDROID)
::firebase::gma::Initialize(app_framework::GetJniEnv(),
app_framework::GetActivity(), &result);
#else // !defined(ANDROID)
::firebase::gma::Initialize(&result);
#endif // defined(ANDROID)
EXPECT_EQ(result, ::firebase::kInitResultSuccess);
WaitForCompletion(firebase::gma::InitializeLastResult(), "gma::Initialize");
LogDebug("Successfully initialized GMA.");

LogDebug("Shutdown GMA.");
firebase::gma::Terminate();
}

TEST_F(FirebaseGmaPreInitializationTests, TestDisableMediationInitialization) {
// Note: This test should be disabled or put in an entirely different test
// binrary if we ever wish to test mediation in this application.
Expand Down
13 changes: 7 additions & 6 deletions gma/src/android/gma_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,7 @@ Future<AdapterInitializationStatus> Initialize(JNIEnv* env, jobject activity,
env->GetJavaVM(&g_java_vm);
}

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

if (!util::Initialize(env, activity)) {
// GMA requires Google Play services if the class
// "com.google.android.gms.ads.internal.ClientApi" does not exist.
if (!util::FindClass(env, "com/google/android/gms/ads/internal/ClientApi") &&
google_play_services::CheckAvailability(env, activity) !=
google_play_services::kAvailabilityAvailable) {
util::Terminate(env);
if (init_result_out) {
AlmostMatt marked this conversation as resolved.
Show resolved Hide resolved
*init_result_out = kInitResultFailedMissingDependency;
}
Expand Down
4 changes: 4 additions & 0 deletions release_build_files/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ workflow use only during the development of your app, not for publicly shipping
code.

## Release Notes
### Upcoming Release
- Changes
- GMA (Android): Fixed a crash when initializing GMA without a Firebase App.

### 11.3.0
- Changes
- General (Android): Update to Firebase Android BoM version 32.2.0.
Expand Down