c# - 在 Xamarin.Android 使用的 NuGet 包中包含 native 库

标签 c# android xamarin xamarin.android nuget

我有一个分为两部分的项目。第一部分是一组包装 native .dll 或 .so 库的 C# 绑定(bind)。这会生成一个 NugGet 包,然后由其他项目使用。

另一个是使用这些 C# 绑定(bind)的 Xamarin Forms 项目。我的 Xamarin 项目的 UWP 部分可以毫无问题地在 NuGet 包中找到 native 库。然而,Android 项目无法找到它。事实上,打开 APK 会发现 native .so 文件永远不会进入。

我正在使用 NuGet's mechanism for including arch-specific libraries ,通过将每个特定于平台的 .dll-or-.so 包含在 /runtimes/platform-arch/native/ 文件夹中。对于绑定(bind)项目,我的文件夹结构如下所示:

Folder structure of bindings project

...我可以确认文件夹结构已正确保留在生成的 .nupkg 文件中,包的根目录下有一个 runtimes 文件夹,并且它下面的一系列子文件夹。

我使用新的内置 .csproj 机制生成 NuGet 包,没有任何 .targets 文件。我的 NuGet 项目的 csproj 如下所示:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <IncludeSymbols>true</IncludeSymbols>
    <SymbolPackageFormat>snupkg</SymbolPackageFormat>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <Version>1.0.5.1</Version>
    <Company />
    <Product />
  </PropertyGroup>

  <ItemGroup>
    <Content Include="runtimes\**" PackagePath="runtimes">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="OneOf" Version="2.1.150" />
  </ItemGroup>

</Project>

我的项目的第二部分是 Xamarin Forms 项目,我将 NuGet 包导入到项目的 .NET Standard 跨平台部分。

当我构建 UWP 项目时,我的平台特定 .dll 被正确复制到我的 .appx 文件的根目录,我可以成功地 P/Invoke 它. (我注意到这只有在我构建 x64 配置时才有效,并且我正在 x64 机器上编译。)但是,在构建 Android 项目时,.so Android 不会发生这样的事情图书馆。

整个依赖链看起来像这样:

-------------NuGet Package----------
|   - native_lib.so                |
|   - (other runtime dlls, etc)    |
------------------------------------
                 ^
                 |                 
-----Xamarin.Forms Core Project-----
| - Imports NuGet                  |
------------------------------------
                  ^
                  |
-------Xamarin Android Project------
| - Imports Xamarin Forms Core Proj|
| - Produces APK                   |
------------------------------------

这似乎不是命名问题——尝试将绑定(bind)项目中的 native_lib.so 重命名为 libnative_lib.so 没有任何效果。

我是不是漏掉了一步,或者 NuGet 根本不明白 Xamarin.Android 项目需要将 native 库复制到 APK 中?

NuGet 项目的源代码:https://github.com/pingzing/scannitsharp 使用 NuGet 的 Xamarin Forms 项目的源代码:https://github.com/pingzing/scannit NuGet 包:https://www.nuget.org/packages/ScannitSharp/1.0.5.1

最佳答案

一般来说,您需要在您的Q中分享更多信息,例如您的代码和打包文件。

does NuGet simply not understand that a Xamarin.Android project needs native libraries copied into the APK?

是的。您需要以 MSBuild 将此文件包含在最终 APK 中的方式打包您的 NuGet。

对于 Android,这是通过 AndroidNativeLibrary 完成的

参见 https://learn.microsoft.com/en-us/xamarin/android/deploy-test/building-apps/build-process#item-attribute-name

这里有一个工作示例 https://github.com/mfkl/libvlc-nuget/blob/master/build/VideoLAN.LibVLC.Android.targets

I have noticed that this only works if I build in an x64 configuration, and I am compiling on an x64 machine.

您可能需要修改目标文件以处理 3 种可能的情况 AnyCPUx86x64。您的目标应该在实际构建之前运行,使用 BeforeTargets="BeforeBuild"

你可以使用类似的东西

<Content Include="@(WindowsX64IncludeFilesFullPath)">
        <Link>$(WindowsX64TargetDir)\$([MSBuild]::MakeRelative($(MSBuildThisFileDirectory)..\build\x64\, %(FullPath)))</Link>
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

检查一下:https://github.com/mfkl/libvlc-nuget/blob/master/build/VideoLAN.LibVLC.Windows.targets

关于c# - 在 Xamarin.Android 使用的 NuGet 包中包含 native 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58891684/

相关文章:

c# - 如何通过 float 表示形式将 float 转换为 int?

接收 cpp/cli 对象的 C# 类构造函数导致错误 'not supported by the language'

c# - 使用 "LibTiff.Net"创建 Geotiff,并添加地理信息

java - 运行任何新的 Android 项目时出现问题 - 未找到默认 Activity/无效的 Java 包

java - 适配器不更新和更改 recyclerview,notifyDataSetChanged 不工作

android - 在 Android 中启动 AlarmManager (C#)

c# - 使用 Xamarin.iOS 访问 iOS 8 中的 tmp 目录

c# - 如何在C#中实现C++样式联盟?

具有固定后缀的 Android EditText

c# - 绑定(bind)到 getter 属性时,Xamarin IsVisible 不会动态更新