Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Proguard with a library that uses Spring @Autowired by name

I'm using Proguard to obfuscate a library that has several @Autowired fields. The obfuscator is renaming those class fields (because they are private/internal to the class) and thus my bean is failing to instantiate.

Pre-obfuscated:

@Service
public class LicenseServiceImpl implements LicenseService {

    @Autowired(required = false)
    LicenseSessionStore licenseSessionStore;

    @Autowired(required = false)
    LicenseStore licenseStore;

...
}

Post-obfuscation:

@Service
public class LicenseServiceImpl implements LicenseService {

  @Autowired(required=false)
  LicenseSessionStore a;

  @Autowired(required=false)
  LicenseStore b;

...
}

Now there are likely a lot of ways to make these particular fields not get autowired but what I was hoping to find was a way to tell Proguard to not obfuscate any internal fields that are annotated with important Spring-isms (@Autowired, etc.).

Anyone have an idea on how I can generically do this?

Grant

like image 828
Grant Cermak Avatar asked Dec 09 '10 15:12

Grant Cermak


People also ask

Does ProGuard obfuscate package name?

By default, ProGuard obfuscates the code: it assigns new short random names to classes and class members. It removes internal attributes that are only useful for debugging, such as source files names, variable names, and line numbers.

What is the use of@ Autowired annotation?

The @Autowired annotation can be used to autowire bean on the setter method just like @Required annotation, constructor, a property or methods with arbitrary names and/or multiple arguments.

What is Spring@ Autowired?

Enabling @Autowired AnnotationsThe Spring framework enables automatic dependency injection. In other words, by declaring all the bean dependencies in a Spring configuration file, Spring container can autowire relationships between collaborating beans. This is called Spring bean autowiring.


2 Answers

I had a similar problem retaining class names for simplexml annotated classes. My fix was to add the following:

-keepclassmembers class * {
    @org.simpleframework.xml.* *;
}

I think something similar would work for you:

-keepclassmembers class * {
    @org.springframework.beans.factory.annotation.* *;
} 
like image 81
Jake Hall Avatar answered Sep 29 '22 21:09

Jake Hall


I do not consider this a correct answer to my question and would still like an elegant, generic solution to this problem. What I'm posting is my temporary workaround which got me past my issue in the most brute force, inelegant way.

I explicitly excluded these items from obfuscation by adding them into a keepclassmembernames option:

<option>-keepclassmembernames class * {com.common.license.LicenseSessionStore licenseSessionStore; com.common.license.LicenseStore licenseStore; }</option>

This is not a preferred solution as it requires specific named overriding in each and every class and will become a maintenance nightmare.

A better answer is still needed!

Grant

like image 23
Grant Cermak Avatar answered Sep 29 '22 21:09

Grant Cermak