ProGuard 使用 sbt-proguard 说 Unsupported class version number [52.0] (maximum 51.0, Java 1.7)
Posted
技术标签:
【中文标题】ProGuard 使用 sbt-proguard 说 Unsupported class version number [52.0] (maximum 51.0, Java 1.7)【英文标题】:ProGuard says Unsupported class version number [52.0] (maximum 51.0, Java 1.7) with sbt-proguard 【发布时间】:2014-06-03 22:19:28 【问题描述】:我使用的是 Mac OS X 10.9.2 和 sbt 0.13.3-SNAPSHOT(根据源代码构建),Java 8和 sbt-proguard 0.2.2 插件。
sbt 0.13.3-SNAPSHOT
[jacek]> sbtVersion
[info] 0.13.3-SNAPSHOT
Java 8
$ /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/bin/java -version
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
project/plugins.sbt
addSbtPlugin("com.typesafe.sbt" % "sbt-proguard" % "0.2.2")
当我在 sbt shell 中运行 proguard:proguard
时,它爆炸了,出现以下异常:
[sbt-updates]> show proguard:proguard
[info] ProGuard, version 4.9
[info] Reading program directory [/Users/jacek/oss/sbt-updates/target/scala-2.10/sbt-0.13/classes] (filtered)
[info] Reading program jar [/Users/jacek/.ivy2/cache/org.scalaz/scalaz-concurrent_2.10/bundles/scalaz-concurrent_2.10-7.1.0-M6.jar] (filtered)
[info] Reading program jar [/Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-library.jar] (filtered)
[info] Reading program jar [/Users/jacek/.ivy2/cache/org.scalaz/scalaz-core_2.10/bundles/scalaz-core_2.10-7.1.0-M6.jar] (filtered)
[info] Reading program jar [/Users/jacek/.ivy2/cache/org.scalaz/scalaz-effect_2.10/bundles/scalaz-effect_2.10-7.1.0-M6.jar] (filtered)
[info] Reading library jar [/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar]
[error] Error: Can't read [/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar] (Can't process class [apple/applescript/AppleScriptEngine.class] (Unsupported class version number [52.0] (maximum 51.0, Java 1.7)))
[trace] Stack trace suppressed: run last proguard:proguard for the full output.
[error] (proguard:proguard) Proguard failed with exit code [1]
[error] Total time: 16 s, completed Apr 19, 2014 2:27:56 PM
为什么会出现错误?
【问题讨论】:
【参考方案1】:ProGuard 和 sbt-proguard
似乎还不支持 Java 8,因此更改脚本中用于启动 sbt 的 Java 版本有所帮助。
[sbt-updates]> show proguard:proguard
[info] Compiling 8 Scala sources to /Users/jacek/oss/sbt-updates/target/scala-2.10/sbt-0.13/classes...
[warn] there were 6 feature warning(s); re-run with -feature for details
[warn] one warning found
[info] ProGuard, version 4.9
[info] Reading program directory [/Users/jacek/oss/sbt-updates/target/scala-2.10/sbt-0.13/classes] (filtered)
[info] Reading program jar [/Users/jacek/.ivy2/cache/org.scalaz/scalaz-concurrent_2.10/bundles/scalaz-concurrent_2.10-7.1.0-M6.jar] (filtered)
...
这适用于以下版本的 Java 7:
$ /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
【讨论】:
Proguard 确实支持 java 8,但我没有使用 sbt-proguard,我最终在构建工件作为构建过程中的一个阶段之后混淆了 jar。你可以在这里看到我的完整答案:***.com/questions/24670430/…【参考方案2】:这个问题真的很有趣,我遇到了同样的问题,唯一的区别是我使用 maven 以编程方式添加了 Proguard。因此,我认为发布我的解决方案会有所帮助,尽管它与主要问题有点不同。对于所有使用 maven 并面临同样问题的人,我的解决方法是通过将 Proguard 用作插件内的运行时来更新 Proguard 的版本,因此工作的 pom.xml 看起来像这样
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.14</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<obfuscate>true</obfuscate>
<attach>true</attach>
<appendClassifier>false</appendClassifier>
<addMavenDescriptor>true</addMavenDescriptor>
<injar>$project.build.finalName-jar-with-dependencies.jar</injar>
<injarNotExistsSkip>true</injarNotExistsSkip>
<libs>
<lib>$java.home/lib/rt.jar</lib>
<lib>$java.home/lib/jce.jar</lib>
<lib>$java.home/lib/ext/sunjce_provider.jar</lib>
</libs>
<options>
<option>-allowaccessmodification</option>
<option>-optimizationpasses 3</option>
<option>-overloadaggressively</option>
<option>-repackageclasses ''</option>
<option>-dontusemixedcaseclassnames</option>
<option>-dontskipnonpubliclibraryclasses</option>
<option>-flattenpackagehierarch</option>
<option>-dontwarn</option> <!-- added option to ignore com.sun missing classes -->
<option>-keep public class com.StocksNews.App
public static void main(java.lang.String[]);
</option>
</options>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>6.1.1</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
【讨论】:
这是完美的解决方案。【参考方案3】:可以通过将 build.sbt
中的密钥 proguardVersion
更改为高于 5.0 的设置来更新 sbt-proguard 使用的 Proguard 版本,例如
ProguardKeys.proguardVersion in Proguard := "5.2.1"
见:https://github.com/sbt/sbt-proguard/issues/5
【讨论】:
【参考方案4】:我通过从下面的链接更新到最新版本的 ProGuad 解决了这个问题
Sourceforge ProGuard page
【讨论】:
以上是关于ProGuard 使用 sbt-proguard 说 Unsupported class version number [52.0] (maximum 51.0, Java 1.7)的主要内容,如果未能解决你的问题,请参考以下文章
SBT ProGuard 插件:OutOfMemoryError,如何增加堆空间?
使用 Proguard 在 dist 上混淆 Play 2 Web 应用程序?
Proguard - 也使用模块中的 proguard 文件