Press ESC to close

Xamarin Forms Packages Config To Packages Reference

Hello friends, in this article I will talk about how to modernize our old Xamarin projects. Let’s talk about why we need such a thing first. In our old type projects, when we add packages with packages.config, update the packages or throw the packages to git, the size of the project increases and causes version conflicts.

Packages.config works like this; When you install a package, it adds the package to the packages.config file. Each package added to the Packages.config file is downloaded to the packages folder within the project file. For this reason, the dimensions of your projects become too large and increase maintenance costs. The packages you install with PackagesReference are downloaded to a common area, not into the project. In this way, the size of the project does not increase and it uses the previously downloaded packages in other projects without downloading them again. There is also an increase in the project opening time.

For this example, I chose an open source project that we used for the Progress Bar at the time. You can reach the project here.

When you open the project, we will see such a structure.

For this, we first need to make changes in the .csproj section in the Android section. We now specify that this project will restore packages with PackageReference. Then again in this file, as you can see below, the packages; It takes it from the packages folder in the project. Instead, we state that we will now add packages by reference.

After doing these operations, it looks like this.

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <ProjectGuid>{2B700BAB-5304-4AC1-8321-6F68578D9459}</ProjectGuid>
    <OutputType>Library</OutputType>
    <RootNamespace>CustomProgressBar.Droid</RootNamespace>
    <MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
    <MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
    <AndroidResgenClass>Resource</AndroidResgenClass>
    <AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
    <AndroidApplication>True</AndroidApplication>
    <AssemblyName>CustomProgressBar.Droid</AssemblyName>
    <TargetFrameworkVersion>v6.0</TargetFrameworkVersion>
    <AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug</OutputPath>
    <DefineConstants>DEBUG;</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <AndroidLinkMode>None</AndroidLinkMode>
    <ConsolePause>false</ConsolePause>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>full</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release</OutputPath>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
    <ConsolePause>false</ConsolePause>
  </PropertyGroup>
  
  <PropertyGroup>
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
  </PropertyGroup>

  <ItemGroup>
      <PackageReference Include="MonoDroid.Toolkit" Version="1.1.0.0" />
      <PackageReference Include="Xamarin.Android.Support.Animated.Vector.Drawable" Version="23.3.0" />
      <PackageReference Include="Xamarin.Android.Support.Design" Version="23.3.0" />
      <PackageReference Include="Xamarin.Android.Support.v4" Version="23.3.0" />
      <PackageReference Include="Xamarin.Android.Support.v7.AppCompat" Version="23.3.0" />
      <PackageReference Include="Xamarin.Android.Support.v7.CardView" Version="23.3.0" />
      <PackageReference Include="Xamarin.Android.Support.v7.MediaRouter" Version="23.3.0" />
      <PackageReference Include="Xamarin.Android.Support.v7.RecyclerView" Version="23.3.0" />
      <PackageReference Include="Xamarin.Android.Support.Vector.Drawable" Version="23.3.0" />
      <PackageReference Include="Xamarin.Forms" Version="2.3.3.180" />
    </ItemGroup>

  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Xml" />
    <Reference Include="System.Core" />
    <Reference Include="Mono.Android" />
    <Reference Include="System.Windows" />
  </ItemGroup>

  <ItemGroup>
    <Compile Include="MainActivity.cs" />
    <Compile Include="Resources\Resource.designer.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <Compile Include="Renderers\CircularProgressRenderer.cs" />
  </ItemGroup>
  <ItemGroup>
    <None Include="Resources\AboutResources.txt" />
    <None Include="Assets\AboutAssets.txt" />
    <None Include="Properties\AndroidManifest.xml" />
  </ItemGroup>
  <ItemGroup>
    <AndroidResource Include="Resources\layout\Main.axml" />
    <AndroidResource Include="Resources\values\Strings.xml" />
    <AndroidResource Include="Resources\drawable\Icon.png" />
  </ItemGroup>
  <Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
  <ItemGroup />
  <ItemGroup>
    <ProjectReference Include="..\CustomProgressBar.Portable\CustomProgressBar.Portable.csproj">
      <Project>{BCBDC8BF-157E-450D-8B1E-4CBF49A81EDA}</Project>
      <Name>CustomProgressBar.Portable</Name>
    </ProjectReference>
  </ItemGroup>
  <Import Project="..\packages\Xamarin.Android.Support.Vector.Drawable.23.3.0\build\Xamarin.Android.Support.Vector.Drawable.targets" Condition="Exists('..\packages\Xamarin.Android.Support.Vector.Drawable.23.3.0\build\Xamarin.Android.Support.Vector.Drawable.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
  </Target>
</Project>

As a last step, you can delete the packages.config file from the project.

If you want to transfer your project to AndroidX, you can find more detailed information here.

If you have any questions, you can ask by comment or e-mail. Good work.

 

Leave a Reply

Your email address will not be published. Required fields are marked *