다음을 통해 공유


Visual Studio 2017 또는 2019를 사용하여 Xamarin용 패키지 만들기

Xamarin용 패키지에는 런타임 운영 체제에 따라 iOS, Android 및 Windows에서 네이티브 API를 사용하는 코드가 포함되어 있습니다. 이 작업은 간단하지만 개발자가 공통 API 노출 영역을 통해 PCL 또는 .NET Standard 라이브러리에서 패키지를 사용할 수 있게 하는 것이 좋습니다.

이 연습에서는 Visual Studio 2017 또는 2019를 사용하여 iOS, Android 및 Windows의 모바일 프로젝트에 사용할 수 있는 플랫폼 간 NuGet 패키지를 만듭니다.

  1. 필수 구성 요소
  2. 프로젝트 구조 및 추상화 코드 만들기
  3. 플랫폼 특정 코드 작성
  4. .nuspec 파일 만들기 및 업데이트
  5. 구성 요소 패키징
  6. 관련 항목

필수 조건

  1. UWP(유니버설 Windows 플랫폼) 및 Xamarin이 있는 Visual Studio 2017 또는 2019입니다. visualstudio.com에서 추가 비용 없이 Community 버전을 설치합니다. Professional 및 Enterprise 버전도 사용할 수 있습니다. UWP 및 Xamarin 도구를 포함하려면 사용자 지정 설치를 선택하고 적절한 옵션을 선택합니다.
  2. NuGet CLI - nuget.org/downloads에서 최신 버전의 nuget.exe를 다운로드하여 원하는 위치에 저장합니다. 그런 다음 해당 위치를 PATH 환경 변수에 추가합니다(아직 없는 경우).

참고

nuget.exe는 설치 관리자가 아니라 CLI 도구 자체이므로 브라우저에서 다운로드한 파일을 실행하는 대신 저장해야 합니다.

프로젝트 구조 및 추상화 코드 만들기

  1. Visual Studio용 플랫폼 간 .NET 표준 플러그 인 템플릿 확장을 다운로드하여 실행합니다. 이러한 템플릿을 사용하면 이 연습에 필요한 프로젝트 구조를 쉽게 만들 수 있습니다.

  2. Visual Studio 2017에서 새 > 파일 > Project 검색하고Plugin, 플랫폼 간 .NET 표준 라이브러리 플러그 인 템플릿을 선택하고, 이름을 LoggingLibrary로 변경하고, 확인을 클릭합니다.

    New Blank App (Xamarin.Forms Portable) project in VS 2017

    Visual Studio 2019에서 새 > 파일 > Project 검색하고 Plugin플랫폼 간 .NET 표준 라이브러리 플러그 인 템플릿을 선택하고 다음을 클릭합니다.

    New Blank App (Xamarin.Forms Portable) project in VS 2019

    이름을 LoggingLibrary로 변경하고 만들기를 클릭합니다.

    New Blank App (Xamarin.Forms Portable) configuration in VS 2019

결과 솔루션에는 다양한 플랫폼별 프로젝트와 함께 다음과 같은 두 개의 Shared 프로젝트가 포함되어 있습니다.

  • ILoggingLibrary.shared.cs 파일에 포함된 ILoggingLibrary 프로젝트는 구성 요소의 공용 인터페이스(API 노출 영역)를 정의합니다. 여기서는 라이브러리에 대한 인터페이스를 정의합니다.
  • 다른 Shared 프로젝트에는 런타임 시 추상 인터페이스의 플랫폼 특정 구현을 찾는 CrossLoggingLibrary.shared.cs의 코드가 포함되어 있습니다. 일반적으로 이 파일은 수정할 필요가 없습니다.
  • 플랫폼별 프로젝트(예: LoggingLibrary.android.cs각 프로젝트에는 각각 LoggingLibraryImplementation.cs VS 2017) 또는 LoggingLibrary.<PLATFORM>.cs (VS 2019) 파일에 인터페이스의 네이티브 구현이 포함됩니다. 여기서는 라이브러리 코드를 빌드합니다.

