使用proguard混淆java9代碼

本文主要研究一下如何使用proguard混淆java9代碼html

maven

<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.0.1</proguardVersion>
                    <injar>${project.build.finalName}.jar</injar>
                    <outjar>${project.build.finalName}.jar</outjar>
                    <inFilter>!META-INF/maven/**,!module-info.class</inFilter>
                    <obfuscate>true</obfuscate>
                    <proguardInclude>${project.basedir}/proguard.cfg</proguardInclude>
                    <libs>
                        <lib>${java.home}/jmods/java.base.jmod(!**.jar;!module-info.class)</lib>
                    </libs>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>net.sf.proguard</groupId>
                        <artifactId>proguard-base</artifactId>
                        <version>6.0.1</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
            </plugin>
這裏使用6.0.1版本的proguard-base

proguard.cfg

-target 9
-dontshrink
-dontoptimize
-useuniqueclassmembernames
-adaptclassstrings
-dontusemixedcaseclassnames
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
-keepclasseswithmembers public class * { public static void main(java.lang.String[]);}
這裏target要指定爲9版本

問題

duplicate class definitions

[proguard] Warning: class [META-INF/versions/9/org/apache/logging/log4j/util/ProcessIdUtil.class] unexpectedly contains class [org.apache.logging.log4j.util.ProcessIdUtil]
 [proguard] Reading library jar [/Users/demo/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.10.0/log4j-to-slf4j-2.10.0.jar]
 [proguard] Reading library jar [/Users/demo/.m2/repository/org/apache/logging/log4j/log4j-api/2.10.0/log4j-api-2.10.0.jar]
 [proguard] Note: duplicate definition of library class [org.apache.logging.log4j.util.ProcessIdUtil]
 [proguard] Note: duplicate definition of library class [org.apache.logging.log4j.util.StackLocator]
 [proguard] Reading library jar [/Users/demo/.m2/repository/org/slf4j/jul-to-slf4j/1.7.25/jul-to-slf4j-1.7.25.jar]
 [proguard] Reading library jar [/Users/demo/.m2/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar]
 [proguard] Warning: class [META-INF/versions/9/org/apache/logging/log4j/util/StackLocator.class] unexpectedly contains class [org.apache.logging.log4j.util.StackLocator]
 
[proguard] Note: there were 2 duplicate class definitions.
 [proguard]       (http://proguard.sourceforge.net/manual/troubleshooting.html#duplicateclass)
 [proguard] Warning: there were 2 classes in incorrectly named files.
 [proguard]          You should make sure all file names correspond to their class names.
 [proguard]          The directory hierarchies must correspond to the package hierarchies.
 [proguard]          (http://proguard.sourceforge.net/manual/troubleshooting.html#unexpectedclass)
 [proguard]          If you don't mind the mentioned classes not being written out,
 [proguard]          you could try your luck using the '-ignorewarnings' option.
 [proguard] Error: Please correct the above warnings first.
若是沒有使用到log4j的話,能夠在progurard.cfg文件中配置dontwarn忽略

can't find referenced class

[proguard] Warning: cn.example.Demo: can't find referenced class java.io.ByteArrayOutputStream
 [proguard] Warning: there were 858 unresolved references to classes or interfaces.
 [proguard]          You may need to add missing library jars or update their versions.
 [proguard]          If your code works fine without the missing classes, you can suppress
 [proguard]          the warnings with '-dontwarn' options.
 [proguard]          (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedclass)
 [proguard] Warning: there were 1 unresolved references to library class members.
 [proguard]          You probably need to update the library versions.
 [proguard]          (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedlibraryclassmember)
 [proguard] Error: Please correct the above warnings first.
這種多半是沒有配置好libraryjars的問題,好比這裏就是沒有配置java.base.jmod的問題。若是還依賴有其餘jmod,能夠根據具體日誌修改配置。

Can't read java.base.jmod

[proguard] Reading library directory [/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jmods/java.base.jmod(!**.jar;!module-info.class)]
 [proguard] Error: Can't read [/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jmods/java.base.jmod(!**.jar;!module-info.class)] (No such file or directory: /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jmods/java.base.jmod(!**.jar;!module-info.class))
這個是在maven的pom文件配置lib引發的
<libs>
                        <lib>${java.home}/jmods/java.base.jmod(!**.jar;!module-info.class)</lib>
                    </libs>
多是該plugin的問題,將其配置移到proguard.cfg就能夠
-libraryjars <java.home>/jmods/java.base.jmod(!.jar;!module-info.class)

小結

這裏的例子僅僅仍是jdk是模塊化的,可是工程代碼尚未模塊化。等全部依賴都模塊化了,能夠從新試驗一下。java

doc

相關文章
相關標籤/搜索