45

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)

Eino Gourdin
  • 4,169
  • 3
  • 39
  • 67

6 Answers6

115

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

Jon Douglas
  • 13,006
  • 4
  • 38
  • 51
  • 3
    This works for me, for the details I replace the Android `tools/proguard/lib` folder by the `lib` folder inside the latest Proguard release from sourceforge (5.3.2 at this time). – Guillaume Perrot Jan 21 '17 at 01:23
  • 14
    If you replace the whole proguard directory, make sure to copy the config files from the old directory: proguard-android-optimize.txt, proguard-android.txt, proguard-project.txt. – rooby Jan 24 '17 at 01:11
  • 1
    The most fantastic thing is that it really helps even 2 years ago ) Thx man, you've saved my day. – Darkmike Mar 16 '17 at 12:28
  • this solution works even if you have the latest version. sometimes you may get the error, just redownload and replace with the existing. I dont know why but it works like that – Emil May 19 '17 at 00:51
  • Thank you very much @GuillaumePerrot Turns out replacing the whole proguard folder can not fix the problem but replacing the proguard/lib can. – Chandler Apr 26 '18 at 01:20
4

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>(...);
}
Community
  • 1
  • 1
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
3

Simple Answer

According to documentation you need update proguard version (starting from version 5 it support Java 8 ) and make sure you are using jdk1.8

JDK 1.8 - up to API 24+

JDK 1.7 - up to API 23

JDK 1.6 - up to API 20

how to update from package console write this command

PM> Install-Package name.atsushieno.proguard.facebook 

https://www.nuget.org/packages/name.atsushieno.proguard.facebook

or download proguard manually and replace it with current version

YOUR_SDK --> tools --> proguard --> (delete all old files , unzip new version )

Detailed Answer if you enable target android Nogut and you enable Multi-Dex or ProGuard you need use proper ProGuard version (as ToolMakerSteve said they have dependency on ProGuard) so make sure you target java 8 from tools ->options ->xamarin ->java and update ProGuard version

Update Answer

make sure your support library version compatible with target version , if you target

Android 7 (api 24) mean your support library should be 24

Android 7.1 (api 25 ) mean your support library should be 25

to all support library and if you using google play services as well

Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
2

I was also facing the same issue on my Windows 10 x64 and visual studio 2015. I have set the JDK path to JDK 1.8 under android settings in Visual studio tools ->options ->xamarin ->java development kit location. it was pointing to "C:\Program Files (x86)\Java\jdk1.7.0_55" and I made it to "C:\Program Files\Java\jdk1.8.0_31", and issue has been fixed.

  • Does changing the path fixed this issue?? – Femil Shajin Nov 26 '16 at 20:11
  • No it does not work for me, whether I use Java 7 or Java 8 in VS/Xamarin Studio I get the same error. – Guillaume Perrot Jan 20 '17 at 22:13
  • This does **not fix** the problem, if using **ProGuard or Multi-Dex**, because those depend on a tool inside android sdk/tools/proguard, as described in Jon Douglas answer. On the other hand, it **should fix** the problem for anyone **not using either ProGuard or Multi-Dex**, if they are getting this error trying to use Nougat [API 24], or some package/library that depends on an API 24 feature. – ToolmakerSteve Feb 08 '17 at 22:24
1

Using Visual Studio

If you are using Visual Studio 2017 or 2019, please note that these applications pack their proguard. For 2019, proguard version: 5.3.2. For me, I needed version 6.0.3, so I made a backup of the proguard.jar file at:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Xamarin\Android\proguard\lib

And copied the latest one from zip from the sourceforge site.

James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78
0

To get ProGuard work, you have to follow few steps

  • First Download latest ProGuard file from here, unzip and paste in your android-sdk\tools\proguard folder
  • First you need to add proguard.cfg to your android project level.
  • Change proguard.cfg file build action to ProguardConfiguration Change
  • proguard.cfg file Encoding using Notepad++ to UTF-8

If you are getting warnings then you need to use -dontwarn attribute like below

-dontwarn com.symbol.emdk.**
-dontwarn org.apache.http.**
-dontwarn com.appdynamics.eumagent.runtime.**

You must need to remove/solve warning because that cause errors. Above code will remove all of your warnings.

If you are getting exception after executing your application you need to use -keep attribute. like below for required classes

-keep public class android.support.v7.widget.** { *; }
R15
  • 13,982
  • 14
  • 97
  • 173