Xamarin Forms+Prism.DryIoc下Linker配置异常,求正确配置方案
Fixing DryIoc Initialization Exception with Prism.DryIoc and Xamarin Forms Linker
I’ve run into this exact issue before when optimizing APK size with the Linker and Prism.DryIoc. The error you’re seeing happens because the Linker is stripping critical reflection-related methods and types from DryIoc that its WrappersSupport class depends on during initialization. Your current configuration only covers Prism assemblies, but we need to explicitly preserve key parts of DryIoc itself too.
Step 1: Update Your Linker Configuration
Replace your existing Linker.xml with this revised version—it adds explicit rules for DryIoc and tightens up Prism-related preservation to avoid unnecessary bloat while keeping everything functional:
<?xml version="1.0" encoding="utf-8" ?> <linker> <!-- Prism Core and Forms --> <assembly fullname="Prism"> <namespace fullname="Prism.Ioc" preserve="all" /> <namespace fullname="Prism.Modularity" preserve="all" /> <namespace fullname="Prism.Common" preserve="all" /> </assembly> <assembly fullname="Prism.Forms"> <type fullname="Prism.Common.ApplicationProvider" preserve="all" /> <type fullname="Prism.Services.PageDialogService" preserve="all" /> <type fullname="Prism.Services.DeviceService" preserve="all" /> <namespace fullname="Prism.Navigation" preserve="all" /> </assembly> <!-- Prism DryIoc Integration --> <assembly fullname="Prism.DryIoc.Forms"> <namespace fullname="Prism.DryIoc" preserve="all" /> </assembly> <!-- Critical DryIoc Components (Fixes the WrappersSupport Exception) --> <assembly fullname="DryIoc"> <type fullname="DryIoc.WrappersSupport" preserve="all" /> <type fullname="DryIoc.Registry" preserve="all" /> <namespace fullname="DryIoc" preserve="all" /> <namespace fullname="DryIoc.Reflection" preserve="all" /> </assembly> <!-- .NET Standard Dependencies --> <assembly fullname="netstandard"> <namespace fullname="System.Reflection" preserve="all" /> <namespace fullname="System.Linq.Expressions" preserve="all" /> </assembly> </linker>
Why This Works
- DryIoc Preservation: The original error stems from
DryIoc.WrappersSupportandRegistrybeing partially stripped. By preserving the entireDryIocnamespace and its reflection sub-namespace, we ensure all internal reflection logic and initialization code remains intact. - Prism Tweaks: Added
Prism.ModularityandPrism.Navigationto cover module loading and navigation logic that might get stripped otherwise—this is a common oversight in Prism app linker configurations. - System.Linq.Expressions: DryIoc relies heavily on expression trees for dependency resolution, so we need to preserve that namespace in
netstandardto prevent further runtime errors.
Additional Tips
- ProGuard Compatibility: If you’re using ProGuard alongside the Linker, add these rules to your
proguard.cfgto avoid stripping Android-specific Prism/DryIoc code:-keep class Prism.** { *; } -keep class DryIoc.** { *; } -keepattributes *Annotation*, Signature - Test with Link All Assemblies: To verify your configuration works fully, set the Linker behavior to Link All Assemblies in your Android project properties (under Android Options > Linker) during testing. This will catch any remaining stripped types or methods.
- Refine Over Time: While preserving entire namespaces works for a quick fix, you can later narrow down the rules by analyzing Linker logs to identify exactly which types/methods are needed—this helps keep your APK as small as possible.
内容的提问来源于stack exchange,提问作者Alan Jones Rios
相关产品推荐
相关产品推荐

