首页 > 解决方案 > Proguard 保留选项来处理 OSGi 声明式服务

问题描述

什么是用于 OSGi 声明式服务与 Proguard 混淆的 keep 选项

参考下面的例子,我需要保留DS相关功能而不用Proguard删除它,因为它找不到参考

@Component
public class RandomApp {

    private RandomGenApi randomGenApi;

    @Reference
    public void setRandomGenService(RandomGenApi randomGenApi) {
        this.randomGenApi = randomGenApi;
    }
      
    
    @Activate
    void startRandomApp() {
        System.out.println("Startig RandomApp...");          

    }

标签: osgiproguardosgi-bundle

解决方案


我可以通过将 OSGi 服务定义为入口点来实现这一点。这是要定义的保留选项

#Keep annotations.
-keepattributes *Annotation*

#Keep all Component classes
-keep @org.osgi.service.component.annotations.Component class *

#Kepp all Component classes member functions with OSGi specific annotations
-keepclassmembers @org.osgi.service.component.annotations.Component class * {
   #Keep all methods with annotatios Reference.
   @org.osgi.service.component.annotations.Reference *;
   #Keep all methods with annotatios Activate.
   @org.osgi.service.component.annotations.Activate *;   
}

推荐阅读