Java maven 环境下使用proguard-maven-plugin 插件混淆代码

时间:2022-02-26 14:33:27


相关链接

http://blog.csdn.net/zhyhang/article/details/18567409


http://blog.csdn.net/zhangjianying/article/details/7939593?c=127e3ef14a86fd3afef97eac34257db1


http://my.oschina.net/noahxiao/blog/304746?utm_source=tuicool&utm_medium=referral

 maven配置:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yunshouhu</groupId>
<artifactId>ApkProtect</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<java.version>1.7</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.yunshouhu.apkprotect.MainHelper</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.pyx4me</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<obfuscate>true</obfuscate>
<proguardInclude>${basedir}/proguard.conf</proguardInclude>
<!-- 添加依赖,这里你可以按你的需要修改 -->
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>lib/fcexporter_jdk1.5.jar</lib>
<lib>lib/fcexporthandler.jar</lib>
<lib>lib/jsp-api.jar</lib>
<lib>lib/servlet-api.jar</lib>
</libs>
<addMavenDescriptor>false</addMavenDescriptor>
</configuration>
<dependencies>
<!-- 使用4.8版本来混淆 -->
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard</artifactId>
<version>4.4</version>
<scope>runtime</scope>
</dependency>
</dependencies>

</plugin>

</plugins>
</build>
</project>

ok版本

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yunshouhu</groupId>
<artifactId>ApkProtect</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.yunshouhu.apkprotect.MainHelper</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.11</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>

<attach>true</attach>
<attachArtifactClassifier>proguard</attachArtifactClassifier>
<!-- attach 的作用是在 install 与 deploy 时将生成的 pg 文件也安装与部署 -->
<options> <!-- 详细配置方式参考 ProGuard 官方文档 -->
<!--<option>-dontobfuscate</option>-->
<option>-ignorewarnings</option> <!--忽略所有告警-->
<option>-dontshrink</option> <!--不做 shrink -->
<option>-dontoptimize</option> <!--不做 optimize -->
<option>-dontskipnonpubliclibraryclasses</option>
<option>-dontskipnonpubliclibraryclassmembers</option>

<!--不要重构包名
<option>-repackageclasses org.noahx.proguard.example.project2.pg</option>
-->
<!--平行包结构(重构包层次),所有混淆的类放在 pg 包下-->

<!--保护程序入口-->
<option>-keep public class com.yunshouhu.apkprotect.MainHelper { public static void main(java.lang.String[]);}</option>


<!-- 以下为 Keep,哪些内容保持不变,因为有一些内容混淆后(a,b,c)导致反射或按类名字符串相关的操作失效 -->

<option>-keep class **.package-info</option>
<!--保持包注解类-->

<option>-keepattributes Signature</option>
<!--JAXB NEED,具体原因不明,不加会导致 JAXB 出异常,如果不使用 JAXB 根据需要修改-->
<!-- Jaxb requires generics to be available to perform xml parsing and without this option ProGuard was not retaining that information after obfuscation. That was causing the exception above. -->

<option>-keepattributes SourceFile,LineNumberTable,*Annotation*</option>
<!--保持源码名与行号(异常时有明确的栈信息),注解(默认会过滤掉所有注解,会影响框架的注解)-->

<option>-keepclassmembers enum org.noahx.proguard.example.project2.** { *;}</option>
<!--保持枚举中的名子,确保枚举 valueOf 可以使用-->

<option>-keep class org.noahx.proguard.example.project2.bean.** { *;}</option>
<!--保持 Bean 类,(由于很多框架会对 Bean 中的内容做反射处理,请根据自己的业务调整) -->

<option>-keep class org.noahx.proguard.example.project2.Project2 { public void init(); public void
destroy(); }
</option>
<!-- 保持对外的接口性质类对外的类名与方法名不变 -->

</options>
<outjar>${project.build.finalName}-proguard</outjar>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
</libs>

</configuration>
</plugin>

</plugins>
</build>
</project>

proguard.cfg 文件方式配置ok版本

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yunshouhu</groupId>
<artifactId>ApkProtect</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<java.version>1.7</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.yunshouhu.apkprotect.MainHelper</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.11</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<obfuscate>true</obfuscate>
<proguardInclude>${basedir}/proguard.cfg</proguardInclude>
<attach>true</attach>
<attachArtifactClassifier>proguard</attachArtifactClassifier>
</configuration>
</plugin>

</plugins>
</build>
</project>
proguard.cfg文件
 

# ----------------------------------
# 通过指定数量的优化能执行
# -optimizationpasses n
# ----------------------------------
-optimizationpasses 3

# ----------------------------------
# 混淆时不会产生形形色色的类名
# -dontusemixedcaseclassnames
# ----------------------------------
-dontusemixedcaseclassnames
# ----------------------------------
# 指定不去忽略非公共的库类
# -dontskipnonpubliclibraryclasses
# ----------------------------------
#-dontskipnonpubliclibraryclasses

# ----------------------------------
# 不预校验
# -dontpreverify
# ----------------------------------
# -dontpreverify

# ----------------------------------
# 输出生成信息
# -verbose
# ----------------------------------
-verbose

#混淆时应用侵入式重载
-overloadaggressively

#优化时允许访问并修改有修饰符的类和类的成员
-allowaccessmodification
#确定统一的混淆类的成员名称来增加混淆
-useuniqueclassmembernames

-ignorewarnings
-dontshrink
-dontoptimize
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers

-keep public class com.yunshouhu.apkprotect.MainHelper { public static void main(java.lang.String[]);}

-keep class **.package-info
-keepattributes Signature

-keepattributes SourceFile,LineNumberTable,*Annotation*


-keep public class com.yunshouhu.apkprotect.MainHelper {
public static void main(java.lang.String[]);
}

#这里添加你不需要混淆的类

-keep class com.zsoftware.common.cache.** {*;}

-keep public class * extends javax.servlet.Servlet

-keepattributes **


#-keepnames class * implements java.io.Serializable
# ---------保护所有实体中的字段名称----------
-keepclassmembers class * implements java.io.Serializable {
<fields>;
}

# --------- 保护类中的所有方法名 ------------
-keepclassmembers class * {
public <methods>;
}