IDEA 使用JDK1.8使用ProGuard混淆Maven项目代码

这里面的坑实在是太多了,整这个东西整到心态爆炸,要不是公司要求搞这个东西,我都想放弃了!最终在我的坚持下完成了这个混淆。

前面的一些小坑:

坑1:proguard-maven-plugin下载非常慢,以及跑mvn clean package下载非常慢。

解决方法:在C:\Users\${用户名}\.m2文件夹下创建settings.xml,然后填入:

          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

   

        

            alimaven

            aliyun maven

            http://maven.aliyun.com/nexus/content/groups/public/

            central

       

       

            maven.net.cn

            oneof the central mirrors in china

            http://maven.net.cn/content/groups/public/

            central

       

   

使用阿里云的Maven库即可。

 

坑2:maven unmappable character for encoding GBK

Maven用GBK去编译UTF-8编码的Java文件。

解决方法:在pom.xml的project标签下增加:

   

                UTF-8

                UTF-8

   

 

如果没用呢,就在project下建立build标签(如果存在请忽略)、建立一个plugins标签(如果存在请忽略),加上一个plugin:

   

                                        org.apache.maven.plugins

                                        maven-compiler-plugin

                                       

                                                    1.8

                                                    1.8

                                                    UTF-8

                                       

   

 

 

然后IDEA右下角Import Changes即可。

坑3:IDEA Maven Lifecycle install出现Perhaps you are running on a JRE rather than a JDK

解决方法:老老实实配置JAVA_HOME,配置Java环境变量,IDEA虽然不需要配置,但是Maven需要。

参考:https://blog.csdn.net/nobb111/article/details/77116259

配置完:

IDEA 使用JDK1.8使用ProGuard混淆Maven项目代码_第1张图片

path:

IDEA 使用JDK1.8使用ProGuard混淆Maven项目代码_第2张图片

配置完后最好重启IDEA

 

坑4:IDEA Maven Lifecycle install测试类报错

由于项目有其他同事使用Eclipse提交的代码,IDEA就会出现报'\ufeff'错误,解决方法当然也有:https://blog.csdn.net/DCTANT/article/details/81708393

当然还是跳过比较方便,在pom.xml中添加一个plugin:

                                        org.apache.maven.plugins

                                        maven-surefire-plugin

                                       

                                                    true

                                       

                           

添加之后Maven Install就会跳过测试类了。

 

进入正题,IDEA Maven配置ProGuard。此处是参考了:https://blog.csdn.net/sinat_35100573/article/details/51833294,这篇博客给我了非常大的帮助,但是还是过时了,不能使用了,这里列出我的配置文件:

                                        com.github.wvengen

                                        proguard-maven-plugin

                                        2.0.14

                                       

                                                   

                                                               

                                                                package

                                                               

                                                                           

                                                                            proguard

                                                               

                                                   

                                       

                                       

 

                                                    6.0.2

                                                    true

                                                   

                                                    true

                                                   

                                                    true

                                                   

                                                    pg

                                                   

                                                               

                                                               

                                                               

                                                               

                                                               

                                                               

                                                               

                                                               

                                                               

                                                               

                                                               

                                                               

                                                               

                                                               

                                                               

                                                               

                                                               

                                                               

                                                               

 

                                                               

                                                                            public protected *;

                                                                            }

 

                                                               

                                                               

                                                   

                                                    ${project.build.finalName}-pg.jar

                                                   

                                                   

                                                                ${java.home}/lib/rt.jar

                                                                ${java.home}/lib/jce.jar

                                                                ${java.home}/lib/security/local_policy.jar

                                                                ${java.home}/lib/security/US_export_policy.jar

                                                                ${java.home}/lib/ext/sunjce_provider.jar

                                                   

                                                   

                                                   

                                                    com/chisalsoft/wordsdk/**

                                                   

                                                    classes

                                                   

                                                    ${project.build.directory}

                                       

                                       

                                                   

                                                   

                                                                net.sf.proguard

                                                                proguard-base

                                                                6.0.2

                                                                runtime

                                                   

                                       

                           

 

在IDEA的terminal中输入:mvn clean package即可开始编译和混淆。

 

 

各种让人炸毛的坑(基于https://blog.csdn.net/sinat_35100573/article/details/51833294的坑):

最常报的就是MojoExecutionException,Obfuscation failed (result=1) -> [Help 1],也不说清楚是什么原因导致的问题。其中有篇博客说加入-Dproguard.skip=true,那就是骗自己的一个方法,直接跳过混淆,以至于根本无法混淆代码!!所以根本没用!!

Maven提示说输入-e -X开启详细的报错信息。那就这么做:在terminal中输入mvn clean package -e -X,这回详细的报错信息出来了:

 

提示(Unsupported class version number [53.0] (maximum 52.0, Java 1.8)),明显ProGuard不支持1.8,搜了很久搜到了https://blog.csdn.net/testcs_dn/article/details/45622331,这篇博客,其中为ProGuard指定了一个版本,但是!这个5.2版本也不支持Java1.8,然后我只能去ProGuard官网找新版本,我找的是6.0.2版本,算是非常新的了,加上后这个错误终于是不报了,但是新错误就来了:

 

Note: there were 1 duplicate class definitions.然后是一个Warning:

there were 2 classes in incorrectly named files.这个错误命名的classes有时候不止2个,网上我看到有报1000多个的,而这些Warning通常来自各个引入的jar包,包括Java自己的rt.jar,我一怒之下把ProGuard Plugin中的所有lib都注释掉了,结果后面又导致了新的Error。

这个ProGuard最变态的就是必须要你解决掉所有Warning才给你继续混淆,不然就给你报个Error!然后编译就无法继续下去了。

解决方法:在各种option标签中增加:,作用是拒绝提示任何Warning,以至于ProGuard想报都报不了,顺利通过编译。

 

新的错误又来了,这是由于我把plugin标签中的所有lib都注释导致的,找不到父类了!以至于ArrayList这样类都因为找不到源文件而报错。

解决方法:把rt.jar包加进去。

 

然后新的错误又来了,Error: You have to specify '-keep' options for the shrinking step.这个是因为没有加上-keep参数,这个参数是用来保留文件的,如果一个-keep都没有,那么将会生成一个空的jar包,所以就报错了。

解决方法:加入

                                                                            public protected *;

                                                                            }

 

 

最终……终于解决了……顺利编译,生成了一个叫classes-pg.jar的一个jar包,解压后用IDEA反编译打开,发现代码已经混淆好了,终于成功了……

截个图表示纪念:

IDEA 使用JDK1.8使用ProGuard混淆Maven项目代码_第3张图片

当然这些令人炸毛的坑都不用你们踩了,我在上面的配置清单中都加上了,应该不会有什么问题了。

 

 

 

 

 

 

你可能感兴趣的:(idea,java,ProGuard,maven,混淆)