ProGuard is a tool used in the building process to optimize and obfuscate the Java code of your APK, and also remove unused classes. The resulting APK when using ProGuard will have a smaller size and will be harder to reverse-engineer (decompilation).
ProGuard can be used too in Xamarin.Android apps, and also will reduce the APK file size and obfuscate the Java code. Be aware, however, that the ProGuard obfuscation applies only to Java code. To obfuscate .NET code, the developer should use Dotfuscator or similar tools.
First, to enable ProGuard in your Xamarin.Android app, go to your project Properties -> Android Options -> Packaging -> Enable ProGuard, as in the print screen below:
This enables ProGuard when building your app.
Xamarin.Android, by default, sets its own configurations for ProGuard, that can be found inside the folders
obj/Release/proguard, in the files
proguard_xamarin.cfg. The three files are combined as configurations for ProGuard and they are automatically created by Xamarin when building.
If the developer wishes to further customize the ProGuard options, he/she can create a file in the project's root named
proguard.cfg (other names are valid too, as long as the extension is .cfg) and setting its Build Action to ProguardConfiguration, as in the picture below:
In the file, custom ProGuard options can be inserted, such as
-keep class and others.
As by now (April/2017), the Android SDK that is usually downloaded has an old version of ProGuard, which can cause errors when building the app using Java 1.8. When building, the Error List shows the following message:
Error Can't read [C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v7.0\mono.android.jar] (Can't process class [android/app/ActivityTracker.class] (Unsupported class version number [52.0] (maximum 51.0, Java 1.7))) [CREATEMULTIDEXMAINDEXCLASSLIST]
To fix this problem, you must download the most recent version of ProGuard (here) and copy the contents of the .zip file to
android-sdk\tools\proguard\. That will update the ProGuard and building process should run without problems.
After that, you should be able to successfully build your Xamarin.Android app with ProGuard.