From 5ecb5865124b9fd0ee821e349625eab2c5e689d9 Mon Sep 17 00:00:00 2001 From: Dheepakraj Selvakumar Date: Fri, 4 Dec 2026 11:15:05 +0000 Subject: [PATCH] fix(android): keep the by-name androidx members when the app minifies IntercomPlugin.initialize() installs the ViewTree lifecycle, saved-state and ViewModelStore owners on the Unity activity's decor view through reflection and Proxy dispatch on interface method names. When the app minifies with R8, the Class.forName strings are rewritten but the methods are renamed, so the setup throws and logs "Could not set view tree owners", and in-app messages built with Compose cannot render. The Android post-processor now appends exact keep rules for those members to unityLibrary's proguard-unity.txt, which the generated build already lists in consumerProguardFiles. The block also keeps IntercomPlugin's public static entry points and HeaderInterceptor.setUnityVersion, both reached only by name, so the wrapper no longer depends on the native SDK shipping keep rules for them. Co-Authored-By: Claude Opus 5.5 --- .../Editor/IntercomAndroidPostProcessor.cs | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) 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))