Information on Xamarin.Android

By Edward Brey

This page shows how to automate Obfuscar for Xamarin.Android using MSBuild. Integration for other Xamarin platforms is similar.

In this article:

Nuget Package

Install the Obfuscar NuGet package into the Xamarin.Android project.

Install-Package Obfuscar

Project File

In the Xamarin.Android project file (.csproj), add an AfterBuild target to run the obfuscator.

The obfuscator runs from MonoAndroidIntermediateAssetsDir, which is the assets directory that gets bundled into the APK. An example of this directory is obj\Release\90\android\assets.

The obfuscator outputs to the target directory (e.g. bin\Release) so that the mapping file will go there. It then copies the obfuscated files to the assets directory.

This is an example target for obfuscating a single assembly called MyAssembly.dll:

<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release' ">
  <Exec WorkingDirectory="$(MonoAndroidIntermediateAssetsDir)" Command="$(Obfuscar) $(ProjectDir)obfuscar.xml" />
  <Copy SourceFiles="$(TargetDir)MyAssembly.dll" DestinationFolder="$(MonoAndroidIntermediateAssetsDir)" />

Obfuscar Configuration File

Create obfuscar.xml in the Xamarin.Android project. Configure it to output to the target directory relative to the assets directory.

  <Var name="OutPath" value="..\..\..\..\..\bin\Release" />
  <Module file="MyAssembly.dll" />