diff --git a/UnityLibrary/Editor/IntercomAndroidPostProcessor.cs b/UnityLibrary/Editor/IntercomAndroidPostProcessor.cs index 7e5e067..f7db752 100644 --- a/UnityLibrary/Editor/IntercomAndroidPostProcessor.cs +++ b/UnityLibrary/Editor/IntercomAndroidPostProcessor.cs @@ -14,6 +14,76 @@ public class IntercomAndroidPostProcessor : IPostGenerateGradleAndroidProject public int callbackOrder => 100; public void OnPostGenerateGradleAndroidProject(string path) + { + AppendKeepRules(path); + ConfigureRegion(path); + } + + private const string KeepRulesMarker = "# Intercom Unity wrapper keep rules"; + + // IntercomPlugin and its androidx calls are reached only by name (C# AndroidJavaClass, + // Class.forName/getMethod, and Proxy dispatch on interface method names), so R8 renames + // them when the host app minifies. + private const string KeepRules = KeepRulesMarker + @" +-keep class com.intercom.unity.IntercomPlugin { + public static ; +} +-keep class io.intercom.android.sdk.api.HeaderInterceptor { + public static void setUnityVersion(android.content.Context, java.lang.String); +} +-keep class androidx.lifecycle.ViewTreeLifecycleOwner { + public static *** get(android.view.View); + public static void set(android.view.View, androidx.lifecycle.LifecycleOwner); +} +-keep interface androidx.lifecycle.LifecycleOwner { + *** getLifecycle(); +} +-keep class androidx.lifecycle.LifecycleRegistry { + public (androidx.lifecycle.LifecycleOwner); + public void handleLifecycleEvent(androidx.lifecycle.Lifecycle$Event); +} +-keep enum androidx.lifecycle.Lifecycle$Event { + public static **[] values(); +} +-keep interface androidx.savedstate.SavedStateRegistryOwner { + *** getSavedStateRegistry(); +} +-keep class androidx.savedstate.SavedStateRegistryController { + public static *** create(androidx.savedstate.SavedStateRegistryOwner); + public void performAttach(); + public void performRestore(android.os.Bundle); + public *** getSavedStateRegistry(); +} +-keep class androidx.savedstate.ViewTreeSavedStateRegistryOwner { + public static void set(android.view.View, androidx.savedstate.SavedStateRegistryOwner); +} +-keep interface androidx.lifecycle.ViewModelStoreOwner { + *** getViewModelStore(); +} +-keep class androidx.lifecycle.ViewModelStore { + public (); +} +-keep class androidx.lifecycle.ViewTreeViewModelStoreOwner { + public static void set(android.view.View, androidx.lifecycle.ViewModelStoreOwner); +} +"; + + // unityLibrary's build.gradle lists proguard-unity.txt in consumerProguardFiles, so the + // rules reach the launcher's R8 pass without editing any Gradle file. + private static void AppendKeepRules(string path) + { + string proguardPath = Path.Combine(path, "proguard-unity.txt"); + string existing = File.Exists(proguardPath) ? File.ReadAllText(proguardPath) : string.Empty; + if (existing.Contains(KeepRulesMarker)) + { + return; + } + + string separator = existing.Length == 0 || existing.EndsWith("\n") ? string.Empty : "\n"; + File.WriteAllText(proguardPath, existing + separator + KeepRules); + } + + private static void ConfigureRegion(string path) { string manifestPath = Path.Combine(path, "src", "main", "AndroidManifest.xml"); if (!File.Exists(manifestPath))