Skip to content

Commit 1953260

Browse files
authored
Merge pull request #1492 from OneSignal/fix/proguard_assumenosideeffects
Proguard -assumenosideeffects Compatibility
2 parents c244915 + dd7395d commit 1953260

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
@@ -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,61 @@ 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')
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
148+
@Keep
149+
private static boolean opaqueHasClass(Class<?> _class) {
150+
return true;
151+
}
152+
143153
static boolean hasFCMLibrary() {
144154
try {
145-
com.google.firebase.messaging.FirebaseMessaging.class.getName();
146-
return true;
155+
return opaqueHasClass(com.google.firebase.messaging.FirebaseMessaging.class);
147156
} catch (NoClassDefFoundError e) {
148157
return false;
149158
}
150159
}
151160

152161
static boolean hasGMSLocationLibrary() {
153162
try {
154-
com.google.android.gms.location.LocationListener.class.getName();
155-
return true;
163+
return opaqueHasClass(com.google.android.gms.location.LocationListener.class);
156164
} catch (NoClassDefFoundError e) {
157165
return false;
158166
}
159167
}
160168

161169
private static boolean hasHMSAvailabilityLibrary() {
162170
try {
163-
com.huawei.hms.api.HuaweiApiAvailability.class.getName();
164-
return true;
171+
return opaqueHasClass(com.huawei.hms.api.HuaweiApiAvailability.class);
165172
} catch (NoClassDefFoundError e) {
166173
return false;
167174
}
168175
}
169176

170177
private static boolean hasHMSPushKitLibrary() {
171178
try {
172-
com.huawei.hms.aaid.HmsInstanceId.class.getName();
173-
return true;
179+
return opaqueHasClass(com.huawei.hms.aaid.HmsInstanceId.class);
174180
} catch (NoClassDefFoundError e) {
175181
return false;
176182
}
177183
}
178184

179185
private static boolean hasHMSAGConnectLibrary() {
180186
try {
181-
com.huawei.agconnect.config.AGConnectServicesConfig.class.getName();
182-
return true;
187+
return opaqueHasClass(com.huawei.agconnect.config.AGConnectServicesConfig.class);
183188
} catch (NoClassDefFoundError e) {
184189
return false;
185190
}
186191
}
187192

188193
static boolean hasHMSLocationLibrary() {
189194
try {
190-
com.huawei.hms.location.LocationCallback.class.getName();
191-
return true;
195+
return opaqueHasClass(com.huawei.hms.location.LocationCallback.class);
192196
} catch (NoClassDefFoundError e) {
193197
return false;
194198
}
@@ -653,4 +657,4 @@ static boolean shouldLogMissingAppIdError(@Nullable String appId) {
653657
static int getRandomDelay(int minDelay, int maxDelay) {
654658
return new Random().nextInt(maxDelay + 1 - minDelay) + minDelay;
655659
}
656-
}
660+
}

0 commit comments

Comments
 (0)