1

In my eclipse plugin project, I am trying to use the Android API.

The first thing I did is to add Android.jar to the build path. Then, I attempted to show a message on both the console (using System.out.println()), and on the LogCat (using android/util.Log). I am showing these messages in my start() method of the Bundle Activator class.

I also exported and imported the android.util package in my MANIFEST.MF file.

When I run the bundle, I see the first message on my console, but after that, I get the following errors:

!ENTRY OSGI_Android_Bundle 4 0 2013-08-11 07:54:56.008
!MESSAGE FrameworkEvent ERROR
!STACK 0
org.osgi.framework.BundleException: Exception in osgi_android_bundle.Activator.start() of bundle OSGI_Android_Bundle.
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:734)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
    at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:390)
    at org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1177)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
    at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
Caused by: java.lang.NoClassDefFoundError: android/util/Log
    at osgi_android_bundle.Activator.start(Activator.java:27)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
    ... 12 more
Caused by: java.lang.ClassNotFoundException: android.util.Log
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 16 more
Root exception:
java.lang.NoClassDefFoundError: android/util/Log
    at osgi_android_bundle.Activator.start(Activator.java:27)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
    at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:390)
    at org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1177)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
    at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
Caused by: java.lang.ClassNotFoundException: android.util.Log
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 16 more

I tried to search for similar situations but I found them unanswered. For example here at stackoveflow.

Can some one help? Below is my code:

package osgi_android_bundle;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;


import android.util.Log;

public class Activator implements BundleActivator {

    private static BundleContext context;

    static BundleContext getContext() {
        return context;
    }

    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext bundleContext) throws Exception {
        Activator.context = bundleContext;


        System.out.println("Hello World. I am the OSGI_Android_Bundle!");

        Log.d("Zaid Log", "Hello World. I am the OSGI_Android_Bundle!!");
    }

    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext bundleContext) throws Exception {
        Activator.context = null;


        System.out.println("Goodbye World. I am the OSGI_Android_Bundle!");

        Log.d("Zaid Log", "Goodbye World. I am the OSGI_Android_Bundle!!");
    }

}

and also my MANIFEST.MF:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: OSGI_Android_Bundle
Bundle-SymbolicName: OSGI_Android_Bundle
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: osgi_android_bundle.Activator
Import-Package: org.osgi.framework;version="1.3.0", android.util
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: android.util

Note: When I start this bundle in my Android app (which has Knopflerfish platform embedded), it does not show anything at all. So I thought I should fix the above errors first, in order to see the message in the log.

Community
  • 1
  • 1
Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83
  • Why do you export the `android.util` package? Is that package physically part of your bundle? Or is it exported by another bundle (or the system bundle)? – Neil Bartlett Aug 11 '13 at 11:16
  • I export it because when I don't do that, I get "No available bundle exports package 'android.util'" at the line I Import it. When I export it, this error is gone. I didn't understand what you exactly mean by physically part of the bundle, but no, It is not exported by any other bundle. – Traveling Salesman Aug 11 '13 at 11:54
  • If the package isn't *in* your bundle, you can't export it. That's just. – Neil Bartlett Aug 12 '13 at 07:07

2 Answers2

0

I'm not familiar with andriod, but I can comment from the OSGi perspective. It is not correct for you to export android.util as your bundle does not have that class.

Instead, you need to prepare the android.jar as another OSGi bundle as that jar does have that package and add it to the OSGi runtime. It is not enough to add it to the build path.

sjlee
  • 7,726
  • 2
  • 29
  • 37
  • okay, I prepared the android.jar as a bundle. Now I couldn't figure out how to open that bundle in eclipse so I can export the packages. Can you help? – Traveling Salesman Aug 11 '13 at 17:35
  • okay, I created the android bundle in eclipse following this link: http://stackoverflow.com/questions/9819090/how-to-convert-jar-to-osgi-bundle-using-eclipse-and-bndtools In that bundle, I exported android.util Now my original bundle only imports the android.util, (I removed the export android.util). After all, when I run my original bundle, I get new nice errors, I will show part of them in my next comment. – Traveling Salesman Aug 11 '13 at 18:24
  • Caused by: java.lang.RuntimeException: Stub! at android.util.Log.d(Log.java:7) at osgi_android_bundle.Activator.start(Activator.java:27) at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711) at java.security.AccessController.doPrivileged(Native Method) at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702) ... 9 more Root exception: java.lang.RuntimeException: Stub! at android.util.Log.d(Log.java:7) at osgi_android_bundle.Activator.start(Activator.java:27) at ....... – Traveling Salesman Aug 11 '13 at 18:26
  • Well, at least it indicates that your bundle is now properly wired against the andriod bundle, from the fact that the android.util.Log.d() method was called. Beyond that, I'm not sure if I have anything to add to help as I'm not familiar with the android code... – sjlee Aug 11 '13 at 22:39
  • You should accept @sjlee's answer because it does fix the problem reported in the question. If you have a supplemental question, ask it separately. For one thing, the stack trace is completely unreadable when posted in a comment. – Neil Bartlett Aug 12 '13 at 07:10
0

You can refer to the chapter "Enable non-graphical android bundle" in my topic. Even if it is aimed at Knopflerfish, the changes should be applicable to another OSGi implementation as long as you can modify it.

Full Android support for OSGi bundles

At the same time, I don't mind some help on my topic.

Community
  • 1
  • 1
slash33
  • 899
  • 1
  • 11
  • 17