用ProGuard混淆Clojure Uberjars [英] Obfuscating clojure uberjars with ProGuard
问题描述
我想知道是否有人对使用proguard混淆了他们的leiningen编译过的uberjars有任何经验.我已经尽力在Google上寻找解决方案,但找不到真正的答案.我想知道这是否完全可能.
I was wondering if anybody has any experience with obfuscating their leiningen compiled uberjars with proguard. I've tried my best to look for a solution on Google but couldn't really find an answer. I'm wondering if this is at all possible.
我一直试图混淆默认的lein项目.这是core.clj文件:
I've been trying to obfuscate a default lein project. Here's the core.clj file:
(ns proguard.core
(:gen-class))
(defn -main
"I don't do a whole lot."
[& args]
(println "Hello, World!"))
项目文件:
(defproject proguard "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.4.0"]]
:aot :all
:main proguard.core)
和我的proguard配置文件:
and my proguard config file:
-injars clojure/proguard/target/proguard-0.1.0-SNAPSHOT-standalone.jar
-outjars clojure/test-project
-libraryjars /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/rt.jar
-dontshrink
-dontoptimize
-dontusemixedcaseclassnames
-dontpreverify
-dontnote
-printseeds
-keepclasseswithmembers public class * {
public static void main(java.lang.String[]);
}
-keep class clojure.core__init { public static void load(); }
-keep class clojure.core_proxy__init { public static void load(); }
-keep class clojure.core_print__init { public static void load(); }
-keep class clojure.genclass__init { public static void load(); }
-keep class clojure.core_deftype__init { public static void load(); }
-keep class clojure.core.protocols__init { public static void load(); }
-keep class clojure.gvec__init { public static void load(); }
-keep class clojure.java.io__init { public static void load(); }
-keep class clojure.lang__init { public static void load(); }
-keep class proguard.core__init {
public static void load();
}
-keep class proguard.core {
public *** super*(...);
}
每当我尝试运行混淆的jar时,都会出现以下错误:
Whenever I try to run the obfuscated jar I get the following errors:
Exception in thread "main" java.lang.ExceptionInInitializerError
at clojure.lang.ve.<init>(Unknown Source)
at clojure.lang.ve.c(Unknown Source)
at clojure.lang.yf.a(Unknown Source)
at proguard.core.<clinit>(Unknown Source)
Caused by: java.lang.ClassNotFoundException: clojure.lang.PersistentList, compiling:(clojure/core.clj:20)
at clojure.lang.at.a(Unknown Source)
at clojure.lang.at.b(Unknown Source)
at clojure.lang.at.a(Unknown Source)
at clojure.lang.bj.a(Unknown Source)
at clojure.lang.at.a(Unknown Source)
at clojure.lang.at.b(Unknown Source)
at clojure.lang.at.a(Unknown Source)
at clojure.lang.at.a(Unknown Source)
at clojure.lang.at.a(Unknown Source)
at clojure.lang.xh.a(Unknown Source)
at clojure.lang.xh.a(Unknown Source)
at clojure.lang.xh.b(Unknown Source)
at clojure.lang.xh.d(Unknown Source)
at clojure.lang.xh.c(Unknown Source)
at clojure.lang.xh.<clinit>(Unknown Source)
... 4 more
Caused by: java.lang.ClassNotFoundException: clojure.lang.PersistentList
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at clojure.lang.ec.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at clojure.lang.xh.h(Unknown Source)
at clojure.lang.bp.b(Unknown Source)
at clojure.lang.bp.a(Unknown Source)
at clojure.lang.bq.a(Unknown Source)
... 19 more
我不太确定自己在做什么错...我尝试遵循针对Clojure的Proguard教程
I'm not really sure what I'm doing wrong here... I've tried to follow a clojure targeted proguard tutorial obfuscating with proguard, however it is android and ant specific so I'm wondering if the process is entirely different for desktop applications that use lein.
谢谢.
推荐答案
从上方复制:
混淆uberjars
1.准备您的project.clj文件
这是我的副本(简单的默认lein项目,带有注释):
Here's a copy of mine (simple, default lein project, with comments):
(defproject proguard "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.4.0"]]
:main proguard.core
;;; Make sure everything is aot compiled
:aot :all
;;; Remove source .clj files from the resulting jar
:omit-source true
)
这里没有更多的内容...还要确保(:gen-class)
包含在名称空间声明中.
There isn't much more to it here... Also make sure that (:gen-class)
is included in your namespace declarations.
使用 lein uberjar
构建uberjar,我们进入下一步.
Build the uberjar with lein uberjar
and we are off to the next step.
2.准备您的ProGuard配置文件
再次我的文件副本后面带有注释
Once again a copy of my file follows with annotations
# Our uberjar
-injars clojure/proguard/target/proguard-0.1.0-SNAPSHOT-standalone.jar
# Our output direcotry
-outjars clojure/obfuscated
# Link to rt.jar. I'm on a Mac so your path may differ
-libraryjars /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/rt.jar
# ProGuard options. Detailed explanation here http://proguard.sourceforge.net/index.html#manual/usage.html
-dontskipnonpubliclibraryclassmembers
-dontnote
-printseeds
# What we will be doing is obfuscating, shrinking and optimizing the jar.
# If you experience any problems start out with obfuscation and add the
# -dontoptimize and the -dontshrink flags and see if it works.
# Tell proguard to leave the clojure runtime alone
# You would need to add any other classes that you wish to preserve here.
-keep class clojure.** { *; }
# Keep our core__init class
-keep class proguard.core__init {
public static void load();
}
# Keep classes that contain a main method (otherwise we won't be able to run the jar)
-keepclasseswithmembers public class * {
public static void main(java.lang.String[]);
}
就这样.现在,使用新的配置文件 java -jar proguard.jar @ myconfig.pro
运行proguard.由于 -printseeds
标志,您应该看到一堆输出(如果您不希望看到proguard将保留哪些类,当然可以删除该标志).
Thats it. Now run proguard with your new configuration file java -jar proguard.jar @myconfig.pro
. You should see a bunch of output due to the -printseeds
flag (which you of course can remove if you don't want to see which classes will be kept by proguard).
这篇关于用ProGuard混淆Clojure Uberjars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!