Skip to content

Commit dd7395d

Browse files
committed
Simplify hasClass and rename to opaqueHasClass
The extra getName and catch housed in hasClass was not needed after further testing. Renamed to opaqueHasClass to provide a more specific name to what it does.
1 parent b5bfd59 commit dd7395d

File tree

1 file changed

+16
-23
lines changed
  • OneSignalSDK/onesignal/src/main/java/com/onesignal

1 file changed

+16
-23
lines changed

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

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -138,68 +138,61 @@ int initializationChecker(Context context, String oneSignalAppId) {
138138
return subscribableStatus;
139139
}
140140

141-
// Interim method that prevents proguard from making a wrong assumption that NoClassDefFoundError
142-
// catches are not needed when rules include "-assumenosideeffects" with getName().
143-
// -assumenosideeffects public final class java.lang.Class {
144-
// public java.lang.String getName();
145-
// }
146-
// Using Class instead of String as class renames would result incorrectly not finding the class
147-
// Using class.getName() as if no method is called then the try-catch would be removed.
148-
// This @Keep annotation is also key so this method does not get inlined
141+
// Interim method that works around Proguard's overly aggressive assumenosideeffects which
142+
// ignores keep rules.
143+
// This is specifically designed to address Proguard removing catches for NoClassDefFoundError
144+
// when the config has "-assumenosideeffects" with
145+
// java.lang.Class.getName() & java.lang.Object.getClass().
146+
// This @Keep annotation is key so this method does not get removed / inlined.
147+
// Addresses issue https://github.com/OneSignal/OneSignal-Android-SDK/issues/1423
149148
@Keep
150-
private static boolean hasClass(Class<?> clazz) {
151-
try {
152-
clazz.getName();
153-
return true;
154-
} catch (NoClassDefFoundError e) {
155-
return false;
156-
}
149+
private static boolean opaqueHasClass(Class<?> _class) {
150+
return true;
157151
}
158152

159153
static boolean hasFCMLibrary() {
160154
try {
161-
return hasClass(com.google.firebase.messaging.FirebaseMessaging.class);
155+
return opaqueHasClass(com.google.firebase.messaging.FirebaseMessaging.class);
162156
} catch (NoClassDefFoundError e) {
163157
return false;
164158
}
165159
}
166160

167-
@Keep
168161
static boolean hasGMSLocationLibrary() {
169162
try {
170-
return hasClass(com.google.android.gms.location.LocationListener.class);
163+
return opaqueHasClass(com.google.android.gms.location.LocationListener.class);
171164
} catch (NoClassDefFoundError e) {
172165
return false;
173166
}
174167
}
175168

176169
private static boolean hasHMSAvailabilityLibrary() {
177170
try {
178-
return hasClass(com.huawei.hms.api.HuaweiApiAvailability.class);
171+
return opaqueHasClass(com.huawei.hms.api.HuaweiApiAvailability.class);
179172
} catch (NoClassDefFoundError e) {
180173
return false;
181174
}
182175
}
183176

184177
private static boolean hasHMSPushKitLibrary() {
185178
try {
186-
return hasClass(com.huawei.hms.aaid.HmsInstanceId.class);
179+
return opaqueHasClass(com.huawei.hms.aaid.HmsInstanceId.class);
187180
} catch (NoClassDefFoundError e) {
188181
return false;
189182
}
190183
}
191184

192185
private static boolean hasHMSAGConnectLibrary() {
193186
try {
194-
return hasClass(com.huawei.agconnect.config.AGConnectServicesConfig.class);
187+
return opaqueHasClass(com.huawei.agconnect.config.AGConnectServicesConfig.class);
195188
} catch (NoClassDefFoundError e) {
196189
return false;
197190
}
198191
}
199192

200193
static boolean hasHMSLocationLibrary() {
201194
try {
202-
return hasClass(com.huawei.hms.location.LocationCallback.class);
195+
return opaqueHasClass(com.huawei.hms.location.LocationCallback.class);
203196
} catch (NoClassDefFoundError e) {
204197
return false;
205198
}
@@ -664,4 +657,4 @@ static boolean shouldLogMissingAppIdError(@Nullable String appId) {
664657
static int getRandomDelay(int minDelay, int maxDelay) {
665658
return new Random().nextInt(maxDelay + 1 - minDelay) + minDelay;
666659
}
667-
}
660+
}

0 commit comments

Comments
 (0)