Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 54b8921

Browse files
committedMar 9, 2023
Change GetGooglePlayServices to use reflection to avoid build errors in some tests.
1 parent ae3d689 commit 54b8921

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed
 

‎testing/test_framework/src/android/java/com/google/firebase/example/TestHelper.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
import android.content.Context;
1818
import android.os.Build;
19-
import com.google.android.gms.common.GoogleApiAvailability;
19+
import java.lang.Class;
20+
import java.lang.reflect.Method;
2021

2122
/**
2223
* A simple class with test helper methods.
@@ -31,6 +32,22 @@ public static boolean isRunningOnEmulator() {
3132
|| Build.DEVICE.contains("vbox86p") || Build.HARDWARE.contains("vbox86");
3233
}
3334
public static int getGooglePlayServicesVersion(Context context) {
34-
return GoogleApiAvailability.getInstance().getApkVersion(context);
35+
// Use reflection to invoke GoogleApiAvailability.getInstance().getApkVersion(context).
36+
// This avoids needing Google Play services to be present (and returns 0 if it's not)..
37+
38+
// GoogleApiAvailability
39+
Class<?> googleApiAvailabilityClass = Class.forName("com.google.android.gms.common.GoogleApiAvailability");
40+
if (googleApiAvailabilityClass == null) {
41+
return 0;
42+
}
43+
// .getInstance()
44+
Method getInstanceMethod = googleApiAvailabilityClass.getDeclaredMethod("getInstance");
45+
Object instance = getInstanceMethod.invoke(null);
46+
47+
// .getApkVersion(context)
48+
Method getApkVersionMethod = googleApiAvailabilityClass.getMethod("getApkVersion", new Class[]{context.class});
49+
Object apkVersionObject = getInstanceMethod.invoke(instance, context);
50+
51+
int apkVersion = apkVersionObject.intValue;
3552
}
3653
}

‎testing/test_framework/src/firebase_test_framework.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ class FirebaseTest : public testing::Test {
354354
// on a real device (or on desktop).
355355
static bool IsRunningOnEmulator();
356356

357-
// Return Google Play services version on Android, 0 elsewhere.
357+
// If on Android and Google Play services is available, returns the
358+
// Google Play services version. Otherwise, returns 0.
358359
static int GetGooglePlayServicesVersion();
359360

360361
// Returns true if the future completed as expected, fails the test and

0 commit comments

Comments
 (0)
Please sign in to comment.