기본적으로 ILoggingLibrary 프로젝트의 ILoggingLibrary.shared.cs 파일에는 인터페이스 정의가 있지만, 메서드는 없습니다. 이 연습의 목적을 위해 다음과 같이 Log 메서드를 추가합니다.

using System;
using System.Collections.Generic;
using System.Text;

namespace Plugin.LoggingLibrary
{
    /// <summary>
    /// Interface for LoggingLibrary
    /// </summary>
    public interface ILoggingLibrary
    {
        /// <summary>
        /// Log a message
        /// </summary>
        void Log(string text);
    }
}

플랫폼 특정 코드 작성

ILoggingLibrary 인터페이스 및 해당 메서드의 플랫폼 특정 구현을 구현하려면 다음을 수행합니다.

  1. 각 플랫폼 프로젝트의 LoggingLibraryImplementation.cs(VS 2017) 또는 LoggingLibrary.<PLATFORM>.cs(VS 2019) 파일을 열고 필요한 코드를 추가합니다. 예(Android 플랫폼 프로젝트 사용):

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Plugin.LoggingLibrary
    {
        /// <summary>
        /// Implementation for Feature
        /// </summary>
        public class LoggingLibraryImplementation : ILoggingLibrary
        {
            /// <summary>
            /// Log a message
            /// </summary>
            public void Log(string text)
            {
                throw new NotImplementedException("Called Log on Android");
            }
        }
    }
    
  2. 지원하려는 각 플랫폼에 대한 프로젝트에서 이 구현을 반복합니다.

  3. 솔루션을 마우스 오른쪽 단추로 클릭하고, 솔루션 빌드를 선택하여 작업을 확인하고, 다음에 패키지할 아티팩트를 생성합니다. 누락된 참조에 대한 오류가 발생하면 솔루션을 마우스 오른쪽 단추로 클릭하고, NuGet 패키지 복원을 선택하여 종속성을 설치한 다음, 다시 빌드합니다.

참고

Visual Studio 2019를 사용하는 경우 NuGet 패키지 복원을 선택하고 다시 빌드를 시도하기 전에 LoggingLibrary.csproj에서 버전을 MSBuild.Sdk.Extras에서 2.0.54로 변경해야 합니다. 이 파일은 먼저 프로젝트를 마우스 오른쪽 단추로 클릭하고 Unload Project을(를) 선택해야만 액세스할 수 있습니다. 그런 다음 언로드된 프로젝트를 마우스 오른쪽 단추로 클릭하고 Edit LoggingLibrary.csproj을(를) 선택합니다.

참고

iOS용으로 빌드하려면 Visual Studio용 Xamarin.iOS 소개에서 설명한 대로 네트워크에서 Visual Studio에 연결된 Mac이 필요합니다. Mac을 사용할 수 없는 경우 구성 관리자에서 iOS 프로젝트를 선택 취소합니다(위 3단계).

.nuspec 파일 만들기 및 업데이트

  1. 명령 프롬프트를 열고, .sln 파일이 있는 위치에서 한 수준 아래의 LoggingLibrary 폴더로 이동한 다음, NuGet spec 명령을 실행하여 초기 Package.nuspec 파일을 만듭니다.

    nuget spec
    
  2. 이 파일의 이름을 LoggingLibrary.nuspec으로 변경하고 편집기에서 엽니다.

  3. YOUR_NAME을 적절한 값으로 바꿔 다음과 일치하도록 파일을 업데이트합니다. 특히 <id> 값은 nuget.org 전체에서 고유해야 합니다(패키지 만들기에서 설명한 명명 규칙 참조). 또한 작성자 및 설명 태그도 업데이트해야 합니다. 그렇지 않으면 압축 단계에서 오류가 발생합니다.

    <?xml version="1.0"?>
    <package >
        <metadata>
        <id>LoggingLibrary.YOUR_NAME</id>
        <version>1.0.0</version>
        <title>LoggingLibrary</title>
        <authors>YOUR_NAME</authors>
        <owners>YOUR_NAME</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>Awesome application logging utility</description>
        <releaseNotes>First release</releaseNotes>
        <copyright>Copyright 2018</copyright>
        <tags>logger logging logs</tags>
        </metadata>
    </package>
    

