androidcordovaionic-frameworkpluginshybrid-mobile-app

Class Not Found Exception using custom cordova plugin


I'm developing a printing app that uses a custom API to access the printer via USB, so I needed a custom Cordova plugin. I started developing it, it has been a very good challenge but also very frustrating at the moment because I can't figure out why my plugin can't be used correctly. The thing is: 1.- The plugin installs correctly and lets me build the application 2.- The Javascript code runs correctly 3.- I get a runtime error that doesn't crash the app. It seems like the Java code is skipped. And I noticed there was an error in the Android Monitor.


You can find my plugin here: https://github.com/krlozadan/cordova-custom-printer-plugin


W/System.err: java.lang.ClassNotFoundException: com.duplou.cordova.plugin.customprinter.CustomPrinter
W/System.err:     at java.lang.Class.classForName(Native Method)
W/System.err:     at java.lang.Class.forName(Class.java:324)
W/System.err:     at java.lang.Class.forName(Class.java:285)
W/System.err:     at org.apache.cordova.PluginManager.instantiatePlugin(PluginManager.java:489)
W/System.err:     at org.apache.cordova.PluginManager.getPlugin(PluginManager.java:169)
W/System.err:     at org.apache.cordova.PluginManager.exec(PluginManager.java:122)
W/System.err:     at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:57)
W/System.err:     at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41)
W/System.err:     at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
W/System.err:     at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:53)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:61)
W/System.err: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.duplou.cordova.plugin.customprinter.CustomPrinter" on path: DexPathList[[zip file "/data/app/1/lib/1/base.apk!/lib/x86, /vendor/lib, /system/lib]]
W/System.err:     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
W/System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
W/System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
W/System.err:  ... 13 more
W/System.err:  Suppressed: java.lang.ClassNotFoundException: com.duplou.cordova.plugin.customprinter.CustomPrinter
W/System.err:     at java.lang.Class.classForName(Native Method)
W/System.err:     at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
W/System.err:     at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
W/System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
W/System.err:      ... 14 more
W/System.err:  Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
I/System.out: Error adding plugin com.duplou.cordova.plugin.customprinter.CustomPrinter.
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.apache.cordova.CordovaPlugin.privateInitialize(java.lang.String, org.apache.cordova.CordovaInterface, org.apache.cordova.CordovaWebView, org.apache.cordova.CordovaPreferences)' on a null object reference
W/System.err:     at org.apache.cordova.PluginManager.getPlugin(PluginManager.java:171)
W/System.err:     at org.apache.cordova.PluginManager.exec(PluginManager.java:122)
W/System.err:     at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:57)
W/System.err:     at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41)
W/System.err:     at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
W/System.err:     at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:53)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:61)
D/SystemWebChromeClient: file:///android_asset/www/plugins/cordova-custom-printer-plugin/www/custom-printer.js: Line 11 : Se terminó la ejecución
I/chromium: [INFO:CONSOLE(11)] "Se terminó la ejecución", source: file:///android_asset/www/plugins/cordova-custom-printer-plugin/www/custom-printer.js (11)
D/CordovaWebViewImpl: onPageFinished(file:///android_asset/www/index.html)
W/PluginManager: THREAD WARNING: exec() call to Sim.getSimInfo blocked the main thread for 53ms. Plugin should use CordovaInterface.getThreadPool().
D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 1436 : ERROR
I/chromium: [INFO:CONSOLE(1436)] "ERROR", source: file:///android_asset/www/build/main.js (1436)
W/BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 12139
D/EGL_emulation: eglMakeCurrent: 0xae414a40: ver 2 0 (tinfo 0xa14bfbe0)

Here's my ionic info output
global packages:
@ionic/cli-utils : 1.5.0
Cordova CLI      : 7.0.1
Ionic CLI        : 3.5.0
local packages:

@ionic/app-scripts              : 1.3.7
@ionic/cli-plugin-cordova       : 1.4.1
@ionic/cli-plugin-ionic-angular : 1.3.2
Cordova Platforms               : android 6.2.3
Ionic Framework                 : ionic-angular 3.2.1
System:

Node       : v6.10.3
OS         : macOS Sierra
Xcode      : Xcode 8.3.2 Build version 8E2002
ios-deploy : not installed
ios-sim    : not installed
npm        : 3.10.10

Solution

  • I see that you are pointing to a jar file in your config.xml. If you really want to keep using this jar file make sure that the path is correctly mapped in android platform folders and it matches the classpath.

    If you want to do a simple test, you should declare your android source files one by one. So after the node config-file you should add several nodes like this one:

    <source-file src="src/android/src/net/mydomain/myplugin/MyClass.java" target-dir="src/mydomain/myplugin" />