5

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?

qakmak
  • 1,287
  • 9
  • 31
  • 62
Ajit Hegde
  • 542
  • 1
  • 15
  • 33

3 Answers3

2

It has come extremely easy to use .net reactor with xamarin today:

  1. from reactor ide install extension for your visual studio appropriate version (menu Visual Studio)

  2. in reactor ide create a project for your xamarin solution. You can use menu "Protection Themes->Xamarin" or play with you own settings. Do not forget to add additional files (your Forms shared project and others needed). save project, note its path.

  3. in Visual Studio ide menu Tools->Reactor Automation set:

  • project file path to your Xamarin Android (or other needed) platform project, check Enabled.
  • configuration to Release,
  • Net Reactor Project file to the one that you created in step 2.

Click ok, that's it.

The build process would be MSBuild compiling, then reactor would apply its processing, then VS would pack/aot whatever was set for platform deployment/distribution.

Nick Kovalsky
  • 5,378
  • 2
  • 23
  • 50
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.

Polyariz
  • 534
  • 1
  • 7
  • 17
  • Didn't work. When I tried to publish ,it is replacing the obfuscated dll with original dll. – Ajit Hegde Jun 26 '16 at 09:31
  • Try to specify absolute links to files. – Polyariz Jun 26 '16 at 17:34
  • When I tried to publish the project replaced the "bin\Release\obfuscated.dll" with "bin\release\un obfuscated.dll". I also replaced dll in "obj\release\", "obj\release\assemblies" etc. But they also got replaced – Ajit Hegde Jun 27 '16 at 01:29
  • There is my video guide (on russian): [link](https://youtu.be/MQi0f1BLBzI) – Polyariz Jun 27 '16 at 18:23
  • Ok. It worked. But I have a question. What is the difference between obfuscate_android.bat PostBuildEvent> and ...? – Ajit Hegde Jun 28 '16 at 04:06
  • There is the answer you can read [link](http://stackoverflow.com/questions/6128567/visual-studio-project-file-difference-between-postbuildevent-and-afterbuild-targ) – Polyariz Jun 28 '16 at 09:49
  • I am very much grateful for the answer and making a video to show it !!! – Ajit Hegde Jun 28 '16 at 12:59
  • I'm glad I could help. Good luck to you! – Polyariz Jun 29 '16 at 07:11
  • Unfortunately, this does not work with PCL or Shared projects. It throws a "LinkAssemblies task failed unexpectedly" error. –  Jul 21 '16 at 04:54
  • @Polyariz I watched your tutorial, but for xamarin v6 it doesn't work. Always obfuscated dlls are ingored and original dll is added to apk instaed. Any solution? – marcu Jul 25 '16 at 18:09
  • You can put the obfuscated file into .apk package and then manually sign and zipalign it. – Polyariz Jul 26 '16 at 19:38
  • For normal C# projects I used .net Reactor sucessfully with a "AfterCompile" target. Unfortunately the Xamarin project build fails with "The "LinkAssemblies" task failed unexpectedly after compile" – smedasn Oct 04 '16 at 19:38
  • @smedasn you can build the project in same type for xamarin forms too. once build the project go to the pcl projects bin\Release folder and obj\release folder. Obfuscate those dlls like we do in windows. Use the same method of obfuscation shown in video by "Polyariz" then build the apk. It worked for me. I am using vs2017 rc and instead of pcl project I used .net standard 1.4 project – Ajit Hegde Dec 24 '16 at 10:57
1

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.

ncruz
  • 93
  • 2
  • 9
  • It works. try postbuild event and also did you check whether it compiles correctly without obfuscation enabled. – Ajit Hegde Jan 01 '17 at 04:30
  • Ajit, can you post your csproj xml? Are you using "" or your command here PostBuildEvent> ? Are you manually inserting the obfuscated DLL into the apk and manually signing the apk - as Polyariz suggested, or are you using the apk generated by the build? – ncruz Jan 04 '17 at 03:40
  • PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> pdbonly true bin\Release\ TRACE prompt 4 False SdkOnly "obfuscate.bat" True – Ajit Hegde Jan 04 '17 at 04:00
  • I am adding postbuild event in property group – Ajit Hegde Jan 04 '17 at 04:01
  • I'm getting the "The "LinkAssemblies" task failed unexpectedly." error again. What I'm doing is manually running Reactor and creating the obfuscated DLL then just doing a 'copy' command to copy the obfuscated DLL into the release folder, instead of calling Reactor to create the obfuscated DLL on the fly. Should be the same. Did you decompile the DLL inside your apk to make sure it is the obfuscated one? – ncruz Jan 05 '17 at 05:29
  • yes I did that and its working perfectly. Did you tried to make the apk file without obfuscation? Because Link Assemblies task failed unexpectedly isn't the cause of obfuscation. So try to make apk file without obfuscation . – Ajit Hegde Jan 06 '17 at 05:49
  • Ajit, I'm just doing a Release build (Build > Rebuild Solution), I'm not even making the apk file. If I remove '"obfuscate.bat"' it builds fine. As far as making an apk file, I see two ways to do this in Visual Studio: 1) using the menu option Build > Export Android Package (.apk) or 2) using the menu option Tools > Android > Publish Android App. In Xamarin Studio, it has the 'Archive for Publishing' menu option under 'Build'. When I do method #1 w/o obfuscation in Visual Studio, it creates 3 apk files: my-app.apk, my-app.Signed.apk and my-app-Aligned.apk. – ncruz Jan 08 '17 at 00:18
  • Ajit, what is your setup: version of Visual Studio, do you have just 1 project w/o a PCL... I'm using VS 2015 and have a 2nd project for my Utilities which is referenced as a DLL in my 1st project. – ncruz Jan 08 '17 at 00:25
  • Also, do you do a Build first then make the apk or go straight to making the apk? Which method do you use to make the apk, #1 or #2? – ncruz Jan 08 '17 at 00:35
  • I just tested making the apk file in Xamarin Studio (Build > Archive For Publishing) without obfuscation and it gets created fine. I then insert "obfuscate.bat" into the csproj file and do a rebuild, and it fails with the "LinkAssemblies" error. I then remove "obfuscate.bat" and manually copy an obfuscated version of the DLL file into bin/Release and obj/Release folders and make the apk file again (Build > Archive For Publishing) and the apk is created but the DLL file inside is the non-obfuscated version. – ncruz Jan 08 '17 at 02:20
  • Ajit, what versions of the Android SDK and Java JDK do you have installed? And do you have the Android NDK installed? Along with the 'LinkAssemblies' error, I'm also getting an error message stating that "Xamarin.Android for Visual Studio requires Android NDK, which is used to embed .NET assemblies into native libraries." – ncruz Jan 09 '17 at 00:20
  • sdk details : jdk 1.8, android sdk 4.4,5,6 , android ndk r12b – Ajit Hegde Jan 09 '17 at 17:27
  • compilation details: compile using android version: 6.0 min android to target: api level 20 and target android version target level 22 – Ajit Hegde Jan 09 '17 at 17:29
  • linker : linking sdk assemblies only – Ajit Hegde Jan 09 '17 at 17:30
  • Ajit, thank you for your answers. However,its still not working. I downloaded the Android NDK (r13b) and unzipped it into the C:\Program Files (x86)\Android\ folder then set it in Visual Studio (Tools>Options>Xamarin>Android Settings) but it still has the 'Link Assemblies' error. – ncruz Jan 11 '17 at 05:59
  • It really looks like the 'Link Assemblies' error is caused by the obfuscation. At this point, with all the things I've tried I still don't know what would cause this not to work in my setup. How do you make your apk file? Do you build/rebuild and then publish separately? – ncruz Jan 11 '17 at 06:08
  • Btw, my setup is Visual Studio 2015 Pro, Xamarin 4.1.0.530, Xamarin Android 6.1.0.71 - not sure if that makes a difference. – ncruz Jan 11 '17 at 06:09
  • is it possible for you to create a github solution.? – Ajit Hegde Jan 13 '17 at 08:44
  • I was thinking something along the same lines. I had created a solution called 'TestObfuscate' which is a blank Android project in VS 2015 to see if a simple project will obfuscate without problems as my project is quite complex. This simple blank project still errors out with the 'LinkAssemblies' error. I will upload to github and provide the link as an edit to my answer above. – ncruz Jan 14 '17 at 22:03
  • Ajit, were you able to download the code/project and try it? I'm thinking it might be the version of .NET Reactor I'm using. – ncruz Jan 22 '17 at 22:00
  • which version you are using? I am using version 5.0 – Ajit Hegde Jan 24 '17 at 13:06
  • I'm using version 4.8 - maybe it's as simple as upgrading to the latest version. Were you able to do a successful build/obfuscate on the project I uploaded to git? I'd like to have this confirmed before buying a license. – ncruz Jan 25 '17 at 18:01
  • I will update the project today or tomorrow. Once you check it you can decide – Ajit Hegde Jan 27 '17 at 03:19
  • Ajit, I just checked the project on github and there are no updates. Can you pls upload your update and/or describe the mods/edits you did to make it work? Thanks. – ncruz Feb 04 '17 at 19:00
  • Ajit, I downloaded & installed a demo of version 5 of .NET Reactor but it still has the same 'LinkAsssemblies' error. Can you pls share your findings on what worked for your project? – ncruz Feb 05 '17 at 19:26
  • the project you uploaded will work no problem with it. However when you add the code and references then it will give the error. – Ajit Hegde Feb 06 '17 at 10:18
  • So you're not able to get around the error? Can you upload a sample project that works for you so I can check it out and see if there are differences? Thanks. – ncruz Feb 07 '17 at 15:34
  • I uploaded working obfuscated projects project file and manifest https://drive.google.com/open?id=0B8TaBG9aVytvX1lvclJBQl9IQWs also check whether project obfuscates in dotfuscator community edition. – Ajit Hegde Feb 08 '17 at 11:26
  • Ajit, I checked the proj file & manifest and saw that I was using earlier versions of Android for Compile, Min and Target. My proj was using v4.4 (API 16) for Target and v4.1 (API 16) for min and your manifest is using v5.1 (API 22) for Target and v4.4 for min so I downloaded the newer SDKs as well as a couple of updates then changed the manifest to use them but it still throws the error. Then I thought it could be the version of the .NET framework that I was using (v4.5) so I created a new proj that uses .NET framework 4.6.1 but the 'LinkAssemblies' error is still there. – ncruz Feb 11 '17 at 16:18
  • I see that your proj is referencing Xamarin.Android.Support.v4 so I tried to install it via NuGet but says that it can't since my proj targets v5.1. Can you create a clean sample project (just a blank Android proj) & upload a zip file of it? Thanks. – ncruz Feb 11 '17 at 16:18
  • Ajit, thanks for all your help so far but it's still not working. Btw, what version of MSBuild and Xamarin.Android (Xamarin.VisualStudio) do you have installed? The version of MSBuild I had was 2013 and I just installed v2015. The version of Xamarin.Android I have installed now is v4.1.0.530 from summer 2016. Also, what version of the Xamarin.Android.Common.targets file do you have? The file location is in "C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets" and is 139KB in size. – ncruz Feb 18 '17 at 22:14
  • Ajit, can you create a clean sample project (just a blank Android proj) & upload a zip file of it? I'd like to test a project in my dev environment that works from yours. Regards – ncruz Feb 18 '17 at 22:18