代码之家  ›  专栏  ›  技术社区  ›  JonZarate

在安装NuGet时将文件复制到Android项目

  •  6
  • JonZarate  · 技术社区  · 6 年前

    期望输出

    我们想分发一个 .dll (NetStandard项目)和一些通过NuGet包安装的文件。当安装到 Xamarin.Android 项目:

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

    问题

    • PackageReference 无法复制文件( content 不支持)
    • .nuspec 文件;源文件, obj bin 等也打包了

    解决方案

    理想情况下,我们希望:

    • .csproj 文件(不带 努斯佩克
    • 两者都没有 contentFiles 包装在 .nupkg
    • .dll文件
    • 安装较新的 版本,旧文件将被覆盖

    问题

    (一) 这是可行的吗 包引用 内容文件 ?

    你能想到的最好的方法是什么?

    谢谢。

    响应

    狮子座

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

    Solution projects after NuGet package installation

    狮子座

    我不能使用新的SDK csproj格式。摘自你的链接:

    免责声明

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

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

    1 回复  |  直到 6 年前
        1
  •  0
  •   Leo Liu    6 年前

    (1) 这在PackageReference和contentFiles中是可行的吗?

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

    你可以用 PackageReference contentFiles 直接针对后两个需求, config.exe Files Directory.Buil.props ,我们需要做更多的事情,因为它被复制到 解决方案 文件夹而不是 文件夹。

    对于后两项要求 配置文件 文件夹 ,我们可以使用 .nuspec 内容文件 包括他们,需要设置 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>
    

    包装好后

    然而 ,在“引用”节点下找不到这些文件。因为项目仍然使用旧的csproj packagereference

    Old csproj to new csproj: Visual Studio 2017 upgrade guide

    此外,不支持将文件复制到项目的源目录中,对于经典项目来说,这是一种不可取的做法。这个 节控制在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

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

    对于第一个要求 ,以便添加 目录.Buil.props 对于解决方案文件夹,我们需要在 YourPackageName.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>