Search code examples
androidosginoclassdeffounderrorapache-felix

OSGi (Felix) on Android: NoClassDefFoundError: sun/reflect/ConstructorAccessorImpl


I try to run a bundle that obviously uses sun.reflect package.

First some more backgroundinformation: I'm using Apache Felix on Android. And i also added sun.reflect as a system extra package.

String extrapackets =  "sun.reflect; version=\"1.0.0\"";

config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, extrapackets);

config.put("felix.bootdelegation.implicit", "false");

"sun.reflect" is also shown in the exportinglist of the system bundle (using headers cmd). And it is imported by my bundle. It also got correct versionnumbers at the export and importing stuff.

Errormsg:

java.lang.NoClassDefFoundError: sun/reflect/ConstructorAccessorImpl
at sun.misc.Unsafe.defineClass(Native Method)
at sun.reflect.ClassDefiner.defineClass(ClassDefiner.java:45)
at sun.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:381)
at java.security.AccessController.doPrivileged(Native Method)
at sun.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:377)
at sun.reflect.MethodAccessorGenerator.generateConstructor(MethodAccessorGenerator.java:76)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:30)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
...

Solution

  • In case anyone else runs across this issue, there may be a quicker fix.

    I hit this bug when using reflection in OSGi running on a Sun/Oracle JVM. It turns out this is a known issue. The suggested fix of setting -Dsun.reflect.noInflation=true at the JVM level will cause the class not to be referenced, at the cost of ignoring an optimization.

    I tried the fix, and it worked for me.