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.92k stars 525 forks source link

Error dotnet reactor code obfuscation #8908

Open vkmohan1689 opened 5 months ago

vkmohan1689 commented 5 months ago

Steps to Reproduce

  1. obfuscate dll with dotreactor cmd line tool
  2. project should be added as reference in android project
  3. build release version

Expected Behavior

apk should be generated without any issue

Actual Behavior

when link behavior is set to "Don't link " no issue with build if link behavior is set either "link sdk assemblies " or "link all assemblies " throws below error

Environment

<Target Name="ReactorCall" AfterTargets="Compile">
      <Exec Command="./dotNET_Reactor -file $(ProjectDir)$(IntermediateOutputPath)$(TargetFileName) -obfuscation 1 -stringencryption 1 -targetfile $(ProjectDir)$(IntermediateOutputPath)$(TargetFileName)" />

Build Logs

/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Legacy.targets(5,5): Error XALNK7000: Mono.Linker.MarkException: Error processing method: 'System.Void <Module>::m8DC66491E0B6E2E()' in assembly: 'Libary3.dll' ---> System.NullReferenceException: Object reference not set to an instance of an object
  at Mono.Linker.Steps.MarkStep.MarkMethod (Mono.Cecil.MethodReference reference) [0x00009] in <43053c0ed9b14666903c480c1357d840>:0 
  at Mono.Linker.Steps.MarkStep.MarkInstruction (Mono.Cecil.Cil.Instruction instruction) [0x00040] in <43053c0ed9b14666903c480c1357d840>:0 
  at Mono.Linker.Steps.MarkStep.MarkMethodBody (Mono.Cecil.Cil.MethodBody body) [0x000c2] in <43053c0ed9b14666903c480c1357d840>:0 
  at Mono.Linker.Steps.MarkStep.ProcessMethod (Mono.Cecil.MethodDefinition method) [0x001b1] in <43053c0ed9b14666903c480c1357d840>:0 
  at Mono.Linker.Steps.MarkStep.ProcessQueue () [0x0001b] in <43053c0ed9b14666903c480c1357d840>:0 
   --- End of inner exception stack trace ---
  at Mono.Linker.Steps.MarkStep.ProcessQueue () [0x00047] in <43053c0ed9b14666903c480c1357d840>:0 
  at Mono.Linker.Steps.MarkStep.ProcessPrimaryQueue () [0x0000a] in <43053c0ed9b14666903c480c1357d840>:0 
  at Mono.Linker.Steps.MarkStep.Process () [0x000f6] in <43053c0ed9b14666903c480c1357d840>:0 
  at Mono.Linker.Steps.MarkStep.Process (Mono.Linker.LinkContext context) [0x0000d] in <43053c0ed9b14666903c480c1357d840>:0 
  at MonoDroid.Tuner.MonoDroidMarkStep.Process (Mono.Linker.LinkContext context) [0x0000b] in <43053c0ed9b14666903c480c1357d840>:0 
  at Mono.Linker.Pipeline.ProcessStep (Mono.Linker.LinkContext context, Mono.Linker.Steps.IStep step) [0x0000d] in <43053c0ed9b14666903c480c1357d840>:0 
  at Mono.Linker.Pipeline.Process (Mono.Linker.LinkContext context) [0x0000f] in <43053c0ed9b14666903c480c1357d840>:0 
  at MonoDroid.Tuner.Linker.Run (Mono.Linker.Pipeline pipeline, Mono.Linker.LinkContext context) [0x00000] in <43053c0ed9b14666903c480c1357d840>:0 
  at MonoDroid.Tuner.Linker.Process (MonoDroid.Tuner.LinkerOptions options, Mono.Linker.ILogger logger, Mono.Linker.LinkContext& context) [0x00071] in <43053c0ed9b14666903c480c1357d840>:0 
  at Xamarin.Android.Tasks.LinkAssemblies.Execute (Java.Interop.Tools.Cecil.DirectoryAssemblyResolver res) [0x0018f] in <43053c0ed9b14666903c480c1357d840>:0 
  at Xamarin.Android.Tasks.LinkAssemblies.RunTask () [0x0001b] in <43053c0ed9b14666903c480c1357d840>:0 
  at Microsoft.Android.Build.Tasks.AndroidTask.Execute () [0x00000] in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:17  (XALNK7000) (OfuscationExample.Android)
jonathanpeppers commented 5 months ago

@vkmohan1689 for this to work, you are going to need some general MSBuild knowledge and know the appropriate places to hook into the build process. This isn't going to work out of the box. We can offer suggestions, but the docs here are probably the place to start:

$(AfterGenerateAndroidManifest) is an example of a build-time extension point.

If ".NET Reactor" is a product, they could build this support for you. I imagine it would be a NuGet package that supports all .NET MAUI platforms.