How to use .Net Reactor obfuscated dll of Xamarin Android APK

自闭症网瘾萝莉.ら 提交于 2019-12-04 14:32:09

问题


I am new to Xamarin Android. I have created an App using Visual Studio 2015 Community Edition. I have set the Solution Configuration to Release.

For obfuscation I used .Net Reactor.

This is how I tried to obfuscate

1: Once I build the App, I will go to Bin\Release folder

2: Obfuscate the App.dll

3: Replace the original dll with obfuscated dll in Bin\Release, Obj\Release and Obj\Release\assemblies

4: Go to Tools->Android->Publish

However when I tried to publish the obfuscated dll will be replaced by original dll.

So what I am doing wrong ? Do I have to manually pack the apk file. If that is the case how can I do that?


回答1:


1) Publish not obfuscated an application .apk, rename it to .zip.

2) Move from it the file Mono.Android.dll from the folder "assemblies" into the folder YourProjectName\bin\Release.

3) Create a new .NET Reactor project, set the right settings (uncheck Anti ILDASM, also the obfuscation process renames a fields that is used in reflection or serialization. To avoid crash of app, should set the exclusions for ignore some types or methods.). Save this project file into YourXamarinFolder\YourProjectName\bin\Release.

4) Create the file obfuscate_android.bat, insert here the code:

 "C:\Program Files (x86)\Eziriz\.NET Reactor\dotNET_Reactor.exe" -project "NET_Reactor_Project_Name.nrproj"
  copy "YourProjectName_Secure\YourProjectName.dll" "YourProjectName.dll"
  DEL "YourProjectName_Secure\*.dll" "YourProjectName_Secure\*.pdb" /q

Move this file into YourProjectName\bin\Release.

5) Unload your Xamarin project and edit YourProjectName.csproj Find the next block:

<PropertyGroup Condition = " '$ (Configuration) | $ (Platform)' == 'Release | AnyCPU'">
<! -- Insert here the link to obfuscate_android.bat -->
<PostBuildEvent>obfuscate_android.bat </ PostBuildEvent>
 </ PropertyGroup>

6) Save all and Reload project

7) Run Publish.




回答2:


I got my Xamarin Android project in Xamarin Studio obfuscating fine with .NET Reactor by hooking into MSBuild. Add this markup to the bottom of your csproj file:

  <Target Name="AfterCompile">
    <Exec Command="&quot;C:\Program Files (x86)\Eziriz\.NET Reactor\dotNET_Reactor.exe&quot; -file &quot;$(ProjectDir)$(IntermediateOutputPath)$(TargetFileName)&quot; -targetfile &quot;$(ProjectDir)$(IntermediateOutputPath)$(TargetFileName)&quot; -suppressildasm 1 -obfuscation 1 -stringencryption 1 -antitamp 1 -control_flow_obfuscation 1 -flow_level 9 -resourceencryption 1 -mono 1 -exception_handling 1" Condition="'$(ConfigurationName)' == 'Release'" />
  </Target>
</Project>

To note, when I upgraded to Xamarin in Visual Studio 2015 this does not work anymore. Gave me a "error MSB4018: The "LinkAssemblies" task failed unexpectedly."

**edit I got past the 'LinkAssemblies' task error by setting PostBuild to true and hooking to the 'PostBuildEvent' event instead as Ajit pointed out.

However, the obfuscated DLL is not getting added to the apk file. I tested this by unzipping the .apk file (just change the file extension to .zip or .rar) and decompiling the DLL file inside. It was not obfuscated.

I looked in the bin/Release and the obj/Release folders, and the DLL in there is obfuscated. But for some reason, the DLL in the apk is not the obfuscated DLL.

Perhaps the PostBuildEvent is too late in the process of creating the apk file?

Here is my current setup:

Set the PostBuild to true:

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
    <PostBuildEventUseInBuild>true</PostBuildEventUseInBuild>
  </PropertyGroup>

and then hook to the 'PostBuildEvent' event instead 'AfterCompile'

  <Target Name="PostBuildEvent">
    <Exec Command="&quot;C:\Program Files (x86)\Eziriz\.NET Reactor\dotNET_Reactor.exe&quot; -file &quot;$(ProjectDir)$(IntermediateOutputPath)$(TargetFileName)&quot; -targetfile &quot;$(ProjectDir)$(IntermediateOutputPath)$(TargetFileName)&quot; -suppressildasm 1 -obfuscation 1 -stringencryption 1 -antitamp 1 -control_flow_obfuscation 1 -flow_level 9 -resourceencryption 1 -mono 1 -exception_handling 1" Condition="'$(ConfigurationName)' == 'Release'" />
  </Target>
</Project>

** update I created a simple blank Android project in VS 2015 to test if the problem may be caused by the complexity of my project (use of 3rd party controls, common utility library as a 2nd project in solution, etc). The 'LinkAssemblies' error still occurs so it's probably my environment/setup.

I have a GitHub account but haven't downloaded the app, so I couldn't upload the entire project folder structure, so I just zipped up the whole solution and uploaded that. It's available here:

https://github.com/nbcruz/TestObfuscate/blob/master/TestObfuscate.zip

I had to delete the contents of the obj/Debug folder as it had 78MB of files to make the zip file under 25MB for GitHub to allow.

The .NET Reactor project is inside the bin\Release\ folder. I manually run it and create the obfuscated DLL which is inside the bin\Release\TestObfuscate_Secure folder. The obfuscate.bat file is also inside the bin\Release\ folder and it basically just copies the DLL inside the bin\Release\TestObfuscate_Secure folder into the bin\Release\ folder. This blank simple solution still throws the 'LinkAssemblies' error.



来源:https://stackoverflow.com/questions/37764732/how-to-use-net-reactor-obfuscated-dll-of-xamarin-android-apk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!