You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Padma shankar <pa...@gmail.com> on 2009/04/08 10:01:53 UTC

BundleException - not able to load this type of class.

I am trying to port the Sip communicator application to android. This is
done by launching felix from the android activity. The other bundles are
installed by reading the bundles a Inputstream from the res/raw. The bundles
are getting installed.
The bundle.start() function is successful for bundles, which doesn't have
the Bundle-Activator constant in the manifest file. But if the
Bundle-Activator is present, the activator class fails to get loaded and
hence the bundle.start() fails with
BundleException - unable to load this type of class and a reference to the
defineClass function of the Dalvik vm.

I tried to use the luminis solution for this as below. Please see my
comments in the code

public class DalvikClassLoader {


    static final Constructor m_dexFileClassConstructor;
    private static final Method m_dexFileClassLoadClass;

static
{
    Constructor dexFileClassConstructor = null;
    Method dexFileClassLoadClass = null;
    try
    {
        Class dexFileClass =  Class.forName("android.dalvik.DexFile");
//Padma - First it failed here saying ClassNotFound exception. Then I
changed it "dalvil.system.DexFile" and this step was through.

        dexFileClassConstructor = dexFileClass.getConstructor(
            new Class[] { java.io.File.class });
        dexFileClassLoadClass = dexFileClass.getMethod("loadClass",
            new Class[] { String.class, ClassLoader.class });
    }
    catch (Exception ex)
    {
        dexFileClassConstructor = null;
        dexFileClassLoadClass = null;
    }
    m_dexFileClassConstructor = dexFileClassConstructor;
    m_dexFileClassLoadClass = dexFileClassLoadClass;
}


//Padma - I called this function after installing the bundle and before
starting the bundle as
//getDexFileClass(activatorClassName, ClassLoader.getSystemClassLoader)
// where the activatorClassName =
(String)bundle.getHeaders().get("Bundle-Activator");

private Object m_dexFile = null;

public synchronized Class getDexFileClass(String name, ClassLoader loader)
    throws Exception
{
    if (m_dexFile == null)
    {
        if ((m_dexFileClassConstructor != null) &&
              (m_dexFileClassLoadClass != null))
        {
            m_dexFile = m_dexFileClassConstructor.newInstance(
                new Object[] { m_file });
//It failed at m_file. So I tried to use the following:
// classes.dex - failed.
// /data/app/net.java.sip.communicator.android.apk - fails with unable to
open DEX filenull

//Because of these errors, I changed the constructor in the static block to
//dexFileClassConstructor = dexFileClass.getConstructor(
//            new Class[] { java.lang.String.class });

//And Changed the m_dexFile creation as below:
//m_dexFile = m_dexFileClassConstructor.newInstance(
//                new Object[] {
"/data/app/net.java.sip.communicator.android.apk" });


        }
        else
        {
            return null;
        }
    }

    return (Class) m_dexFileClassLoadClass.invoke(m_dexFile,
        new Object[] { name.replace('.','/'), loader });

//This returns null again. Whereas the m_dexFile value is
dalvik.system.DexFile@434ae908
}

 I tried to load the bundles given by luminis as well, but didn't understand
how to load it dynamically.I am terribly confused on what's going wrong and
stuck with this for the past couple of days. Can somebody help me on this.

Thanks in advance.

padma

Re: BundleException - not able to load this type of class.

Posted by Clement Escoffier <cl...@gmail.com>.
Hi,

Unfortunaly there is no way to use OSGi (and so Felix) on Android  
without "hacking" the phone.
If you read : http://ipojo-dark-side.blogspot.com/2008/10/ipojo-on-android.html

You will see that, you have to do a chmod 777 on the dalvik-cache

Why ?
Just because the Android security manager does not allow Felix to  
install files in the dalvik cache.
If you add the write permission, Felix is able to load bundle classes  
correctly.

Regards,

Clement


On 08.04.2009, at 10:01, Padma shankar wrote:

