Obfuscating clojure uberjars with ProGuard

后端 未结 1 1668
隐瞒了意图╮
隐瞒了意图╮ 2021-02-01 09:13

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\'

相关标签:
1条回答
  • 2021-02-01 09:53

    Copied from above:

    Obfuscating uberjars

    1. Preparing your project.clj file

    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
      )
    

    There isn't much more to it here... Also make sure that (:gen-class) is included in your namespace declarations.

    Build the uberjar with lein uberjar and we are off to the next step.

    2. Preparing your ProGuard config file

    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[]);
    }
    

    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).

    0 讨论(0)
提交回复
热议问题