Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proguard -assumenosideeffects Compatibility #1492

Merged
merged 2 commits into from
Dec 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}