繁体   English   中英

在 NuGet package 中包含本机库,由 Xamarin.ZE84E30B9390CDB646DB6DB2C9ABZ78 使用

[英]Include native library in NuGet package consumed by Xamarin.Android

我有一个两部分的项目。 第一部分是一组 C# 绑定,它们包装了 native.dll or.so 库。 这会生成一个 NugGet package,然后由另一个项目使用。

另一个是使用那些 C# 绑定的 Xamarin Forms 项目。 我的 Xamarin 项目的 UWP 部分在 NuGet ZEFE90A8E604A7C840D788D03 中找到本机库没有问题。 但是,Android 项目找不到它。 事实上,打开 APK 会发现 native.so 文件永远不会进入。

我正在使用NuGet 的机制来包含特定于架构的库,方法是将每个特定于平台的.dll-or-.so包含在/runtimes/platform-arch/native/文件夹中。 对于绑定项目,我的文件夹结构如下所示:

绑定项目的文件夹结构

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

我使用新的内置.csproj 机制生成 NuGet package,没有任何.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>

The second part of my project is a Xamarin Forms project, and I import the NuGet package to the .NET Standard cross-platform portion of the project.

当我构建 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                   |
------------------------------------

这似乎不是命名问题——尝试将绑定项目中的libnative_lib.so native_lib.so

我错过了一步,还是 NuGet 根本不明白 Xamarin.Android 项目需要将本机库复制到 APK 中?

Source code for the NuGet project: https://github.com/pingzing/scannitsharp Source code for the Xamarin Forms project that consumes the NuGet: https://github.com/pingzing/scannit NuGet package: https://www. nuget.org/packages/ScannitSharp/1.0.5.1

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

NuGet 根本不明白 Xamarin.Android 项目需要将本机库复制到 APK 中吗?

是的,它确实。 您需要 package 您的 NuGet 以 MSBuild 将此文件包含在最终 APK 中的方式。

对于 Android,这是使用AndroidNativeLibrary完成的

请参阅https://docs.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

我注意到这只有在我构建 x64 配置时才有效,并且我正在 x64 机器上编译。

您可能需要修改目标文件以处理 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM