ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names. Finally, it preverifies the processed code for Java 6 or higher, or for Java Micro Edition.
ProGuard's main advantage compared to other Java obfuscators is probably its compact template- based configuration. A few intuitive command line options or a simple configuration file are usually sufficient. The user manual explains all available options and shows examples of this powerful configuration style.
ProGuard特点是免费,可以压缩,优化和混淆java代码。最重要的一点应该是混淆代码,防止代码轻易被反编译。作者是Eric P.F. Lafortune,比利时人,主要从事计算机图形学研究。从2002年开始发布ProGuard,一直维护到现在,最新版本是ProGuard5.2 下载连接。ProGuard总体上还是比较简单的,提供了一个GUI简单界面,如果代码相对简单,按照默认操作就可以混淆了,但是对于稍微复杂一点的项目,还是需要手动配置proguard.conf。官网上ProGuard Troubleshooting页面有一些常见问题的回答,同时作者也经常在sourceforge.net上回答相关的问题。<!-- 使用5.2版本来混淆 -->
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>5.2</version>
<scope>runtime</scope>
</dependency>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.10</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<proguardVersion>5.2</proguardVersion>
<obfuscate>true</obfuscate>
<proguardInclude>${basedir}/proguard.conf</proguardInclude>
<!-- 添加依赖,这里你可以按你的需要修改 -->
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>${basedir}/lib</lib>
</libs>
<addMavenDescriptor>false</addMavenDescriptor>
<!-- <injar>${project.build.finalName}.war</injar>
<libraryjars>${basedir}/src/main/radi.jar</libraryjars>
<outjar>${project.build.finalName}-pg.war</outjar> -->
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
二是
添加proguard.conf文件,放到pom.xml目录下就可以。
# ----------------------------------
# 通过指定数量的优化能执行
# -optimizationpasses n
# ----------------------------------
-dontoptimize
# ----------------------------------
# 混淆时不会产生形形色色的类名
# -dontusemixedcaseclassnames
# ----------------------------------
#-dontusemixedcaseclassnames
# ----------------------------------
# 指定不去忽略非公共的库类
# -dontskipnonpubliclibraryclasses
# ----------------------------------
-dontskipnonpubliclibraryclasses
# ----------------------------------
# 不预校验
# -dontpreverify
# ----------------------------------
#-dontpreverify
# ----------------------------------
# 输出生成信息
# -verbose
# ----------------------------------
-verbose
#不缩减代码
-dontshrink
#混淆时应用侵入式重载
-overloadaggressively
#优化时允许访问并修改有修饰符的类和类的成员
-allowaccessmodification
#确定统一的混淆类的成员名称来增加混淆
-useuniqueclassmembernames
-injars D:/progrs/DOMSGS/OrbitService/target/OrbitService.war(!WEB-INF/classes/**)
-outjars D:/progrs/DOMSGS/OrbitService/target/OrbitService6pg.war
-dontnote
-ignorewarnings
-dontwarn net.landwind.hip.console.**
#消除警告 Warning: can't find superclass or interface
#Warning: can't find referenced class
-dontwarn org.springframework.**
-keep class org.springframework.** { *;}
-dontwarn org.codehaus.**
-keep class org.codehaus.** { *;}
-dontwarn org.apache.**
-keep class org.apache.** { *;}
# obfuscates the package names further
#-flattenpackagehierarchy 'myobfuscated'
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
#这里添加你不需要混淆的类
-dontwarn radi.domsgs.mvc.**
-keep class radi.domsgs.mvc.endpoint.** { *;}
#-keep class radi.domsgs.mvc.service.** { *;}
-keep class radi.domsgs.mvc.controller.** { *;}
#保留类及其所有方法 成员
#-keepclasseswithmembers class radi.domsgs.mvc.endpoint.AccessEndpoint {
# <fields>;#匹配所有成员
# <methods>;#匹配所有方法
#}
# ---------保护所有实体中的字段名称----------
-keepclassmembers class * implements java.io.Serializable {
<fields>;
}
-keepclassmembers class **.R$* {
public static <fields>;
}
# --------- 保护类中的所有方法名 ------------
-keepclassmembers class * {
public <methods>;
}
-keepclasseswithmembernames,includedescriptorclasses class * {
native <methods>;
}
# Preserve the special static methods that are required in all enumeration classes.
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-printmapping out.map
-renamesourcefileattribute SourceFile
-keepattributes Exceptions,SourceFile