> I am trying to port the Sip communicator application to android.  
> This is
> done by launching felix from the android activity. The other bundles  
> are
> installed by reading the bundles a Inputstream from the res/raw. The  
> bundles
> are getting installed.
> The bundle.start() function is successful for bundles, which doesn't  
> have
> the Bundle-Activator constant in the manifest file. But if the
> Bundle-Activator is present, the activator class fails to get loaded  
> and
> hence the bundle.start() fails with
> BundleException - unable to load this type of class and a reference  
> to the
> defineClass function of the Dalvik vm.
>
> I tried to use the luminis solution for this as below. Please see my
> comments in the code
>
> public class DalvikClassLoader {
>
>
>    static final Constructor m_dexFileClassConstructor;
>    private static final Method m_dexFileClassLoadClass;
>
> static
> {
>    Constructor dexFileClassConstructor = null;
>    Method dexFileClassLoadClass = null;
>    try
>    {
>        Class dexFileClass =  Class.forName("android.dalvik.DexFile");
> //Padma - First it failed here saying ClassNotFound exception. Then I
> changed it "dalvil.system.DexFile" and this step was through.
>
>        dexFileClassConstructor = dexFileClass.getConstructor(
>            new Class[] { java.io.File.class });
>        dexFileClassLoadClass = dexFileClass.getMethod("loadClass",
>            new Class[] { String.class, ClassLoader.class });
>    }
>    catch (Exception ex)
>    {
>        dexFileClassConstructor = null;
>        dexFileClassLoadClass = null;
>    }
>    m_dexFileClassConstructor = dexFileClassConstructor;
>    m_dexFileClassLoadClass = dexFileClassLoadClass;
> }
>
>
> //Padma - I called this function after installing the bundle and  
> before
> starting the bundle as
> //getDexFileClass(activatorClassName,  
> ClassLoader.getSystemClassLoader)
> // where the activatorClassName =
> (String)bundle.getHeaders().get("Bundle-Activator");
>
> private Object m_dexFile = null;
>
> public synchronized Class getDexFileClass(String name, ClassLoader  
> loader)
>    throws Exception
> {
>    if (m_dexFile == null)
>    {
>        if ((m_dexFileClassConstructor != null) &&
>              (m_dexFileClassLoadClass != null))
>        {
>            m_dexFile = m_dexFileClassConstructor.newInstance(
>                new Object[] { m_file });
> //It failed at m_file. So I tried to use the following:
> // classes.dex - failed.
> // /data/app/net.java.sip.communicator.android.apk - fails with  
> unable to
> open DEX filenull
>
> //Because of these errors, I changed the constructor in the static  
> block to
> //dexFileClassConstructor = dexFileClass.getConstructor(
> //            new Class[] { java.lang.String.class });
>
> //And Changed the m_dexFile creation as below:
> //m_dexFile = m_dexFileClassConstructor.newInstance(
> //                new Object[] {
> "/data/app/net.java.sip.communicator.android.apk" });
>
>
>        }
>        else
>        {
>            return null;
>        }
>    }
>
>    return (Class) m_dexFileClassLoadClass.invoke(m_dexFile,
>        new Object[] { name.replace('.','/'), loader });
>
> //This returns null again. Whereas the m_dexFile value is
> dalvik.system.DexFile@434ae908
> }
>
> I tried to load the bundles given by luminis as well, but didn't  
> understand
> how to load it dynamically.I am terribly confused on what's going  
> wrong and
> stuck with this for the past couple of days. Can somebody help me on  
> this.
>
> Thanks in advance.
>
> padma


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: BundleException - not able to load this type of class.

Posted by Padma shankar <pa...@gmail.com>.
Thanks a lot.

The issue was because I was trying to run Felix 1.0.3 over Android1.0.  I
could see the application launching in old android version without any
problem.

To make the application work on Android1.0, I implemented the Felix1.4
changes and the Android1.0 changes to the source.
When I am running from Eclipse, application is launching and the bundles are
installed and started.
But the service reference is read as null. I am suspecting, that the felix
bundleContext and AndroidContext are not able to interact.
Is there any guideline, while migrating from Felix 1.0.x to Felix1.4.

Please help.

Thanks and regards.
Padma
-------------------------------------------------------------------------------------------------------------------------------
Attached the run time error message below:

E/AndroidRuntime( 3140): Uncaught handler: thread SC Felix launcher thread
exiting due to uncaught exception
E/AndroidRuntime( 3140): java.lang.NullPointerException: Specified service
reference cannot be null.
E/AndroidRuntime( 3140):     at
org.apache.felix.framework.BundleContextImpl.getService(BundleContextImpl.java:320)
E/AndroidRuntime( 3140):     at
net.java.sip.communicator.android.SIPCommunicator.getUIService(SIPCommunicator.java:260)
E/AndroidRuntime( 3140):     at
net.java.sip.communicator.android.SIPCommunicator.access$0(SIPCommunicator.java:244)
E/AndroidRuntime( 3140):     at
net.java.sip.communicator.android.SIPCommunicator$FelixLauncher.run(SIPCommunicator.java:145)
E/AndroidRuntime( 3140):     at
android.os.Handler.handleCallback(Handler.java:542)
E/AndroidRuntime( 3140):     at
android.os.Handler.dispatchMessage(Handler.java:86)
E/AndroidRuntime( 3140):     at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 3140):     at
net.java.sip.communicator.android.SIPCommunicator$1.run(SIPCommunicator.java:52)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
On Wed, Apr 8, 2009 at 1:50 PM, Marcel Offermans <
marcel.offermans@luminis.nl> wrote:

