Search Unity

How to force ASMDEF in a package to be included in the build

Discussion in 'Package Manager' started by auchaper, Jul 25, 2019.

  1. auchaper

    auchaper

    Joined:
    Jul 25, 2018
    Posts:
    7
    Hello,

    in our team, we run into some trouble using a custom package in the package manager.
    We have multiple asmdef defining every module of our package.
    But our modular architecture is resoluving some of the references at runtime using C# reflection.
    The problem is that when we build a project that is using this package, Unity does not include the ASMDEF that has not been clearly referenced into the code and then we have a build that is not able to resolve dependancies at runtime because some dll where not compiled.

    Do we have a way to tells UnityCompiler to include specific ASMDEF that are in packages into the build ?

    I don't know if i'am clear enough, but do not hesitate to ask for more information if you can help me resolving this issue. Thanks
     
  2. manu73

    manu73

    Unity Technologies

    Joined:
    Aug 22, 2014
    Posts:
    68
  3. auchaper

    auchaper

    Joined:
    Jul 25, 2018
    Posts:
    7
    Hi @manu73 ,

    Yes, i have made a lot of research on my problem.
    Everything seems defined correctly on the asmdef.

    The thing is, Unity build the according dll because i see it in "/MyProjectFolder/Library/ScriptAssemblies/
    But then, the dll is not copied on the UWP visual project. So my final application is missing a reference.

    It happens only when my package is in the Packages folder.
    When all my code is in Assets (exactly the same code) the build works perfectly and everything is copied in the UWP visual project.

    The tricky point here is that all the classes and types defined in that specific dll are not specified somewhere in the code.
    I need to use C# Reflection to implement an interface with a specific concrete type (modular approach of our architecture) so thiose types are only defined in a configuration file. At runtime, the app is trying to resolve the dependency by implementing the concrete type found in its assemblies.

    But when the code is integrated as a package, this assembly is not integrated into the final build, and then, i got a missing assemby reference.

    I understand that for optimisation, It's a good point to not include everything of the project if it's not used but i am looking for a way to bypass this behaviour and tells the compiler that even if there is no references at all, it should include this assembly for the runtime app.

    any thought about that ?
     
  4. maximeb_unity

    maximeb_unity

    Unity Technologies

    Joined:
    Mar 20, 2018
    Posts:
    555
    When you tried with your code in Assets, did you also keep the .asmdef file next to it?

    Does the .asmdef have the UWP platform properly enabled?

    Otherwise, this is likely Managed code stripping in action (see https://docs.unity3d.com/Manual/ManagedCodeStripping.html). You should read the Reflection and code stripping section of the manual. It provides guidance on how to prevent stripping of C# types that you look up through Reflection only, or how to include an Assembly regardless of references and usage (AlwaysLinkAssembly).

    I hope this helps!
     
  5. auchaper

    auchaper

    Joined:
    Jul 25, 2018
    Posts:
    7
    Ok, that was exactly what I was looking for.
    is it possible to include the link.xml file into the package ? or it as to be in the Assets folder ?
     
  6. maximeb_unity

    maximeb_unity

    Unity Technologies

    Joined:
    Mar 20, 2018
    Posts:
    555
    According to the documentation in that page, no, it has to be under the
    Assets
    directory because it's a project-wide configuration. What you should probably use is the
    Preserve
    attribute on types that need to be preserved from being stripped (which will cause other types they reference to also be preserved, etc.) In your case, assuming the types you're trying to load using reflection derive from the same base type/interface, you should probably only need to mark those with the Preserve attribute.