2
votes

My question is very related to the old unresolved question Android embedded Felix missing requirement osgi.ee .

I simply embedded an instance of OSGi Felix 5.4.0 into an Android app, and tried to install a pure-Java very simple Bundle in it, represented by the following code:

@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;
                }
            }
        }
    } 
}

which obviously requiring osgi.ee=JavaSE in its own manifest:

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

The following code show what I exactly did in the Android app:

@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;
    }
}

It seeems very simple, but I obtain the following error when I try to start the ExampleBattery Bundle:

03-29 05:29:44.942: E/Felix(8156): Unable to start the Bundle "ExampleBattery.ExampleBattery": org.osgi.framework.BundleException: Unable to resolve ExampleBattery.ExampleBattery [1](R 1.0): missing requirement [ExampleBattery.ExampleBattery [1](R 1.0)] osgi.ee; (&(osgi.ee=JavaSE)(version=1.7)) Unresolved requirements: [[ExampleBattery.ExampleBattery [1](R 1.0)] osgi.ee; (&(osgi.ee=JavaSE)(version=1.7))]

This situation is very strange, because Felix exports Java by default using its default.properties configuration file present in felix.jar:

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

eecap-1.8= osgi.ee; osgi.ee="OSGi/Minimum"; version:List="1.0,1.1,1.2", 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"
eecap-1.7= osgi.ee; osgi.ee="OSGi/Minimum"; version:List="1.0,1.1,1.2", osgi.ee; osgi.ee="JavaSE"; version:List="1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7"
....

I really don't know what happened, thanks to all for answer.

1

1 Answers

2
votes

I could solve that problem by setting the following property to 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\"");