Skip to content

Commit

Permalink
Merge pull request #1492 from OneSignal/fix/proguard_assumenosideeffects
Browse files Browse the repository at this point in the history
Proguard -assumenosideeffects Compatibility
  • Loading branch information
jkasten2 authored Dec 13, 2021
2 parents c244915 + dd7395d commit 1953260
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions OneSignalSDK/onesignal/src/main/java/com/onesignal/OSUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationManagerCompat;
Expand Down Expand Up @@ -136,59 +138,61 @@ int initializationChecker(Context context, String oneSignalAppId) {
return subscribableStatus;
}

// The the following is done to ensure Proguard compatibility with class existent detection
// 1. Using Class instead of Strings as class renames would result incorrectly not finding the class
// 2. class.getName() is called as if no method is called then the try-catch would be removed.
// - Only an issue when using Proguard (NOT R8) and using getDefaultProguardFile('proguard-android-optimize.txt')
// Interim method that works around Proguard's overly aggressive assumenosideeffects which
// ignores keep rules.
// This is specifically designed to address Proguard removing catches for NoClassDefFoundError
// when the config has "-assumenosideeffects" with
// java.lang.Class.getName() & java.lang.Object.getClass().
// This @Keep annotation is key so this method does not get removed / inlined.
// Addresses issue https://github.com/OneSignal/OneSignal-Android-SDK/issues/1423
@Keep
private static boolean opaqueHasClass(Class<?> _class) {
return true;
}

static boolean hasFCMLibrary() {
try {
com.google.firebase.messaging.FirebaseMessaging.class.getName();
return true;
return opaqueHasClass(com.google.firebase.messaging.FirebaseMessaging.class);
} catch (NoClassDefFoundError e) {
return false;
}
}

static boolean hasGMSLocationLibrary() {
try {
com.google.android.gms.location.LocationListener.class.getName();
return true;
return opaqueHasClass(com.google.android.gms.location.LocationListener.class);
} catch (NoClassDefFoundError e) {
return false;
}
}

private static boolean hasHMSAvailabilityLibrary() {
try {
com.huawei.hms.api.HuaweiApiAvailability.class.getName();
return true;
return opaqueHasClass(com.huawei.hms.api.HuaweiApiAvailability.class);
} catch (NoClassDefFoundError e) {
return false;
}
}

private static boolean hasHMSPushKitLibrary() {
try {
com.huawei.hms.aaid.HmsInstanceId.class.getName();
return true;
return opaqueHasClass(com.huawei.hms.aaid.HmsInstanceId.class);
} catch (NoClassDefFoundError e) {
return false;
}
}

private static boolean hasHMSAGConnectLibrary() {
try {
com.huawei.agconnect.config.AGConnectServicesConfig.class.getName();
return true;
return opaqueHasClass(com.huawei.agconnect.config.AGConnectServicesConfig.class);
} catch (NoClassDefFoundError e) {
return false;
}
}

static boolean hasHMSLocationLibrary() {
try {
com.huawei.hms.location.LocationCallback.class.getName();
return true;
return opaqueHasClass(com.huawei.hms.location.LocationCallback.class);
} catch (NoClassDefFoundError e) {
return false;
}
Expand Down Expand Up @@ -653,4 +657,4 @@ static boolean shouldLogMissingAppIdError(@Nullable String appId) {
static int getRandomDelay(int minDelay, int maxDelay) {
return new Random().nextInt(maxDelay + 1 - minDelay) + minDelay;
}
}
}

0 comments on commit 1953260

Please sign in to comment.