패키지 버전에 -alpha, -beta 또는 -rc 접미사를 붙여 패키지를 시험판으로 표시할 수 있습니다. 시험판 버전에 대한 자세한 내용은 시험판 버전을 참조하세요.

참조 어셈블리 추가

플랫폼 특정 참조 어셈블리를 포함하려면 지원되는 플랫폼에 맞게 LoggingLibrary.nuspec<files> 요소에 다음을 추가합니다.

<!-- Insert below <metadata> element -->
<files>
    <!-- Cross-platform reference assemblies -->
    <file src="Plugin.LoggingLibrary\bin\Release\Plugin.LoggingLibrary.dll" target="lib\netstandard1.4\Plugin.LoggingLibrary.dll" />
    <file src="Plugin.LoggingLibrary\bin\Release\Plugin.LoggingLibrary.xml" target="lib\netstandard1.4\Plugin.LoggingLibrary.xml" />
    <file src="Plugin.LoggingLibrary.Abstractions\bin\Release\Plugin.LoggingLibrary.Abstractions.dll" target="lib\netstandard1.4\Plugin.LoggingLibrary.Abstractions.dll" />
    <file src="Plugin.LoggingLibrary.Abstractions\bin\Release\Plugin.LoggingLibrary.Abstractions.xml" target="lib\netstandard1.4\Plugin.LoggingLibrary.Abstractions.xml" />

    <!-- iOS reference assemblies -->
    <file src="Plugin.LoggingLibrary.iOS\bin\Release\Plugin.LoggingLibrary.dll" target="lib\Xamarin.iOS10\Plugin.LoggingLibrary.dll" />
    <file src="Plugin.LoggingLibrary.iOS\bin\Release\Plugin.LoggingLibrary.xml" target="lib\Xamarin.iOS10\Plugin.LoggingLibrary.xml" />

    <!-- Android reference assemblies -->
    <file src="Plugin.LoggingLibrary.Android\bin\Release\Plugin.LoggingLibrary.dll" target="lib\MonoAndroid10\Plugin.LoggingLibrary.dll" />
    <file src="Plugin.LoggingLibrary.Android\bin\Release\Plugin.LoggingLibrary.xml" target="lib\MonoAndroid10\Plugin.LoggingLibrary.xml" />

    <!-- UWP reference assemblies -->
    <file src="Plugin.LoggingLibrary.UWP\bin\Release\Plugin.LoggingLibrary.dll" target="lib\UAP10\Plugin.LoggingLibrary.dll" />
    <file src="Plugin.LoggingLibrary.UWP\bin\Release\Plugin.LoggingLibrary.xml" target="lib\UAP10\Plugin.LoggingLibrary.xml" />
</files>

참고

DLL 및 XML 파일의 이름을 짧게 하려면, 지정된 프로젝트를 마우스 오른쪽 단추로 클릭하고 라이브러리 탭을 선택한 다음 어셈블리 이름을 변경합니다.

종속성 추가

네이티브 구현에 대한 특정 종속성이 있는 경우 <dependencies> 요소와 함께 <group> 요소를 사용하여 해당 종속성을 지정합니다. 예를 들어 다음과 같습니다.

<!-- Insert within the <metadata> element -->
<dependencies>
    <group targetFramework="MonoAndroid">
        <!--MonoAndroid dependencies go here-->
    </group>
    <group targetFramework="Xamarin.iOS10">
        <!--Xamarin.iOS10 dependencies go here-->
    </group>
    <group targetFramework="uap">
        <!--uap dependencies go here-->
    </group>
</dependencies>

예를 들어 다음은 iTextSharp를 UAP 대상에 대한 종속성으로 설정합니다.

<dependencies>
    <group targetFramework="uap">
        <dependency id="iTextSharp" version="5.5.9" />
    </group>
</dependencies>

최종 .nuspec

최종 .nuspec 파일은 이제 다음과 같으며, YOUR_NAME을 적절한 값으로 바꾸어야 합니다.

