我正试着在Mac上使用伟大的库libgit2sharp。因此,我正在使用Xamarin创建一个Mac应用程序来读取Git存储库中的信息。
当我在一个普通的mono可执行文件(目标Mono / .NET4.5
)中使用这个库时,一切都很好。但是,当我创建一个Xamarin.Mac项目时,会发生以下错误,但我看不出有什么办法:
Unhandled Exception:
System.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. ---> System.DllNotFoundException: git2-a5cf255
at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_libgit2_init ()
at LibGit2Sharp.Core.NativeMethods+LibraryLifetimeObject..ctor () [0x00006] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0
at LibGit2Sharp.Core.NativeMethods..cctor () [0x00058] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0
--- End of inner exception stack trace ---
at LibGit2Sharp.Core.Proxy.git_repository_open (System.String path) [0x00008] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0
at LibGit2Sharp.Repository..ctor (System.String path, LibGit2Sharp.RepositoryOptions options, LibGit2Sharp.Repository+RepositoryRequiredParameter requiredParameter) [0x0007e] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. ---> System.DllNotFoundException: git2-a5cf255
at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_libgit2_init ()
at LibGit2Sharp.Core.NativeMethods+LibraryLifetimeObject..ctor () [0x00006] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0
at LibGit2Sharp.Core.NativeMethods..cctor () [0x00058] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0
--- End of inner exception stack trace ---
at LibGit2Sharp.Core.Proxy.git_repository_open (System.String path) [0x00008] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0
at LibGit2Sharp.Repository..ctor (System.String path, LibGit2Sharp.RepositoryOptions options, LibGit2Sharp.Repository+RepositoryRequiredParameter requiredParameter) [0x0007e] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0
在搜索时,我发现了许多主题,结尾是“在您的机器上构建libgit2sharp以获得正确的.dylib文件”。这在过去可能是个问题,但现在不是了,所以这对我没有帮助。如前所述,我可以很容易地将libgit2sharp与mono可执行文件一起使用。
顺便说一下,我不能使用Nuget包"libgit2sharp“,因为它说Xamarin.Mac-target是不兼容的。我使用的是"libgit2sharp.portable“包,它不会在安装或编译时抛出错误。只是运行时从上面发出的错误消息。
我的示例应用程序可以在这里下载:https://files.fm/u/7eq55jjq
有什么建议可以绕过这件事吗?谢谢!
更新错误可能提示指向dylib的路径是错误的(“未找到DLL”),但这不是真的。文件正是它应该在的位置。
发布于 2017-04-14 12:29:42
天啊,我差点就到了。Philippe也在那里:确保将dylib文件放置到输出目录中的文件夹(就像Nuget包自动完成的那样)是不正确的,这将是/lib/osx/libgit2-1196807.dylib
。
相反,我发现dylib文件必须放在*.app文件中,但是没有手动执行,而是为Xamarin.Mac项目提供了一个叫做“本机引用”的东西。
我早该看看这个“原住民推荐信”。对不起,伙计们,也许这能帮上忙。
发布于 2017-04-13 14:53:39
我非常肯定,问题的根源在于独立nuget包“Libgit2sharp.NativeBinaries”中的本地库没有被复制到输出文件夹中(应该在子目录‘lib’中)。
该副本通常由目标在项目构建期间完成。
https://stackoverflow.com/questions/43402509
复制