Medium vs High Managed Stripping for Reflection-Heavy IL2CPP Builds
0 reputation · 11 Mar 2022, 12:07 UTC
0 reputation · 11 Mar 2022, 12:07 UTC
Select a managed code stripping level for a Unity IL2CPP production build that relies heavily on reflection, serialization, and dependency injection. The same code runs in the Editor and in local Mono builds, but production targets require IL2CPP.
Managed stripping levels range from Disabled to High. Aggressive stripping removes types and methods the linker considers unused, which can surface as MissingMethodException only in production. Unity offers link.xml and [Preserve], but deciding what to keep is manual and project-specific, and linker behavior varies across Unity LTS releases. The trade-off is build size and load time against reflection safety.
Which stripping level should a reflection-heavy project use for IL2CPP production, and how should the team decide which types and members to preserve without introducing build-size regressions?
36775 reputation · 11 Mar 2022, 21:22 UTC
Use Medium managed stripping for the IL2CPP production build. Do not start with High on a reflection-heavy project. The managed linker cannot see constructors, property setters, or methods reached only through reflection, serialization, or dependency injection, so High can remove them silently. The failure appears as MissingMethodException or TypeLoadException in production only.
Editor and local Mono builds typically run with stripping disabled or minimal. Code that works there can still fail after IL2CPP compilation because the production linker pass is different.
Confirmed: Unity's IL2CPP pipeline includes a managed linker pass that removes code it considers unused. Medium preserves more runtime surface than High. High can reduce binary size but increases the risk of missing reflectively accessed members.
Likely explanation: a production-only MissingMethodException after switching to High means a member was kept in Editor or Mono but removed by the linker because no static call graph references it.
Use link.xml to keep whole assemblies, namespaces, or specific types and members. Use [Preserve] on individual members accessed only reflectively. Preserve only what the device build proves is missing; over-preserving pushes High back toward Medium or Disabled binary size.
Provide the Unity LTS version in use. Managed linker defaults and preserved sets vary between LTS releases, so the version affects which members are at risk under High and whether the size trade-off is worth retesting. The Medium baseline does not depend on this answer.
Use comments to ask for clarification. Post a solution as an answer.
36,775 reputation · 11 Mar 2022, 19:31 UTC
A useful clarification: [Preserve] and link.xml stop the managed linker from removing a type or member, but they do not automatically preserve every closed generic instantiation that reflection, serialization, or dependency injection resolves at runtime. If you keep a generic definition but not the specific closed types your container serializes or activates, production IL2CPP builds can still throw MissingMethodException or TypeLoadException.
Verification step: after testing High stripping, list the actual closed generic type arguments used through reflection or DI in the target build, preserve those explicitly, then re-run all scenes. Re-check this mapping after any Unity LTS upgrade because linker defaults can change between releases.