> On Apr 8, 2009, at 10:01 , Padma shankar wrote:
>
>  I am trying to port the Sip communicator application to android.
>>
>
> First of all, there already is a port of SIP Communicator to Android. You
> can find the project here:
>
> https://sc-android.dev.java.net/
>
> and:
> http://www.mail-archive.com/dev@felix.apache.org/msg05035.html
> https://sip-communicator.dev.java.net/servlets/ReadMsg?list=dev&msgNo=3989 (check
> out the attached PDF too)
>
>  This is
>> done by launching felix from the android activity. The other bundles are
>> installed by reading the bundles a Inputstream from the res/raw. The
>> bundles
>> are getting installed.
>> The bundle.start() function is successful for bundles, which doesn't have
>> the Bundle-Activator constant in the manifest file. But if the
>> Bundle-Activator is present, the activator class fails to get loaded and
>> hence the bundle.start() fails with
>> BundleException - unable to load this type of class and a reference to the
>> defineClass function of the Dalvik vm.
>>
>
> There are a couple of things you need to do on an Android phone or
> emulator:
> 1) make sure you "dex" all your bundles (in theory it's possible to do that
> on the fly on the phone, but in practice people have experienced all kinds
> of errors, running out of resources when doing that)
> 2) prepare your phone so an application can actually access the
> /data/dalvik-cache folder (chmod 777 /data and chmod 777 /data/dalvik-cache
> as root ** note that becoming root on a non-developer phone requires some
> kind of "hack")
>
> Also be aware that he first article that appeared about using OSGi on
> Android (using a pre 1.0 SDK) is now outdated because certain classes were
> renamed AND because Felix since 1.0.3 supports Android out of the box (so no
> need to do any patching anymore).
>
> My guess is that your subsequent problems come from one or more of the
> things mentioned above.
>
>  I tried to load the bundles given by luminis as well, but didn't
>> understand
>> how to load it dynamically.I am terribly confused on what's going wrong
>> and
>> stuck with this for the past couple of days. Can somebody help me on this.
>>
>
> The last demo I gave at ApacheCon uses a provisioning server that is not
> yet available (but will hopefully will be soon, we proposed it as Apache Ace
> last week) to actually deploy the bundles onto the device. It's not too hard
> however to do that in a more simple, direct way by just using the standard
> OSGi API to install a bundle: BundleContext.installBundle(InputStream) (and
> construct some kind of URL to for example a webserver that holds your
> bundle).
>
> Hope this helps a bit.
>
> Greetings, Marcel
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>

Re: BundleException - not able to load this type of class.

Posted by Marcel Offermans <ma...@luminis.nl>.
On Apr 8, 2009, at 10:01 , Padma shankar wrote:

> I am trying to port the Sip communicator application to android.

First of all, there already is a port of SIP Communicator to Android.  
You can find the project here:

https://sc-android.dev.java.net/

and:
http://www.mail-archive.com/dev@felix.apache.org/msg05035.html
https://sip-communicator.dev.java.net/servlets/ReadMsg?list=dev&msgNo=3989 
  (check out the attached PDF too)

> This is
> done by launching felix from the android activity. The other bundles  
> are
> installed by reading the bundles a Inputstream from the res/raw. The  
> bundles
> are getting installed.
> The bundle.start() function is successful for bundles, which doesn't  
> have
> the Bundle-Activator constant in the manifest file. But if the
> Bundle-Activator is present, the activator class fails to get loaded  
> and
> hence the bundle.start() fails with
> BundleException - unable to load this type of class and a reference  
> to the
> defineClass function of the Dalvik vm.

There are a couple of things you need to do on an Android phone or  
emulator:
1) make sure you "dex" all your bundles (in theory it's possible to do  
that on the fly on the phone, but in practice people have experienced  
all kinds of errors, running out of resources when doing that)
2) prepare your phone so an application can actually access the /data/ 
dalvik-cache folder (chmod 777 /data and chmod 777 /data/dalvik-cache  
as root ** note that becoming root on a non-developer phone requires  
some kind of "hack")

Also be aware that he first article that appeared about using OSGi on  
Android (using a pre 1.0 SDK) is now outdated because certain classes  
were renamed AND because Felix since 1.0.3 supports Android out of the  
box (so no need to do any patching anymore).

My guess is that your subsequent problems come from one or more of the  
things mentioned above.

> I tried to load the bundles given by luminis as well, but didn't  
> understand
> how to load it dynamically.I am terribly confused on what's going  
> wrong and
> stuck with this for the past couple of days. Can somebody help me on  
> this.

The last demo I gave at ApacheCon uses a provisioning server that is  
not yet available (but will hopefully will be soon, we proposed it as  
Apache Ace last week) to actually deploy the bundles onto the device.  
It's not too hard however to do that in a more simple, direct way by  
just using the standard OSGi API to install a bundle:  
BundleContext.installBundle(InputStream) (and construct some kind of  
URL to for example a webserver that holds your bundle).

Hope this helps a bit.

Greetings, Marcel


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org