簡體   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