Skip to content

Commit b7a5c08

Browse files
committed
Fixed proguard compatibility
* Proguard is now renaming public classes so checked to static class based checks. - This way when Proguard renames the public class name it will be updated in OSUtils. - This is better then adding a proguard rule to keep class as allows renaming which means a smaller APK.
1 parent 48c94cd commit b7a5c08

File tree

1 file changed

+62
-18
lines changed
  • OneSignalSDK/onesignal/src/main/java/com/onesignal

1 file changed

+62
-18
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSUtils.java

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@
4646
import org.json.JSONException;
4747
import org.json.JSONObject;
4848

49+
import java.io.InputStream;
4950
import java.security.MessageDigest;
5051
import java.util.Locale;
52+
import java.util.Scanner;
5153
import java.util.UUID;
5254
import java.util.regex.Pattern;
5355

@@ -83,13 +85,23 @@ int initializationChecker(Context context, int deviceType, String oneSignalAppId
8385
return subscribableStatus;
8486
}
8587

86-
8788
static boolean hasFCMLibrary() {
88-
return classExists("com.google.firebase.messaging.FirebaseMessaging");
89+
try {
90+
// Using class instead of Strings for proguard compatibility
91+
// noinspection ConstantConditions
92+
return com.google.firebase.messaging.FirebaseMessaging.class != null;
93+
} catch (Throwable e) {
94+
return false;
95+
}
8996
}
9097

91-
static boolean hasGCMLibrary() {
92-
return classExists("com.google.android.gms.gcm.GoogleCloudMessaging");
98+
private static boolean hasGCMLibrary() {
99+
try {
100+
// noinspection ConstantConditions
101+
return com.google.android.gms.gcm.GoogleCloudMessaging.class != null;
102+
} catch (Throwable e) {
103+
return false;
104+
}
93105
}
94106

95107
Integer checkForGooglePushLibrary() {
@@ -110,14 +122,44 @@ Integer checkForGooglePushLibrary() {
110122
return null;
111123
}
112124

113-
Integer checkAndroidSupportLibrary(Context context) {
114-
if (!classExists("android.support.v4.view.MenuCompat")) {
125+
private static boolean hasWakefulBroadcastReceiver() {
126+
try {
127+
// noinspection ConstantConditions
128+
return android.support.v4.content.WakefulBroadcastReceiver.class != null;
129+
} catch (Throwable e) {
130+
return false;
131+
}
132+
}
133+
134+
private static boolean hasNotificationManagerCompat() {
135+
try {
136+
// noinspection ConstantConditions
137+
return android.support.v4.app.NotificationManagerCompat.class != null;
138+
} catch (Throwable e) {
139+
return false;
140+
}
141+
}
142+
143+
private static boolean hasJobIntentService() {
144+
try {
145+
// noinspection ConstantConditions
146+
return android.support.v4.app.JobIntentService.class != null;
147+
} catch (Throwable e) {
148+
return false;
149+
}
150+
}
151+
152+
private Integer checkAndroidSupportLibrary(Context context) {
153+
String version = getAndroidSupporVersionFromMetaFile();
154+
boolean hasWakefulBroadcastReceiver = hasWakefulBroadcastReceiver();
155+
boolean hasNotificationManagerCompat = hasNotificationManagerCompat();
156+
157+
if (version == null && !hasWakefulBroadcastReceiver && !hasNotificationManagerCompat) {
115158
OneSignal.Log(OneSignal.LOG_LEVEL.FATAL, "Could not find the Android Support Library. Please make sure it has been correctly added to your project.");
116159
return UserState.PUSH_STATUS_MISSING_ANDROID_SUPPORT_LIBRARY;
117160
}
118161

119-
if (!classExists("android.support.v4.content.WakefulBroadcastReceiver") ||
120-
!classExists("android.support.v4.app.NotificationManagerCompat")) {
162+
if (!hasWakefulBroadcastReceiver || !hasNotificationManagerCompat) {
121163
OneSignal.Log(OneSignal.LOG_LEVEL.FATAL, "The included Android Support Library is to old or incomplete. Please update to the 26.0.0 revision or newer.");
122164
return UserState.PUSH_STATUS_OUTDATED_ANDROID_SUPPORT_LIBRARY;
123165
}
@@ -127,7 +169,7 @@ Integer checkAndroidSupportLibrary(Context context) {
127169
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
128170
&& getTargetSdkVersion(context) >= Build.VERSION_CODES.O) {
129171
// Class was added in 26.0.0-beta2
130-
if (!classExists("android.support.v4.app.JobIntentService")) {
172+
if (!hasJobIntentService()) {
131173
OneSignal.Log(OneSignal.LOG_LEVEL.FATAL, "The included Android Support Library is to old or incomplete. Please update to the 26.0.0 revision or newer.");
132174
return UserState.PUSH_STATUS_OUTDATED_ANDROID_SUPPORT_LIBRARY;
133175
}
@@ -136,6 +178,17 @@ && getTargetSdkVersion(context) >= Build.VERSION_CODES.O) {
136178
return null;
137179
}
138180

181+
private static String getAndroidSupporVersionFromMetaFile() {
182+
InputStream inputStream = Object.class.getClassLoader().getResourceAsStream("META-INF/com.android.support_support-v4.version");
183+
if (inputStream == null)
184+
return null;
185+
186+
Scanner scanner = new Scanner(inputStream, "UTF-8");
187+
String version = scanner.useDelimiter("\\A").hasNext() ? scanner.next() : null;
188+
scanner.close();
189+
return version;
190+
}
191+
139192
int getDeviceType() {
140193
try {
141194
// Class only available on the FireOS and only when the following is in the AndroidManifest.xml.
@@ -317,13 +370,4 @@ static void sleep(int ms) {
317370
e.printStackTrace();
318371
}
319372
}
320-
321-
static boolean classExists(String classWithNamespace) {
322-
try {
323-
Class.forName(classWithNamespace);
324-
return true;
325-
} catch (ClassNotFoundException e) {
326-
return false;
327-
}
328-
}
329373
}

0 commit comments

Comments
 (0)