Skip to content

Commit cb719bd

Browse files
authored
Change GetGooglePlayServices to use reflection to avoid build errors (#1238)
1 parent 96a9183 commit cb719bd

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

analytics/integration_test/src/integration_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ TEST_F(FirebaseAnalyticsTest, TestGetAnalyticsInstanceID) {
108108

109109
TEST_F(FirebaseAnalyticsTest, TestGetSessionID) {
110110
// Don't run this test if Google Play services is < 23.0.0.
111-
SKIP_TEST_ON_ANDROID_GOOGLE_PLAY_SERVICES_BELOW(230000);
111+
SKIP_TEST_ON_ANDROID_IF_GOOGLE_PLAY_SERVICES_IS_OLDER_THAN(230000);
112112

113113
// iOS simulator tests are currently extra flaky, occasionally failing with an
114114
// "Analytics uninitialized" error even after multiple attempts.

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

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

1717
import android.content.Context;
1818
import android.os.Build;
19-
import com.google.android.gms.common.GoogleApiAvailability;
19+
import android.util.Log;
20+
import java.lang.Class;
21+
import java.lang.reflect.InvocationTargetException;
22+
import java.lang.reflect.Method;
2023

2124
/**
2225
* A simple class with test helper methods.
@@ -31,6 +34,28 @@ public static boolean isRunningOnEmulator() {
3134
|| Build.DEVICE.contains("vbox86p") || Build.HARDWARE.contains("vbox86");
3235
}
3336
public static int getGooglePlayServicesVersion(Context context) {
34-
return GoogleApiAvailability.getInstance().getApkVersion(context);
37+
// Use reflection to return GoogleApiAvailability.getInstance().getApkVersion(context);
38+
// This avoids needing Google Play services to be present (and returns 0 if it's not).
39+
try {
40+
// GoogleApiAvailability
41+
Class<?> googleApiAvailabilityClass =
42+
Class.forName("com.google.android.gms.common.GoogleApiAvailability");
43+
44+
// .getInstance()
45+
Method getInstanceMethod = googleApiAvailabilityClass.getDeclaredMethod("getInstance");
46+
Object instance = getInstanceMethod.invoke(null);
47+
48+
// .getApkVersion(context)
49+
Class[] getApkVersionParams = new Class[] {Class.forName("android.content.Context")};
50+
Method getApkVersionMethod =
51+
googleApiAvailabilityClass.getMethod("getApkVersion", getApkVersionParams);
52+
Object apkVersionObject = getApkVersionMethod.invoke(instance, context);
53+
if (apkVersionObject != null && apkVersionObject instanceof Integer) {
54+
return ((Integer) apkVersionObject).intValue();
55+
}
56+
} catch (Exception e) {
57+
Log.e(TAG, e.toString());
58+
}
59+
return 0;
3560
}
3661
}

testing/test_framework/src/firebase_test_framework.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ namespace firebase_test_framework {
8888
// SKIP_TEST_ON_MACOS
8989
// SKIP_TEST_ON_SIMULATOR / SKIP_TEST_ON_EMULATOR (identical)
9090
// SKIP_TEST_ON_IOS_SIMULATOR / SKIP_TEST_ON_ANDROID_EMULATOR
91+
// SKIP_TEST_ON_ANDROID_IF_GOOGLE_PLAY_SERVICES_IS_OLDER_THAN(version_code)
9192
//
9293
// Also includes a special macro SKIP_TEST_IF_USING_STLPORT if compiling for
9394
// Android STLPort, which does not fully support C++11.
@@ -206,7 +207,7 @@ namespace firebase_test_framework {
206207
#endif
207208

208209
#if defined(ANDROID)
209-
#define SKIP_TEST_ON_ANDROID_GOOGLE_PLAY_SERVICES_BELOW(x) \
210+
#define SKIP_TEST_ON_ANDROID_IF_GOOGLE_PLAY_SERVICES_IS_OLDER_THAN(x) \
210211
{ \
211212
int _required_ver_ = (x); \
212213
/* Example: 23.1.2 has version code 230102???. */ \
@@ -215,7 +216,7 @@ namespace firebase_test_framework {
215216
_required_ver_ *= 1000; \
216217
} \
217218
int _actual_ver_ = GetGooglePlayServicesVersion(); \
218-
if (_actual_ver_ < _required_ver_) { \
219+
if (_actual_ver_ > 0 && _actual_ver_ < _required_ver_) { \
219220
app_framework::LogInfo( \
220221
"Skipping %s, as Google Play services %d is below required %d", \
221222
test_info_->name(), _actual_ver_, _required_ver_); \
@@ -224,7 +225,7 @@ namespace firebase_test_framework {
224225
} \
225226
}
226227
#else
227-
#define SKIP_TEST_ON_ANDROID_GOOGLE_PLAY_SERVICES_BELOW(x) ((void)0)
228+
#define SKIP_TEST_ON_ANDROID_IF_GOOGLE_PLAY_SERVICES_IS_OLDER_THAN(x) ((void)0)
228229
#endif // defined(ANDROID)
229230

230231
#if defined(STLPORT)
@@ -354,7 +355,8 @@ class FirebaseTest : public testing::Test {
354355
// on a real device (or on desktop).
355356
static bool IsRunningOnEmulator();
356357

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

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

0 commit comments

Comments
 (0)