Search code examples
c#xamarin.formsxamarin.androidaot

Can I protect my application using AOT compilation?


I would like to use AOT compilation to protect my code, but according to this: https://learn.microsoft.com/uk-ua/xamarin/android/deploy-test/release-prep/?tabs=windows#aot the resulting native code is included in the APK along with the uncompiled assemblies. Does this mean that both the compiled and the uncompiled versions are included, and AOT does not really help for obfuscation?


Solution

  • I used Hybrid AOT to remove IL code from .NET assemblies. You need to manually edit Xamarim.Android.Common.Targets in C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Xamarin\Android\

      <CilStrip
        Condition=" '$(AndroidAotMode)' == 'Hybrid' And '$(AotAssemblies)' == 'True' "
        AndroidAotMode="$(AndroidAotMode)"
        ToolPath="$(_MonoAndroidToolsDirectory)"
        ApkOutputPath="$(_BuildApkEmbedOutputs)"
        ResolvedAssemblies="@(_ShrunkAssemblies)">
      </CilStrip> 
    

    And edit your Android.csproj

    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <DebugSymbols>false</DebugSymbols>
        <DebugType>none</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>bin\Release</OutputPath>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
        <EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk>
        <AndroidManagedSymbols>true</AndroidManagedSymbols>
        <AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
        <AndroidLinkMode>SdkOnly</AndroidLinkMode>
        <EnableLLVM>false</EnableLLVM>
        <AotAssemblies>true</AotAssemblies>
        <AndroidAotMode>Hybrid</AndroidAotMode>
        <AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
        <AndroidAotAdditionalArguments>no-write-symbols,nodebug</AndroidAotAdditionalArguments>
      </PropertyGroup>
    

    Source: https://forums.xamarin.com/discussion/182826/hybrid-aot