Skip to content

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

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

Merged
merged 22 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
30 changes: 30 additions & 0 deletions gma/integration_test/src/integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,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 @@ -283,6 +289,10 @@ firebase::gma::AdRequest FirebaseGmaTest::GetAdRequest() {
return request;
}

FirebaseGmaMinimalTest::FirebaseGmaMinimalTest() {}

FirebaseGmaMinimalTest::~FirebaseGmaMinimalTest() {}

FirebaseGmaUITest::FirebaseGmaUITest() {}

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

// Test cases below.

TEST_F(FirebaseGmaMinimalTest, TestInitializeGmaWithoutFirebase) {
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);
LogDebug("Successfully initialized GMA.");
// 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);
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
12 changes: 6 additions & 6 deletions gma/src/android/gma_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,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 @@ -296,7 +292,11 @@ 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) {
if (init_result_out) {
*init_result_out = kInitResultFailedMissingDependency;
}
Expand Down
1 change: 1 addition & 0 deletions release_build_files/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ code.
- Storage (Desktop): Fixed a crash on Windows when uploading files from a
path containing non-ANSI characters (Unicode above U+00FF).
- Firestore: Added MultiDb support. ([#1321](https://github.com/firebase/firebase-cpp-sdk/pull/1321)).
- GMA (Android): Fixed a crash when Initializing GMA without a Firebase App.

### 11.0.1
- Changes
Expand Down