8

我有一个库 Common.License,我用 Proguard 混淆了它:

<plugin>
    <groupId>com.pyx4me</groupId>
    <artifactId>proguard-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>proguard</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <obfuscate>true</obfuscate>
        <options>
            <option>-dontoptimize</option>
            <option>-renamesourcefileattribute SourceFile</option>
            <option>-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod</option>
            <option>-keep public class * { public protected *;}</option>
            <option>-keepclassmembernames class * {java.lang.Class class$(java.lang.String); java.lang.Class class$(java.lang.String, boolean);}</option>
            <option>-keepclassmembernames class * {com.common.license.LicenseSessionStore licenseSessionStore; com.common.license.LicenseStore licenseStore;}</option>
            <option>-keepclassmembers enum * {public static **[] values(); public static ** valueOf(java.lang.String);}</option>
            <option>-keepclassmembers class * implements java.io.Serializable { static final long serialVersionUID; private static final java.io.ObjectStreamField[] serialPersistentFields; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve();}</option>
        </options>
        <libs>
            <lib>${java.home}/lib/rt.jar</lib>
            <lib>${java.home}/lib/jsse.jar</lib>
        </libs>
        <addMavenDescriptor>false</addMavenDescriptor>
    </configuration>
</plugin>

这个库有一个用 @Service 注释的 Spring bean:

@Service
public class LicenseServiceImpl implements LicenseService {

    @Autowired(required = false)
    LicenseSessionStore licenseSessionStore;

    @Autowired(required = false)
    LicenseStore licenseStore;

...

}

我在网络服务 Company.License 中使用这个库,我希望 LicenseService 自动装配:

@Component
public class BackgroundTasks {

    @Autowired
    ScheduledExecutorService scheduledExecutorService;

    @Autowired
    LicenseService licenseService;

...

}

所以 Company.License 依赖于 Common.License。如果我混淆 Common.License 则licenseService不会自动装配BackgroundTasks. 我可以解决这个问题的唯一方法是将 licenseService 明确定义为 bean:

@Bean(name = "licenseService", autowire = Autowire.BY_NAME)
public LicenseService getLicenseService() {
    if (licenseService == null) {
        licenseService = new LicenseServiceImpl();
    }
    return licenseService;
}

我不需要将其显式声明为这样的 bean,因为我已经注释了@Service应该足以使 Spring 中的自动装配licenseService神奇BackgroundTasks地工作的类。但事实并非如此!

Proguard 专门做什么来使它不起作用?我可以在 Proguard 的配置中做些什么来使它对 Spring 更友好吗?

授予

4

3 回答 3

6

非常感谢 Eric Lafortune 帮助我指出了正确的方向:

http://sourceforge.net/projects/proguard/forums/forum/182456/topic/2547498

这是指定插件和所需选项的工作 pom 文件添加:

<plugin>
    <groupId>com.pyx4me</groupId>
    <artifactId>proguard-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>proguard</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <obfuscate>true</obfuscate>
        <options>
            <option>-dontoptimize</option>
            <option>-keepdirectories</option>
            <option>-renamesourcefileattribute SourceFile</option>
            <option>-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod</option>
            <option>-keep public class * { public protected *;}</option>
            <option>-keepclassmembernames class * {java.lang.Class class$(java.lang.String); java.lang.Class class$(java.lang.String, boolean);}</option>
            <option>-keepclassmembernames class * {com.common.license.LicenseService licenseService; com.common.license.LicenseSessionStore licenseSessionStore;}</option>
            <option>-keepclassmembers enum * {public static **[] values(); public static ** valueOf(java.lang.String);}</option>
            <option>-keepclassmembers class * implements java.io.Serializable { static final long serialVersionUID; private static final java.io.ObjectStreamField[] serialPersistentFields; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve();}</option>
            <option>-keep @org.springframework.beans.factory.annotation.Service class *</option>
            <option>-keep @org.springframework.stereotype.Controller class *</option>
            <option>-keepclassmembers class * { @org.springframework.beans.factory.annotation.Autowired *; }</option>
        </options>
        <libs>
            <lib>${java.home}/lib/rt.jar</lib>
            <lib>${java.home}/lib/jsse.jar</lib>
        </libs>
        <addMavenDescriptor>false</addMavenDescriptor>
    </configuration>
</plugin>

笔记!您需要使用 ProGuard 4.4,最新版本的 ProGuard-Maven-Plugin (2.0.4) 使用 4.3 所以您需要编辑:

{M2_HOME}\repository\com\pyx4me\proguard-maven-plugin\2.0.4\proguard-maven-plugin-2.0.4.pom 

要拥有 4.4 依赖项(像这样):

<dependency>
  <groupId>net.sf.proguard</groupId>
  <artifactId>proguard</artifactId>
  <version>4.4</version>
  <scope>runtime</scope>
  <optional>true</optional>
</dependency>
于 2010-12-13T17:16:03.613 回答
1

这是切换到 proguard 4.4 版的另一种方法(可能会更好一些):

<configuration>
  <proguardVersion>4.4</proguardVersion>
  <obfuscate>true</obfuscate>
  <options>
  ... 
  </options>    
  <libs>
    <lib>${java.home}/lib/rt.jar</lib>
    <lib>${java.home}/lib/jsse.jar</lib>
  </libs>
  <addMavenDescriptor>false</addMavenDescriptor>
</configuration>
<dependencies>
  <dependency>
    <groupId>net.sf.proguard</groupId>
    <artifactId>proguard</artifactId>
    <version>4.4</version>
    <scope>runtime</scope>
    <optional>true</optional>
  </dependency>
</dependencies>
于 2010-12-14T17:42:05.900 回答
0

我遇到了同样的问题,-keepdirectories 指令帮助我解决了这个问题。

于 2018-08-01T21:48:26.857 回答