Jay Nanavaty Jay Nanavaty - 2 months ago 34
Android Question

Xamarin obfuscation

Is there anything out there that does obfuscation? I have tried Crypto Obfuscator for Android and when I de-compiled using dex2jar, I see no difference between obfuscated and normal assembly. So far I have went through following links:


Mono for Android, code obfuscation



You refered to a forums post on xamarin.com ("Light Obfuscation"). There, now I have added an explanation of how to obfuscate with Xamarin Studio and Babel for .NET.

I will repeat it here:

You don't need to have the full Visual Studio to get an easy and comfortable way of obfuscating. I now use Babel for .NET with Xamarin Studio (in Windows). I haven't tried to get Babel running on a Mac, maybe it's possible.

So, here I will explain how to obfuscate your Android app in Xamarin Studio:

The good thing is that Xamarin Studio uses the MSBuild mechanism and Babel can be integrated in a MSBuild process.

For me (except for installing Babel) there were only two steps necessary:

(Step 1)

Edit you .csproj file with a text editor. Xamarin Studio must not be running.


    [... All existing stuff ...]

    <UsingTask TaskName="Babel" AssemblyName="Babel.Build, Version=, Culture=neutral, PublicKeyToken=138d17b5bd621ab7" />
    <Target Name="AfterBuild" Condition=" '$(Configuration)' != 'Debug' ">
    <Babel InputFile="$(TargetPath)" OutputFile="$(TargetPath)" GenerateDebug="true" 
    SuppressIldasm="false" ObfuscateTypes="true" ObfuscateProperties="true" ObfuscateEvents="true"      ObfuscateMethods="true" 
    ObfuscateFields="true" VirtualFunctions="true" FlattenNamespaces="false" 


Whenever you build your app and the build mode is not Debug (so it is Release), this Task is applied. You can specify an xml file where you can define fine-grained rules for the obfuscation process. (e.g. exclude certain classes etc.)

By the way: A rule of thumb is: Define every class, interface, delegate or enum as "internal", not as "public". By default, types that have to be visible outside the assembly (public types) will not be obfuscated. Internal types will be obfuscated by default. The only class I marked as "public" is "MainActivity".

(Step 2)

When I started the first try for my app, I got the following error message:

BABEL : error : Could not resolve assembly: 'Mono.Android, Version=, Culture=neutral, PublicKeyToken=84e04ff9cfb79065'

Which I could not understand first, because a Hello-World Android app was obfuscated without problems. After some hours of research, I found the reason for the error. My activity (my game has only one activity) had the following attribute:

    Label = "The name of my game", 
    MainLauncher = true, 
    WindowSoftInputMode = SoftInput.AdjustPan,
    ConfigurationChanges = ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.Orientation | ConfigChanges.ScreenSize

"Label" and "MainLauncher" turned out not to be the problem. But "WindowSoftInputMode" and "ConfigurationChanges" were the problems.

To fix it, I fully removed the [Activity (...)] attribute from the .cs file and added the necessary information by hand to the AndroidManifest.xml. This way, the obfuscation worked without problems.

You may wonder why the Activity attribute caused a problem. I realized that ILSpy also had a problem when this attribute was applied to the C# Activity class with "WindowSOftInputMode" and "ConfigurationChanges". So I think it is not a problem of Babel, but a problem of Xamarin. The reason might be that, while "Label" and "MainLauncher" are fundamental types (string and bool), the other two are not. Their types are defined in Mono.Android.dll which seems to be refered to in a wrong way. The best thing would be if Xamarin removed the attribute for the compiled dll because it is only used for making the AndroidManifest.xml in the build step.