最近尝试用Proguard来混淆代码,以增加发布出去的代码安全性。今天运行混淆后的jar包发生如下异常
1 | Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public abc.service.Service test.rest.controller.Controller.service; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [abc.service.Service] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} |
程序很简单,接口
1 | public interface Service { |
实现类使用@Component标签实例化到spring容器中
1 |
|
在Controller中使用@Autowired标签自动装配
1 |
|
原程序可以正常运行,但在混淆了代码之后,发生以上所述的异常,原因是spring找不到能够匹配的Service实例。
忙活了一整天各种测试各种查资料,终于找到解决方案,很简单
- 在proguard配置中加上“-keepdirectories”
- 保证proguard版本不低于4.4
另外附上解决前和解决后的jar包解压时候的控制台输出
这是没加“-keepdirectories”的输出
1 | inflated: META-INF/MANIFEST.MF |
加上“-keepdirectories”之后解压
1 | created: META-INF/ |