Xamarin.Android Proguard - Unsupported class version number 52.0

I'm trying to use Proguard in my Xamarin.Android project, yet the compilation fails with the error Unsupported class version number [52.0] (maximum 51.0, Java 1.7)

I saw from those two questions that it may be a mismatch between Java 7 and Java 8, more precisely some versions of proguard don't support Java 8. However in Xamarin Preferences -> SDK Location, Java SDK points to JDK 7 : /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home

Is there any place where proguard can be configured more precisely ? Any other idea ?

Here's the failure log :

java.io.IOException: Can't read [/Library/Frameworks/Xamarin.Android.framework/Versions/7.0.0-18/lib/xbuild-frameworks/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))) at proguard.InputReader.readInput(InputReader.java:230) at proguard.InputReader.readInput(InputReader.java:200) at proguard.InputReader.readInput(InputReader.java:178) at proguard.InputReader.execute(InputReader.java:78) at proguard.ProGuard.readInput(ProGuard.java:196) at proguard.ProGuard.execute(ProGuard.java:78) at proguard.ProGuard.main(ProGuard.java:492) Caused by: java.io.IOException: Can't process class [android/app/ActivityTracker.class] (Unsupported class version number [52.0] (maximum 51.0, Java 1.7)) at proguard.io.ClassReader.read(ClassReader.java:112) at proguard.io.FilteredDataEntryReader.read(FilteredDataEntryReader.java:87) at proguard.io.JarReader.read(JarReader.java:65) at proguard.io.DirectoryPump.readFiles(DirectoryPump.java:65) at proguard.io.DirectoryPump.pumpDataEntries(DirectoryPump.java:53) at proguard.InputReader.readInput(InputReader.java:226) ... 6 more Caused by: java.lang.UnsupportedOperationException: Unsupported class version number [52.0] (maximum 51.0, Java 1.7) at proguard.classfile.util.ClassUtil.checkVersionNumbers(ClassUtil.java:140) at proguard.classfile.io.ProgramClassReader.visitProgramClass(ProgramClassReader.java:88) at proguard.classfile.ProgramClass.accept(ProgramClass.java:346) at proguard.io.ClassReader.read(ClassReader.java:91) ... 11 more

9 Warning(s) 1 Error(s)


Solution 1:

You need to update the default Android SDK proguard.jar with the latest version of Proguard found here:

https://sourceforge.net/projects/proguard/files/

I would recommend that you install this on the side of the default version that Android ships in android-sdk\tools\proguard. Simply rename the existing folder to something else and add the new version of proguard.

This is listed in the Xamarin.Android 7.0 release notes:

https://developer.xamarin.com/releases/android/xamarin.android_7/xamarin.android_7.0/

In which the following bug was logged as well:

https://bugzilla.xamarin.com/show_bug.cgi?id=44187

Which is coordinated with a Pull Request in the Xamarin.Android repository:

https://github.com/xamarin/xamarin-android/pull/209

Solution 2:

I had a problem where Proguard was removing the Google Play Services libraries from my app.

In addition to following @Jon Douglas' answer above, I had to add the following lines of text to the proguard-android.txt file found in this folder:

/(Path to your Android SDK Folder)/tools/proguard

-keep public class com.google.android.gms.* { public *; }
-dontwarn com.google.android.gms.**

Complete proguard-android.txt File

-keep public class com.google.android.gms.* { public *; }
-dontwarn com.google.android.gms.**

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keepclassmembers class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}