Trying to use an external library in my Android project, but managed code stripping causes an error.
Hi, I'm trying to use the SSH.Net library in my Android project, however I'm running into an error. Specifially, MissingMethodException: Default constructor not found for type Renci.SshNet.Security.KeyExchangeDiffieHellmanGroupExchangeSha256
.
I've determined the problem is to do with 'Managed Code Stripping' in project settings, because if I switch to Mono and disable the option, the library works fine. I've read about link.xml files to disable stripping for particular code, however the contents I have at the minute do not help and the error persists:
<linker>
<assembly fullname="Renci.*" preserve="all">
<namespace fullname="Renci.SshNet" preserve="all"/>
<namespace fullname="Renci.SshNet.*" preserve="all"/>
<type fullname="Renci.SshNet" preserve="all"/>
<type fullname="Renci.SshNet.*" preserve="all"/>
</assembly>
<assembly fullname="Renci.SshNet.*" preserve="all"/>
<assembly fullname="Renci.SshNet.Sftp" preserve="all"/>
<assembly fullname="Renci.SshNet.Messages" preserve="all"/>
<assembly fullname="Renci.SshNet.Compression" preserve="all"/>
<assembly fullname="Renci.SshNet.Security.*" preserve="all">
<namespace fullname="Renci.SshNet.Security" preserve="all"/>
<namespace fullname="Renci.SshNet.Security.*" preserve="all"/>
<type fullname="Renci.SshNet.Security" preserve="all"/>
<type fullname="Renci.SshNet.Security.*" preserve="all"/>
<type fullname="Renci.SshNet.Security.KeyExchangeDiffieHellmanGroupExchangeSha256" preserve="all"/>
</assembly>
</linker>
(you may be able to tell I just wrote as many random lines as I could hoping it would fix the problem)
So my question is, what would a link.xml file need to contain to stop the SSH.Net library code from being stripped? And unfortunately, I cannot use Mono as it does not offer arm64 builds.
Edit: I've also found out by googling the error code (-2146233069), that the class is accessed dynamically so I believe this requires the link.xml file to change (reading the documenttion.)
Thanks.
Answer by JoshPeterson · Sep 11, 2019 at 11:57 AM
Try to use the assembly element with the preserve="all" attribute for each managed assembly by full name without wildcards. That should prevent any of the assemblies listed from being stripped, as if managed code stripping is disabled for this assemblies. There is no need to preserve specific name spaces or types within those assemblies then.
Alright, I'll try that now. However, I've just found out that the constructor is called dynamically, which reading the documentation, seems to change how the link.xml should look.
Answer by gbk_ · Jun 08, 2020 at 03:20 PM
Found solution -, thanks to @JoshPeterson
link.xml works fine, but without astercs
<?xml version="1.0" encoding="UTF-8"?>
<linker>
<assembly fullname="Renci" preserve="all">
<namespace fullname="Renci.SshNet" preserve="all" />
<type fullname="Renci.SshNet" preserve="all" />
</assembly>
<assembly fullname="Renci.SshNet" preserve="all" />
<assembly fullname="Renci.SshNet.Sftp" preserve="all" />
<assembly fullname="Renci.SshNet.Messages" preserve="all" />
<assembly fullname="Renci.SshNet.Compression" preserve="all" />
<assembly fullname="Renci.SshNet.Security" preserve="all">
<namespace fullname="Renci.SshNet.Security" preserve="all" />
<type fullname="Renci.SshNet.Security" preserve="all" />
<type fullname="Renci.SshNet.Security.KeyExchangeDiffieHellmanGroupExchangeSha256" preserve="all" />
</assembly>
</linker>
That's good to know! I ended up not using the library because using an SSH library for a level browser was a dumb idea and switched to using a PHP web server ins$$anonymous$$d, which didn't require any external libraries.
Answer by masterchop · Jul 21, 2021 at 12:51 AM
I tried the XML on MagicLeap but the errror persist. how to fix it please i would really appreciate the help.
Your answer
Follow this Question
Related Questions
il2cpp.exe didn't catch exception: System.IO.IOException: Access to the path '{path}' is denied. 0 Answers
IL2CPP Android build with custom static libraries linked with GNU STL static library 0 Answers
Adding .NET STANDARD DLL to unity 0 Answers
How to fix (Gamename) has stopped error? Here is the logcat file. 1 Answer