dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.93k stars 532 forks source link

Obfuscar #9519

Closed christophechateau closed 3 days ago

christophechateau commented 4 days ago

Android framework version

net8.0-android

Affected platform version

NET8

Description

Hi,

I'm trying to obfuscate the IL Code in the APK/ABB with obfuscar. Its configuration for the old Xamarin.Form no longer works with the new dotnet/android.

Basicaly, obfuscar takes a dll as input and ouput the obfuscate version of the dll.

I've already read about same other thread, extended entrypoint and assembly store.

Unfortunately, i don't find any entry point where i can obfuscate my dll and then its packed into the APK/AAB. On each of my test, the local dll are obfuscated but when i decompress the final APK/AAB there are still not.

At least, myAssembly.dll is located in many paths :

But also in a .so version which i presume is really packed in the APK/AAB :

A little help about the build process (ie: the dll to .so transformation...) could help me to find where i can hook the obfuscation step.

Very thanks.

Steps to Reproduce

  1. add obfuscar

    <PackageReference Include="Obfuscar" Version="2.2.40">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  2. insert new target

  <Target Name="ObfuscarReleaseNoArch" AfterTargets="Build" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <Message Text="obfuscar start" Importance="High" />
    <Copy SourceFiles="$(MonoAndroidIntermediateAssetsDir)..\..\myAssembly.dll" DestinationFolder="$(MonoAndroidIntermediateAssetsDir)..\..\Obfuscator_Input" />
    <Exec WorkingDirectory="$(MonoAndroidIntermediateAssetsDir)..\..\Obfuscator_Input" Command="$(Obfuscar) $(ProjectDir)obfuscar_dll.xml" />
    <Copy SourceFiles="$(MonoAndroidIntermediateAssetsDir)..\..\Obfuscator_Output\myAssembly.dll" DestinationFolder="$(MonoAndroidIntermediateAssetsDir)..\..\" />
    <Copy SourceFiles="$(MonoAndroidIntermediateAssetsDir)..\..\Obfuscator_Output\myAssembly.dll" DestinationFolder="$(TargetDir)" />
    <Message Text="obfuscar end" Importance="High" />
  </Target>
<!-- note : i've also others targets for each architecture to obfuscate the myAssembly.dll -- >
  1. use obfuscar_dll.xml
<?xml version='1.0'?>
<Obfuscator>
  <Var name="InPath" value="." />
  <Var name="OutPath" value="$(InPath)\..\Obfuscator_Output" />
  <Var name="KeepPublicApi" value="true" />
  <Var name="HidePrivateApi" value="true" /
  <Var name="SuppressIldasm" value="false" />
  <Var name="AnalyzeXaml" value="false" />
  <Var name="HideStrings" value="false" />
  <Var name="UseUnicodeNames" value="false" />
  <Var name="RenameProperties" value="false" />
  <Var name="RenameFields" value="false" />
  <Var name="RenameEvents" value="false" />

  <!-- the assembly we want to obfuscate -->
  <Module file="$(InPath)\myAssembly.dll" />

</Obfuscator>
christophechateau commented 3 days ago

For information, i found a workaround by targeting after ILLink (so just before the AOT in Release mode) :

Target

 <Target Name="ObfuscarReleaseAll" AfterTargets="ILLink" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <Message Text="start obfuscate for all $(MonoAndroidIntermediateAssetsDir)..\..\linked\" Importance="High" />
    <Exec WorkingDirectory="$(MonoAndroidIntermediateAssetsDir)..\..\linked\" Command="$(Obfuscar) $(ProjectDir)obfuscar_dll_arch.xml" />
    <Copy SourceFiles="$(MonoAndroidIntermediateAssetsDir)..\..\linked\Obfuscator_Output\myAssembly.dll" DestinationFolder="$(MonoAndroidIntermediateAssetsDir)..\..\linked\" />
    <Message Text="end obfuscate for all $(MonoAndroidIntermediateAssetsDir)..\..\linked\" Importance="High" />
  </Target>

Obfuscar

<?xml version='1.0'?>
<Obfuscator>

  <Var name="InPath" value="." />
  <Var name="OutPath" value="$(InPath)\Obfuscator_Output" />
  <Var name="KeepPublicApi" value="true" />
  <Var name="HidePrivateApi" value="true" />
  <Var name="SuppressIldasm" value="false" />
  <Var name="AnalyzeXaml" value="false" />
  <Var name="HideStrings" value="false" />
  <Var name="UseUnicodeNames" value="false" />

  <Var name="RenameProperties" value="false" />
  <Var name="RenameFields" value="false" />
  <Var name="RenameEvents" value="false" />

  <Module file="$(InPath)\myAssembly.dll" />

</Obfuscator>