Skip to content

Commit 397e2c1

Browse files
committed
Fix class exists dection with proguard optimize
* Fixed issue with Proguard removing try-catch from class exist dection * Added a method call to prevent it from incorrectly optimizing - class.getName() was only picked due it it's runtime preformance. - Any method call inside of the try would do.
1 parent d25a672 commit 397e2c1

File tree

1 file changed

+21
-17
lines changed
  • OneSignalSDK/onesignal/src/main/java/com/onesignal

1 file changed

+21
-17
lines changed

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,65 +134,69 @@ int initializationChecker(Context context, String oneSignalAppId) {
134134
return subscribableStatus;
135135
}
136136

137+
// The the following is done to ensure Proguard compatibility with class existent detection
138+
// 1. Using Class instead of Strings as class renames would result incorrectly not finding the class
139+
// 2. class.getName() is called as if no method is called then the try-catch would be removed.
140+
// - Only an issue when using Proguard (NOT R8) and using getDefaultProguardFile('proguard-android-optimize.txt')
141+
137142
static boolean hasFCMLibrary() {
138143
try {
139-
// Using class instead of Strings for proguard compatibility
140-
// noinspection ConstantConditions
141-
return com.google.firebase.messaging.FirebaseMessaging.class != null;
142-
} catch (Throwable e) {
144+
com.google.firebase.messaging.FirebaseMessaging.class.getName();
145+
return true;
146+
} catch (NoClassDefFoundError e) {
143147
return false;
144148
}
145149
}
146150

147151
private static boolean hasGCMLibrary() {
148152
try {
149-
// noinspection ConstantConditions
150-
return com.google.android.gms.gcm.GoogleCloudMessaging.class != null;
151-
} catch (Throwable e) {
153+
com.google.android.gms.gcm.GoogleCloudMessaging.class.getName();
154+
return true;
155+
} catch (NoClassDefFoundError e) {
152156
return false;
153157
}
154158
}
155159

156160
static boolean hasGMSLocationLibrary() {
157161
try {
158-
// noinspection ConstantConditions
159-
return com.google.android.gms.location.LocationListener.class != null;
162+
com.google.android.gms.location.LocationListener.class.getName();
163+
return true;
160164
} catch (NoClassDefFoundError e) {
161165
return false;
162166
}
163167
}
164168

165169
private static boolean hasHMSAvailabilityLibrary() {
166170
try {
167-
// noinspection ConstantConditions
168-
return com.huawei.hms.api.HuaweiApiAvailability.class != null;
171+
com.huawei.hms.api.HuaweiApiAvailability.class.getName();
172+
return true;
169173
} catch (NoClassDefFoundError e) {
170174
return false;
171175
}
172176
}
173177

174178
private static boolean hasHMSPushKitLibrary() {
175179
try {
176-
// noinspection ConstantConditions
177-
return com.huawei.hms.aaid.HmsInstanceId.class != null;
180+
com.huawei.hms.aaid.HmsInstanceId.class.getName();
181+
return true;
178182
} catch (NoClassDefFoundError e) {
179183
return false;
180184
}
181185
}
182186

183187
private static boolean hasHMSAGConnectLibrary() {
184188
try {
185-
// noinspection ConstantConditions
186-
return com.huawei.agconnect.config.AGConnectServicesConfig.class != null;
189+
com.huawei.agconnect.config.AGConnectServicesConfig.class.getName();
190+
return true;
187191
} catch (NoClassDefFoundError e) {
188192
return false;
189193
}
190194
}
191195

192196
static boolean hasHMSLocationLibrary() {
193197
try {
194-
// noinspection ConstantConditions
195-
return com.huawei.hms.location.LocationCallback.class != null;
198+
com.huawei.hms.location.LocationCallback.class.getName();
199+
return true;
196200
} catch (NoClassDefFoundError e) {
197201
return false;
198202
}

0 commit comments

Comments
 (0)