<?xml version="1.0"?>
<package >
    <metadata>
    <id>LoggingLibrary.YOUR_NAME</id>
    <version>1.0.0</version>
    <title>LoggingLibrary</title>
    <authors>YOUR_NAME</authors>
    <owners>YOUR_NAME</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Awesome application logging utility</description>
    <releaseNotes>First release</releaseNotes>
    <copyright>Copyright 2018</copyright>
    <tags>logger logging logs</tags>
        <dependencies>
        <group targetFramework="MonoAndroid">
            <!--MonoAndroid dependencies go here-->
        </group>
        <group targetFramework="Xamarin.iOS10">
            <!--Xamarin.iOS10 dependencies go here-->
        </group>
        <group targetFramework="uap">
            <dependency id="iTextSharp" version="5.5.9" />
        </group>
    </dependencies>
    </metadata>
    <files>
        <!-- Cross-platform reference assemblies -->
        <file src="Plugin.LoggingLibrary\bin\Release\Plugin.LoggingLibrary.dll" target="lib\netstandard1.4\Plugin.LoggingLibrary.dll" />
        <file src="Plugin.LoggingLibrary\bin\Release\Plugin.LoggingLibrary.xml" target="lib\netstandard1.4\Plugin.LoggingLibrary.xml" />
        <file src="Plugin.LoggingLibrary.Abstractions\bin\Release\Plugin.LoggingLibrary.Abstractions.dll" target="lib\netstandard1.4\Plugin.LoggingLibrary.Abstractions.dll" />
        <file src="Plugin.LoggingLibrary.Abstractions\bin\Release\Plugin.LoggingLibrary.Abstractions.xml" target="lib\netstandard1.4\Plugin.LoggingLibrary.Abstractions.xml" />

        <!-- iOS reference assemblies -->
        <file src="Plugin.LoggingLibrary.iOS\bin\Release\Plugin.LoggingLibrary.dll" target="lib\Xamarin.iOS10\Plugin.LoggingLibrary.dll" />
        <file src="Plugin.LoggingLibrary.iOS\bin\Release\Plugin.LoggingLibrary.xml" target="lib\Xamarin.iOS10\Plugin.LoggingLibrary.xml" />

        <!-- Android reference assemblies -->
        <file src="Plugin.LoggingLibrary.Android\bin\Release\Plugin.LoggingLibrary.dll" target="lib\MonoAndroid10\Plugin.LoggingLibrary.dll" />
        <file src="Plugin.LoggingLibrary.Android\bin\Release\Plugin.LoggingLibrary.xml" target="lib\MonoAndroid10\Plugin.LoggingLibrary.xml" />

        <!-- UWP reference assemblies -->
        <file src="Plugin.LoggingLibrary.UWP\bin\Release\Plugin.LoggingLibrary.dll" target="lib\UAP10\Plugin.LoggingLibrary.dll" />
        <file src="Plugin.LoggingLibrary.UWP\bin\Release\Plugin.LoggingLibrary.xml" target="lib\UAP10\Plugin.LoggingLibrary.xml" />
    </files>
</package>

구성 요소 패키징

완료된 .nuspec에서 패키지에 포함되어야 하는 모든 파일을 참조하면 pack 명령을 실행할 준비가 되었습니다.

nuget pack LoggingLibrary.nuspec

이렇게 하면 LoggingLibrary.YOUR_NAME.1.0.0.nupkg가 생성됩니다. NuGet 패키지 탐색기와 같은 도구에서 이 파일을 열고 모든 노드를 확장하면 다음과 같은 내용이 표시됩니다.

NuGet Package Explorer showing the LoggingLibrary package

.nupkg 파일은 확장명이 다른 ZIP 파일일 뿐입니다. .nupkg.zip으로 변경하여 패키지 내용을 검사할 수도 있지만 패키지를 nuget.org에 업로드하려면 먼저 확장명을 복원해야 합니다.

패키지를 다른 개발자가 사용할 수 있도록 하려면 패키지 게시에 대한 지침을 따르세요.