Log4j 和 Proguard 混淆

问题描述 投票:0回答:1

我试图通过 MAven Proguard 插件混淆我的代码,它几乎完美地工作:运行混淆的 jar 时,我收到有关 log4j 的警告和错误。结果是我根本没有日志(但是程序运行正确)。

这是我的 pom.xml Proguard 部分:

<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>
        <obfuscate>true</obfuscate>
        <injar>${project.build.finalName}-jar-with-dependencies.jar</injar>
        <outjar>${project.build.finalName}-small.jar</outjar>
        <includedependency>true</includedependency>
        <options>
            <option>-keep public class my.package.MyMainClass { *; }</option>
            <option>-ignorewarnings</option>
            <option>-keepclassmembers class * extends java.lang.Enum { *; }</option>
            <option>-keep class org.apache.logging.log4j.** { *; }</option>
            <option>-keep class org.apache.log4j.** { *; }</option>
            <option>-keepattributes *Annotation*</option>
            <option>-keep class org.codehaus.groovy.jsr223.** { *; }</option>
            <option>-adaptresourcefilenames</option>
            <option>-repackageclasses</option>
            <option>-overloadaggressively</option>
            <option>-allowaccessmodification</option>
        </options>
        <libs>
            <lib>${java.home}/lib/rt.jar</lib>
            <lib>${java.home}/lib/jce.jar</lib>
        </libs>
        <archive>
            <manifest>
                <mainclass>MyMainClass</mainclass>
                <packagename>my.package</packagename>
            </manifest>
        </archive>
    </configuration>
    <dependencies>
        <dependency>
            <groupid>net.sf.proguard</groupid>
            <artifactid>proguard-base</artifactid>
            <version>4.10</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</plugin>

当运行我的混淆 jar 时,我收到如下错误和警告:

main WARN Found a TypeConverter [org.apache.logging.log4j.core.config.plugins.convert.TypeConverters$BigIntegerConverter@14c5515] for type [void] that already exists. (this warning for all Converters)

main ERROR Unable to inject fields into builder class for plugin type class org.apache.logging.log4j.core.layout.PatternLayout, element PatternLayout.

main ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.filter.ThresholdFilter for element ThresholdFilter.

main ERROR Null object returned for ThresholdFilter in Filters.

main ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger for element Root.

main ERROR Null object returned for Logger in Loggers.

main ERROR Null object returned for Root in Loggers.

main WARN No Root logger was configured, creating default ERROR-level Root logger with Console appender

知道为什么我无法登录吗? 非常感谢你

maven log4j proguard
1个回答
0
投票

我面临着同样的挑战,添加以下 Proguard 规则后一切都有效。

# ---- Log4j2 Specific Rules ----
# Keep Log4j2 classes
-keep class org.apache.logging.log4j.** { *; }

# Prevent obfuscation of Log4j2 plugin system
-keep class * extends org.apache.logging.log4j.core.config.plugins.util.PluginRegistry { *; }

# Retain Log4j2 annotations
-keepattributes *Annotation*

# Retain META-INF/services for Log4j2 plugin discovery
-keepdirectories META-INF/services/**


# Retain Log4j2 format specifier and runtime methods
-keepclassmembers class * {
    void append*(...);
    java.lang.String format*(...);
}
© www.soinside.com 2019 - 2024. All rights reserved.