Search Unity

External library method doesn't work with IL2CPP backend

Discussion in 'iOS and tvOS' started by Alexis-Dev, Aug 15, 2019.

  1. Alexis-Dev

    Alexis-Dev

    Joined:
    Apr 16, 2019
    Posts:
    121
    Hello,

    I have a problem when I call a method form an external library with IL2CPP backend.
    If I build with Mono backend, everything works fine.
    I don't understand how to fix this issue, so I calling for help! :)

    Some details:
    - I use the library PDFSharp (here the nuget package)
    - Code exemple:
    Code (CSharp):
    1. string path =  "Devis_Client_Vierge.pdf";
    2. string fileName = "Devis-Test.pdf";
    3.          
    4. PdfDocument document = PdfReader.Open(path, PdfDocumentOpenMode.Modify);
    5. PdfAcroForm form = document.AcroForm;
    6.  
    7. document.Save(fileName);
    8. document.Close();
    - The exception:
    I hope someone can help me or have some advice.
    Best regards,

    Alexis
     
    BAIZOR likes this.
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    I'm not too familiar with this library, but in general this should work.

    If you are including the PDFSHarp code as a separate .dll in your project (a managed plug-in), I would recommend disabling managed code stripping for it. See https://docs.unity3d.com/Manual/IL2CPP-BytecodeStripping.html for details.

    Many library don't work well with managed code stripping. Managed code stripping is enable for IL2CPP, but disabled for Mono by default. So that might account for the difference in behavior.
     
    Alexis-Dev likes this.
  3. Alexis-Dev

    Alexis-Dev

    Joined:
    Apr 16, 2019
    Posts:
    121
    Hello,

    Thanks! You save me work! :)

    I have other issue, but it's an other problem.

    Best regards,
    Alexis
     
  4. Alexis-Dev

    Alexis-Dev

    Joined:
    Apr 16, 2019
    Posts:
    121
    Hello again,

    Other problem with IL2CPP and the library System.Drawing.

    I have including System.Drawing.dll in my project and disable the stripping. This time, an MarshalDirectiveException appear... :'(

    It's is an IL2CPP bug or problem with library?

    Best regards,
    Alexis
     
  5. Alexis-Dev

    Alexis-Dev

    Joined:
    Apr 16, 2019
    Posts:
    121
  6. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    I don't believe that this is the same as that issue you linked to.

    In general, I would avoid using System.Drawing.dll. It depends on OS-specific code, and Unity does not support it. As for the specific issue, I suspect that it is a problem with System.Drawing.dll, but if you would like to submit a bug report, we can take a closer look.
     
  7. Alexis-Dev

    Alexis-Dev

    Joined:
    Apr 16, 2019
    Posts:
    121
    Hello,

    I found a solution! I didn't use PDFSharp but PDFSharpCore (Combine with an assembly file because the library break TextMeshPro Package...).

    The problem with PDFSharp is System.Drawing.dll like you said! :(

    Thank you for your time and your help!

    PS: If someone have problem to use PDFSharpCore in Unity, send me a PM.
     
  8. bhupiister

    bhupiister

    Joined:
    Dec 13, 2019
    Posts:
    42
    @Alexis-Dev Can you help me run PDFSharpCore on unity?
     
  9. Alexis-Dev

    Alexis-Dev

    Joined:
    Apr 16, 2019
    Posts:
    121
    Hello,

    Send me a private message if you need me help.

    Best,
    Alexis
     
  10. BAIZOR

    BAIZOR

    Joined:
    Jul 4, 2013
    Posts:
    112
    @JoshPeterson how to do that? That option is unavailable when IL2CPP chosen. Appears only if Mono chosen.

    upload_2021-12-16_4-36-28.png
     
  11. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    BAIZOR likes this.
  12. makomarkus

    makomarkus

    Joined:
    Oct 28, 2020
    Posts:
    61
    @JoshPeterson Is there a way to create a log of what managed code stripping stripped? This would help with projects that rely on reflection and json deserialization/serialization etc.
     
  13. mike-voorhees

    mike-voorhees

    Unity Technologies

    Joined:
    Aug 9, 2016
    Posts:
    46
    We don't have anything officially documented. But there are some crude diagnostics that can be turned on.

    I mentioned how to turn on diagnostics in https://forum.unity.com/threads/il2...tal-player-build-failed.1249945/#post-8423187 see the part about "--enable-report --enable-snapshot"

    "--enable-report" will turn on the diagnostics. "--enable-snapshot" makes the files a little easier to find.

    Here are some tips that sound like they may help you.

    1) When "--enable-report" is used, the Editor log will contain warnings about reflection usages that the linker could not detect.

    Here is an example of what appears in the Editor log when I toss some reflection into a script.


    ILLink: Trim analysis warning IL5999: .test.Start(): Unhandled reflection usage in `System.Void test::Start()` : IL_000b: callvirt System.Reflection.MethodInfo System.Type::GetMethod(System.String)
    ILLink: Trim analysis warning IL2075: .test.Start(): The requirements declared via the 'DynamicallyAccessedMembersAttribute' on the return value of method 'System.Object.GetType()' don't match those on the implicit 'this' parameter of method 'System.Type.GetMethod(String)'. The source value must declare at least the same requirements as those declared on the target location it's assigned to
    ILLink: warning IL5999: .test.Start(): Unhandled reflection usage in `System.Void test::Start()` : IL_000b: callvirt System.Reflection.MethodInfo System.Type::GetMethod(System.String)


    This information is also output to files. In your project directory, search for a directory named "UnityLinker_Diagnostics". In there "Messages.log" and "ReflectionUsages.log" also contain information about reflection.

    If you used "--enable-snapshot" then these files will also appear under the snapshot directory at "%TEMP%\UnityLinker\Snapshots\<snapshot dir>\output\UnityLinker_Diagnostics"

    2) Another file that you might find useful is "SweepReport.log". This is a log of everything that was removed. It will be in the "UnityLinker_Diagnostics" directory alongside the files above.

    These diagnostics are intended for internal use. They are a little rough around the edges and could change at any time. That said, they have been fairly stable since their introduction many years ago.

    Hope that helps. If you have any questions let me know.
     
    makomarkus likes this.