Skip to content

Commit b5bfd59

Browse files
committed
proguard -assumenosideeffects compatibility
Fix compatibility issue with apps that use -assumenosideeffects with java.lang.Class.getName() with progurad or dexguard. No issue with R8. Addressed this by adding a new private hasClass method and utilized it in all existing class exist helper methods. This method has the "Keep" annotation on it so proguard will never change it's code and will not make a wrong assumptions about side effects.
1 parent 757d253 commit b5bfd59

File tree

1 file changed

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

1 file changed

+27
-16
lines changed

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

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import android.os.Bundle;
4343
import android.os.Handler;
4444
import android.os.Looper;
45+
46+
import androidx.annotation.Keep;
4547
import androidx.annotation.NonNull;
4648
import androidx.annotation.Nullable;
4749
import androidx.core.app.NotificationManagerCompat;
@@ -136,59 +138,68 @@ int initializationChecker(Context context, String oneSignalAppId) {
136138
return subscribableStatus;
137139
}
138140

139-
// The the following is done to ensure Proguard compatibility with class existent detection
140-
// 1. Using Class instead of Strings as class renames would result incorrectly not finding the class
141-
// 2. class.getName() is called as if no method is called then the try-catch would be removed.
142-
// - Only an issue when using Proguard (NOT R8) and using getDefaultProguardFile('proguard-android-optimize.txt')
143-
static boolean hasFCMLibrary() {
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
149+
@Keep
150+
private static boolean hasClass(Class<?> clazz) {
144151
try {
145-
com.google.firebase.messaging.FirebaseMessaging.class.getName();
152+
clazz.getName();
146153
return true;
147154
} catch (NoClassDefFoundError e) {
148155
return false;
149156
}
150157
}
151158

159+
static boolean hasFCMLibrary() {
160+
try {
161+
return hasClass(com.google.firebase.messaging.FirebaseMessaging.class);
162+
} catch (NoClassDefFoundError e) {
163+
return false;
164+
}
165+
}
166+
167+
@Keep
152168
static boolean hasGMSLocationLibrary() {
153169
try {
154-
com.google.android.gms.location.LocationListener.class.getName();
155-
return true;
170+
return hasClass(com.google.android.gms.location.LocationListener.class);
156171
} catch (NoClassDefFoundError e) {
157172
return false;
158173
}
159174
}
160175

161176
private static boolean hasHMSAvailabilityLibrary() {
162177
try {
163-
com.huawei.hms.api.HuaweiApiAvailability.class.getName();
164-
return true;
178+
return hasClass(com.huawei.hms.api.HuaweiApiAvailability.class);
165179
} catch (NoClassDefFoundError e) {
166180
return false;
167181
}
168182
}
169183

170184
private static boolean hasHMSPushKitLibrary() {
171185
try {
172-
com.huawei.hms.aaid.HmsInstanceId.class.getName();
173-
return true;
186+
return hasClass(com.huawei.hms.aaid.HmsInstanceId.class);
174187
} catch (NoClassDefFoundError e) {
175188
return false;
176189
}
177190
}
178191

179192
private static boolean hasHMSAGConnectLibrary() {
180193
try {
181-
com.huawei.agconnect.config.AGConnectServicesConfig.class.getName();
182-
return true;
194+
return hasClass(com.huawei.agconnect.config.AGConnectServicesConfig.class);
183195
} catch (NoClassDefFoundError e) {
184196
return false;
185197
}
186198
}
187199

188200
static boolean hasHMSLocationLibrary() {
189201
try {
190-
com.huawei.hms.location.LocationCallback.class.getName();
191-
return true;
202+
return hasClass(com.huawei.hms.location.LocationCallback.class);
192203
} catch (NoClassDefFoundError e) {
193204
return false;
194205
}

0 commit comments

Comments
 (0)