【问题标题】:Copy files to Android project on NuGet installation在 NuGet 安装时将文件复制到 Android 项目
【发布时间】:2018-11-27 07:35:38
【问题描述】:

期望的输出

我们希望通过 NuGet 包安装分发 .dll(NetStandard 项目)和一些文件。在Xamarin.Android 项目中安装它时:

  • 一个文件 (Directory.Buil.props) 被复制到解决方案文件夹中
  • 一个可执行文件 (config.exe) 被复制到项目文件夹中
  • 目录 (Files) 及其内容复制到项目文件夹中

问题

  • 使用PackageReference 的项目将无法复制文件(不支持content)
  • 由于某种原因,当使用.nuspec 文件时;源文件,obj,bin 等也打包了

解决方案

理想情况下,我们希望:

  • 仅使用.csproj 文件(不使用.nuspec)
  • content 和 contentFiles 不能同时包含在 .nupkg 中
  • 从.csproj 轻松访问.dll
  • 安装较新的.nupkg 版本时,旧文件将被覆盖

问题

(1) 这对 PackageReference 和 contentFiles 是否可行?

(2) 您能想到的最佳方法是什么?

谢谢。

回复

狮子座:

在 Android 项目中安装包时,文件不会出现在项目中。更不用说这些文件只是引用而不是复制(即使我有copyToOutput="true"):

狮子座(编辑):

我无法使用新的 SDK csproj 格式。摘自您的链接:

免责声明:这只适用于一小部分项目类型。

  • 类库项目
  • 控制台应用
  • ASP.NET Core Web 应用程序
  • .NET 核心

如果您正在构建 ASP.NET 4(即不是 ASP.NET Core)、WPF、通用 Windows 或 Xamarin 项目,则必须坚持使用旧格式

【问题讨论】:

    标签: c# xamarin xamarin.android visual-studio-2017 nuget


    【解决方案1】:

    (1) 这对 PackageReference 和 contentFiles 可行吗?

    恐怕您无法将这些文件添加到 Android 项目中,但我想在这里提供一个替代解决方案,将这些文件添加到输出文件夹。

    您可以直接使用PackageReference 和contentFiles 来满足后两个要求,config.exe 和Files。但是对于第一个需求Directory.Buil.props,我们需要做更多的事情,因为它被复制到solution文件夹而不是project文件夹。

    对于后两个要求,config.exe和Files,我们可以使用.nuspec文件和contentFiles来包含它们,需要设置copyToOutput="true",例如:

    <?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
      <metadata>
        <id>MyTestCore</id>
        <version>4.0.0</version>
        <authors>TestContentFile</authors>
        <owners>TestContentFile</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>Package Description</description>
        <contentFiles>
          <files include="any/any/config.exe" buildAction="content" flatten="true" copyToOutput="true"/>
          <files include="any/any/Files/1.txt" buildAction="content" flatten="true" copyToOutput="true"/>
          <files include="any/any/Files/2.txt" buildAction="content" flatten="true" copyToOutput="true"/>
        </contentFiles>
      </metadata>
    
      <files>
        <file src="contentFiles/any/any/config.exe" target="contentFiles/any/any/config.exe" />
        <file src="contentFiles/any/any/Files/1.txt" target="contentFiles/any/any/Files" />
        <file src="contentFiles/any/any/Files/2.txt" target="contentFiles/any/any/Files" />
      </files>
    </package>
    

    打包这个.nuspec 并将生成的包安装到项目中。

    但是,我们无法在参考节点下找到这些文件。那是因为该项目仍然使用带有packagereference 的旧csproj,而不是使用新的sdk csproj。

    Old csproj to new csproj: Visual Studio 2017 upgrade guide

    此外,不支持将文件复制到项目的源目录中,并且不鼓励经典项目的做法。 contentFiles 部分控制为这些文件生成到 obj\projectname.csproj.nuget.g.props 文件中的 msbuild 项。并检查您可以找到的project.assets.json 文件:

      "targets": {
        "MonoAndroid,Version=v7.1": {
          "MyTestCore/5.0.0": {
            "type": "package",
            "contentFiles": {
              "contentFiles/any/any/Files/1.txt": {
                "buildAction": "Content",
                "codeLanguage": "any",
                "copyToOutput": true,
                "outputPath": "1.txt"
              },
              "contentFiles/any/any/Files/2.txt": {
                "buildAction": "Content",
                "codeLanguage": "any",
                "copyToOutput": true,
                "outputPath": "2.txt"
              },
              "contentFiles/any/any/config.exe": {
                "buildAction": "Content",
                "codeLanguage": "any",
                "copyToOutput": true,
                "outputPath": "config.exe"
              }
            }
    

    见:nuspec contentFiles not added to a project

    然后我们需要构建这个项目,这些文件将复制到输出文件夹。

    对于第一个要求,为了将Directory.Buil.props添加到解决方案文件夹,我们需要在YourPackageName.targets文件中创建一个自定义复制目标,然后将此.targets文件添加到\build 文件夹,.targets 文件如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
        <ItemGroup>  
            <MySourceFiles Include="<FilePath>\Directory.Buil.props"/>  
        </ItemGroup>  
    
        <Target Name="CopyFiles" BeforeTargets="Build">  
            <Copy  
                SourceFiles="@(MySourceFiles)"  
                DestinationFolder="<SolutionFolder>"  
            />  
        </Target>  
    
    </Project>
    

    .nuspec 文件如:

      <files>
        <file src="<>\xxx.targets" target="build" />
      </files>
    

    希望这会有所帮助。

    【讨论】:

    • 这不适用于 Android 项目。我在结果中添加了一个编辑。
    • @JonZarate,那是我的错。我忘了用 Android 项目测试它,只用PackageReference 测试它。我已经用更多信息更新了我的答案,您可以查看 然而... 部分以了解详细信息。希望这可以提供一些帮助:)。
    • 我添加了另一个回复。您在发布之前是否对此进行了测试?
    • @JonZarate,是的,我已经测试过了。你有没有看到“我们在参考节点下找不到那些文件”这个词。和“这些文件将复制到输出文件夹。”?是否要将这些文件添加到“参考”节点下的项目中?如果是,恐怕我无法提供其他有用的帮助:(。
    • 我想我错过了一些步骤。你能上传解决方案并链接它吗?
    猜你喜欢
    • 2021-06-16
    • 1970-01-01
    • 2021-04-16
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多