本文介绍了防止混淆在Maven模块之间共享的包私有成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用com.pyx4me.proguard-maven-plugin混淆多模块项目中的代码.如何从另一个(混淆的)模块中混淆依赖于朋友"方法的模块?我希望保留基本模块中的所有内容,但在从属模块中尽可能缩小/优化.我尝试了各种保持*","dontoptimize"和"dontshrink"选项,但似乎无济于事.来自 https://groups的交叉发布. google.com/forum/#!topic/pyx4me-users/1auM9xSQkLY .

I'm using the com.pyx4me.proguard-maven-plugin to obfuscate code in a multi-module project. How can I obfuscate a module that depends upon 'friend' methods from a different (obfuscated) module? I wish to keep everything from the base module but shrink/optimize as much as possible in the dependent module. I've tried a variety of 'keep*', 'dontoptimize' and 'dontshrink' options but nothing seems to help. Cross-posting from https://groups.google.com/forum/#!topic/pyx4me-users/1auM9xSQkLY.

以上项目结构产生以下错误:

The above project structure yields the following error:

[proguard] Warning: test.Bar: can't find referenced method 'void x()' in class test.Foo
[proguard] Warning: there were 1 unresolved references to program class members.
[proguard]          Your input classes appear to be inconsistent.

/testproj/pom.xml

<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>group</groupId>
    <artifactId>testproj</artifactId>
    <version>1.0.0.0</version>
    <packaging>pom</packaging>

    <name>testproj</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <modules>
        <module>testproj.a</module>
        <module>testproj.b</module>
    </modules>
</project>

/testproj/testproj.a/pom.xml

<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>group</groupId>
    <artifactId>testproj.a</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>


    <name>testproj.a</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <classpathPrefix>lib/</classpathPrefix>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.holliswaite.swmf.FontTableModel</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>true</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </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>
                    <options>
                        <option>-keep class * {*;}</option>
                    </options>
                    <libs>
                        <lib>${java.home}/lib/rt.jar</lib>
                        <lib>${java.home}/lib/jsse.jar</lib>
                    </libs>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>net.sf.proguard</groupId>
                        <artifactId>proguard</artifactId>
                        <version>4.8</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
            </plugin>


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

/testproj/testproj.b/pom.xml

<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>group</groupId>
    <artifactId>testproj.b</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>testproj.b</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>testproj.a</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <classpathPrefix>lib/</classpathPrefix>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.holliswaite.swmf.SWMFFrame</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>true</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </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>
                    <options>
                        <option>-allowaccessmodification</option>
                        <option>-keep public class test.Bar {*;}</option>
                    </options>
                    <libs>
                        <lib>${java.home}/lib/rt.jar</lib>
                        <lib>${java.home}/lib/jsse.jar</lib>
                    </libs>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>net.sf.proguard</groupId>
                        <artifactId>proguard</artifactId>
                        <version>4.8</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

/testproj/testproj.a/src/main/java/test/Foo.java

package test;

public class Foo {
    void x() {}
    public void y() {}
}

/testproj/testproj.b/src/main/java/test/Bar.java

package test;

public class Bar {void z() {(new Foo()).x();}}

y()的引用可以正常工作,但是调用x()会导致错误.

References to y() work ok but calling x() results in errors.

推荐答案

使用-dontskipnonpubliclibraryclassmembers选项(在testproj.b/pom.xml中).

Use -dontskipnonpubliclibraryclassmembers option (in testproj.b/pom.xml).

这篇关于防止混淆在Maven模块之间共享的包私有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 20:08