Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError at runtime but class is in classes.dex, what givies?

I have included a JAR file built from an Eclipse Android project, and I am referencing it in my Android Studio project like the following:

  compile files('libs/libraryproject.jar')

This works, and I am able to get auto-complete on code references. When I compile the APK everything is fine. I install and run, and then receive a no class def error:

 java.lang.NoClassDefFoundError: com.android.canvas.CanvasContainer
            at com.app.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

However, when I unzip the APK, and use dexdump to view the classes.dex file, I see that the above class exists there.

Why then am I getting this class not found error at runtime?

Update:

Thanks to Chris's suggestion I noticed that earlier in the logs, my CanvasContainer class could not be linked due to it not being able to resolve interface 1990 'Lorg/cocos2dx/lib/Cocos2dxHelper$Cocos2dxHelperListener;'. Pretty apparently it looks like my Cocos library code is not getting exported as part of my JAR.

like image 582
stevebot Avatar asked Feb 24 '15 01:02

stevebot


1 Answers

A class with dependencies which cannot be met at runtime will usually be dropped during installation, dex optimization or similar preparatory process.

If you uninstall the app, start up something collecting all of logcat and then install it again, you may find by searching through the result that there is mention of the issue buried in the logs generated during the installation/preparation process.

In this specific case here, an edit to the question indicates that the missing class itself had this type of unsatisfied dependency.

like image 143
Chris Stratton Avatar answered Nov 05 '22 03:11

Chris Stratton