Skip to content
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
70 changes: 70 additions & 0 deletions UnityLibrary/Editor/IntercomAndroidPostProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <methods>;
}
-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 <init>(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 <init>();
}
-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))
Expand Down