You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.WrappersSupport and Registry being partially stripped. By preserving the entire DryIoc namespace and its reflection sub-namespace, we ensure all internal reflection logic and initialization code remains intact.
  • Prism Tweaks: Added Prism.Modularity and Prism.Navigation to 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 netstandard to prevent further runtime errors.

Additional Tips

  1. ProGuard Compatibility: If you’re using ProGuard alongside the Linker, add these rules to your proguard.cfg to avoid stripping Android-specific Prism/DryIoc code:
    -keep class Prism.** { *; }
    -keep class DryIoc.** { *; }
    -keepattributes *Annotation*, Signature
    
  2. 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.
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:12:33