声明:本项目并非安卓(android)项目,只是springboot项目或者普通的java项目,maven进行依赖管理。
使用插件:proguard
官方帮助文档
maven依赖:
<!-- https://mvnrepository.com/artifact/com.github.wvengen/proguard-maven-plugin -->
<dependency>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.5.3</version>
</dependency>
主要想记录的是:
java多模块如何进行代码混淆?
方法一:
- 将父模块编译出来形成jar包,
- 用maven assembly插件将子模块和父模块的jar包打包成一个jar包,assembly插件输出格式(format)设置为jar,或者将产出设置为一个jar包,设置
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
- 再用proguard进行代码混淆
此方法maven插件执行顺序为 编译-》assembly-》proguard
方法二:
proguard有assembly标签,可以包含自己依赖的模块,这种方式简单,果断使用第二种。
<assembly>
<inclusions>
<!--依赖的子模块,打包在一起形成一个jar进行代码混淆-->
<inclusion>
<groupId>com.依赖的模块groupId</groupId>
<artifactId>模块artifactId</artifactId>
</inclusion>
</inclusions>
</assembly>
给出springboot 项目的proguard 代码混合配置:
<plugins>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<proguardVersion>6.2.2</proguardVersion>
<attach>true</attach>
<attachArtifactClassifier>pg</attachArtifactClassifier>
<obfuscate>true</obfuscate>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>${java.home}/lib/jsse.jar</lib>
</libs>
<assembly>
<inclusions>
<!--依赖的子模块,打包在一起形成一个jar进行代码混淆-->
<inclusion>
<groupId>com.依赖的子模块</groupId>
<artifactId>子模块</artifactId>
</inclusion>
</inclusions>
</assembly>
<options>
<option>-ignorewarnings</option> <!--ignore all warnings-->
<option>-dontshrink</option> <!-- don't shrink -->
<option>-dontoptimize</option> <!-- don't optimize -->
<!--不混淆包名-->
<option>-keeppackagenames</option>
<!--适配类字符串-->
<option>-adaptclassstrings</option>
<!--保留在输出jar的文件目录-->
<option>-keepdirectories</option>
<!-- 对于类成员的命名的混淆采取唯一策略-->
<option>-useuniqueclassmembernames</option>
<option>-keep class **.package-info</option>
<!-- 不混淆接口 -->
<option>-keepnames interface **</option>
<!--保留那些需要被保留的方法的参数名字-->
<option>-keepparameternames</option>
<!--对异常、注解信息在runtime予以保留,不然影响springboot启动-->
<!-- 不混淆所有特殊的类 -->
<option>-keepattributes
Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethod
</option>
<option>-allowaccessmodification</option>
<!--不混淆带有main方法的类:DsMaster,DsAgent,DsLauncher-->
<option>-keepclasseswithmembers public class * {
public static void main(java.lang.String[]);}
</option>
<!--javax.annotation有问题,就加入以下代码-->
<option>-dontnote javax.annotation.**</option>
<option>-keepclassmembers class * {
@org.springframework.beans.factory.annotation.Autowired *;
@org.springframework.beans.factory.annotation.Value *;
@org.springframework.context.annotation.Bean *;
@org.springframework.beans.factory.annotation.Qualifier *;
@org.springframework.stereotype.Repository *;
@org.springframework.data.repository.NoRepositoryBean *;
@org.springframework.security.config.annotation.web.configuration.EnableWebSecurity *;
@org.springframework.context.annotation.Configuration *;
@org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity *;
@org.springframework.boot.web.servlet.ServletComponentScan *;
@org.springframework.web.bind.annotation.RestController *;
@org.springframework.validation.annotation.Validated *;
@org.springframework.stereotype.Component *;
@org.springframework.context.annotation.EnableAspectJAutoProxy *;
@org.springframework.context.annotation.ComponentScan *;
}
</option>
<!-- ensure valueOf method of enum is useful -->
<option>-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
</option>
<!--不能少,否则功能出问题-->
<option>-keepclassmembernames class * {
void set*(***);
boolean is*();
*** get*();
}
</option>
</options>
</configuration>
<dependencies>
<!-- https://mvnrepository.com/artifact/net.sf.proguard/proguard-base -->
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>6.2.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
另外记录问题:
1、如何保持 native 方法不被混淆
-keepclasseswithmembernames class * {
native ;
}
pom文件中対<号是有特殊含义的,所以用<代替,lt = less than 小于号
<option>-keepclasseswithmembers public class * {native <methods>;}</option>