繁体   English   中英

在 Android 中嵌入 OSGi Felix

[英]Embed OSGi Felix in Android

我的问题与旧的未解决问题Android 嵌入式 Felix 缺少需求 osgi.ee非常相关。

我只是将OSGi Felix 5.4.0 的一个实例嵌入到一个 Android 应用程序中,并尝试在其中安装一个纯 Java非常简单的 Bundle,由以下代码表示:

@Component
public class ExampleBattery
{
    @Activate
    public void activate()
    {
        Thread t = new Thread(new Work());
        t.start();
    }

    private class Work implements Runnable
    {
        boolean continueLoop = true;

        @Override
        public void run() 
        {
            while (continueLoop)
            {
                System.out.println("hello");
                try 
                {
                    Thread.sleep(2500);
                } 
                catch (InterruptedException e) 
                {
                    continueLoop = false;
                }
            }
        }
    } 
}

这显然需要osgi.ee=JavaSE在它自己的清单中:

Manifest-Version: 1.0
Bnd-LastModified: 1448019954932
Bundle-ManifestVersion: 2
Bundle-Name: ExampleBattery.ExampleBattery
Bundle-SymbolicName: ExampleBattery.ExampleBattery
Bundle-Version: 1.0.0
Created-By: 1.8.0_66 (Oracle Corporation)
Private-Package: user.producer.battery
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.7))"
Service-Component: OSGI-INF/user.producer.battery.ExampleBattery.xml
Tool: Bnd-3.0.0.201509101326

以下代码显示了我在 Android 应用程序中的具体操作:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    launchFramework();
}

private void launchFramework()
{
    // properties Map for the Felix instance
    Map<String, String> configMap = configMap = new HashMap<String, String>();

    // set some properties (like Felix's cache directory, etc.)
    configMap.put("felix.cache.rootdir", felixBaseDir);                 
    configMap.put("org.osgi.framework.storage", FELIX_CACHE_DIR_NAME);  
    try
    {
        // create and initialize the instance of the Framework
        this.felixInstance = new Felix(configMap);
        this.felixInstance.init();
        Log.i("Felix", "Framework successfully created and initialized");

        // simply use the Bundle context of the Felix 
        // instance to install ExampleBattery from the project's assets folder
        installBasicBundlesFromAssets();
        Log.i("Felix", "All basic Bundles installed successfully");

        // start the framework
        this.felixInstance.start();
        Log.i("Felix", "Framework successfully started");       
        //
        Log.i("Felix", "Starting installed Bundles ...");

        // simply call "start()" on all the installed Bundles
        if (startInstalledBundles())
        {
            Log.i("Felix", "ALL OK");
        }
    }
    catch (Exception ex)
    {
        Log.e("Felix", "Could not create framework: " + ex);
        return;
    }
}

看起来很简单,但是当我尝试启动ExampleBattery Bundle 时出现以下错误:

03-29 05:29:44.942:E/Felix(8156):无法启动捆绑包“ExampleBattery.ExampleBattery”:org.osgi.framework.BundleException:无法解析ExampleBattery.ExampleBattery [1](R 1.0):丢失要求 [ExampleBattery.ExampleBattery [1](R 1.0)] osgi.ee; (&(osgi.ee=JavaSE)(version=1.7)) 未解决的需求:[[ExampleBattery.ExampleBattery [1](R 1.0)] osgi.ee; (&(osgi.ee=JavaSE)(version=1.7))]

这种情况很奇怪,因为 Felix 默认使用felix.jar 中的default.properties配置文件导出 Java:

....
org.osgi.framework.system.capabilities=${eecap-${java.specification.version}}

eecap-1.8=osgi.ee; osgi.ee="OSGi/最低"; version:List="1.0,1.1,1.2", osgi.ee; osgi.ee="JavaSE"; 版本:List="1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8"
eecap-1.7 = osgi.ee; osgi.ee="OSGi/最低"; version:List="1.0,1.1,1.2", osgi.ee; osgi.ee="JavaSE"; 版本:List="1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7"
....

我真的不知道发生了什么,谢谢大家的回答。

我可以通过将以下属性设置为 felix 来解决该问题:

configMap.put(Constants.FRAMEWORK_SYSTEMCAPABILITIES, "osgi.ee; osgi.ee=\"JavaSE\";version:List=\"1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8\"");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM