Class Not Found Exception using custom cordova plugin

时间秒杀一切 提交于 2019-12-13 08:29:31

问题


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

回答1:


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" /> 



回答2:


This is the most important part of your stacktrace:

Caused by: java.lang.ClassNotFoundException: Didn't find class "com.duplou.cordova.plugin.customprinter.CustomPrinter"

Your Cordova configuration references the CustomPrinter plugin. When Javascript triggers loading it, the Java class cannot be found and thus your application crashes.

You need to ensure that the Java class is present in your application package.




回答3:


I have this error and solved it by change android.json file in \platforms\android\android.json

You must change value of res/xml/config.xml to your package of custom Plugin.

The reason is package of your custom plugin and value of above in not same.




回答4:


In my case I had multiple .java files referenced. When I changed the ordering of how it is referenced in plugin.xml, it fixed the problem. Seems like the last .java file is dependent on the second last one.

Before:

    <!--libs-->
    <source-file src="libs/sample-release.aar" target-dir="libs/" />
    <source-file src="src/android/Licence.java" target-dir="src/android/" />
    <source-file src="src/android/Deserializer.java" target-dir="src/android/" />

After:

<!--libs-->
<source-file src="libs/sample-release.aar" target-dir="libs/" />
<source-file src="src/android/Deserializer.java" target-dir="src/android/" />
<source-file src="src/android/Licence.java" target-dir="src/android/" />

So just by switching the ordering, the ClassNotFoundException was resolved.



来源:https://stackoverflow.com/questions/45343459/class-not-found-exception-using-custom-cordova-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!