Class not found error when loading Java plugin on Android device

  • 1
  • Problem
  • Updated 4 years ago
  • In Progress
I want to developp a custom Java plugin for Android.
I do no manage to make it work : I always get a "class not found" error message when loading the plugin from javascript code (Android 4.4; Motorola Moto G).

Here is the code of a demo of the issue.

I just downloaded a copy of this reliable plugin from here and added the following javascript code in the index.html file :
---
var iPlugin = { createEvent: function(todo, successCallback, errorCallback) {
cordova.exec( successCallback, errorCallback, 'Insomnia', todo, [] );}};
var success = function() { alert("Success"); };
var error = function(message) { alert("Oopsie! " + message); };
iPlugin.createEvent("keepAwake", success, error);
---
I always get a "Oopsie! Class not found" error message.
I probably missed something : thanks for your help !
Photo of Raymond Blanc

Raymond Blanc

  • 7 Posts
  • 0 Reply Likes

Posted 6 years ago

  • 1
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Hi,

Are you using PhoneGap(build app locally) or PhoneGap Build(build with our cloud service)?

Also, can we know how you are adding the plugin to your project(recommend following steps mentioned in documentation).

Thanks.
Photo of Raymond Blanc

Raymond Blanc

  • 7 Posts
  • 0 Reply Likes
Hello,

I'm using your cloud service PhoneGap Build.
I'm using it with a .zip package or with my public Github repository: you can test my issue from there.

I tried to follow the documentation Plugin Specification...

I downloaded the code of the plugin from this Github repository, and added very simple config.xml and index.html files (with the javascript code given in my previous post, see above).

Thanks in advance.
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Hi Raymond,

Sorry for the late reply.

Here is the mistake you made:

You are not meant to add the files for a plugin directly into it in PhoneGap Build.

What you were expected to do is add the call to the plugin in your config.xml.

When you build the project, our server would inject the appropriate files into the project.

My guess is that is why it is not working.

You can have a look at this sample with Barcode Scanner plugin added into it.

The various plugin files would be added later when the app is built.

Hope that helps.

Would recommend reading the plugin installation documentation, looking for the section that talks about installing plugin for PGB.

Thanks.
Photo of Raymond Blanc

Raymond Blanc

  • 7 Posts
  • 0 Reply Likes
Hi Ismael,

Thanks for your reply but it seems that the problem comes from somewhere else...

I tested your sample on my Motorola Moto G, Android 4.4.: it does not work.

(I built the app with PhoneGapBuild from the .zip downloaded from your GitHub repository.)

I get the error message "[ERROR] Error initializing Cordova: Class not found" when opening the app.
I get the message "Scanning failed: Class not found" when pressing on the "Scan Code" button.

Thanks for your help.
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Hi Raymond,

That's odd, I just tested this out again and it works as expected.

Here is a link to a project built with the code I sent you.

It's a public project so you should be able to access, scan the barcode and test with your device.

Our device is a Nexus 4, unfortunately I don't have a Motorola device around to test this with, would appreciate testing on any other devices also if you manage to lay your hand on one.

Thanks.
Photo of Raymond Blanc

Raymond Blanc

  • 7 Posts
  • 0 Reply Likes
Hi Ismael,

I tested your app : it works as expected (even on my device).

Accordingly it means that my problem comes from the way I built the app, not from the code.

Here is how I did :
1) I downloaded your code from your GitHub repository as ZIP file
2) I uploaded the ZIP file through the "Update code" button in the PhoneGap Build interface (a few seconds later, the app was ready... but it does not work as expected : I get the error message "[ERROR] Error initializing Cordova: Class not found" when opening the app; I get the message "Scanning failed: Class not found" when pressing on the "Scan Code" button.)

-> What is wrong with that ?

Thanks for your help.
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Hi Raymond,

Let me send you the latest source code zip file as I haven't updated the one on the git in a few days.

Test and let me know if that works for you.

Thanks.
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Sent.
Photo of Raymond Blanc

Raymond Blanc

  • 7 Posts
  • 0 Reply Likes
Hi Ismael,

I tested your code (I uploaded your code zip file in the PhoneGap Build interface and then downloaded the new version of the app on my device) : it does not work :
- I get the error message "[ERROR] Error initializing Cordova: Class not found" when opening the app,
- I get the message "Scanning failed: Class not found" when pressing on the "Scan Code" button.
...
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Could you do the following.

Delete your current app

Create a new one and upload the file I sent you?

Check to ensure the config.xml is valid using an xml validator.

Once done, download to your device and test.

I was able to build this and it works successfully on Nexus 4(Android 4.4.2 earlier in the week and now 4.4.3).

Thanks.
Photo of Raymond Blanc

Raymond Blanc

  • 7 Posts
  • 0 Reply Likes
Hello,

> I tested again your code (I uploaded the code zip file (that you sent to me) in the PhoneGap Build interface and then downloaded the new version of the app on my device).
-> On a Samsung device, Android 4.2.2 : it works as expected,
-> On Motorola Moto G Android 4.4.2: it does not work :
- I get the error message "[ERROR] Error initializing Cordova: Class not found" when opening the app,
- I get the message "Scanning failed: Class not found" when pressing on the "Scan Code" button.

> I tested again your app : it works on both devices.

> I tested again the code of my first post (see above):
-> On Samsung, Android 4.2.2 : I get a "Oopsie! Class not found" error message,
-> On Motorola Moto G, Android 4.4.2: I get the error message "[ERROR] Error initializing Cordova: Class not found".
...
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Hi Pascal,

Your whole code set up is wrong.

If you are using PhoneGap Build, you are not meant to have the codes for insomnia in your project on git.

This would be injected.

Have a look at my own sample and note the differences between teh codes, from there, make appropriate changes to yours removing anything not found in my code.

Thanks.
Photo of Raymond Blanc

Raymond Blanc

  • 7 Posts
  • 0 Reply Likes
Hi Ismael,

Following my previous tests and posts above, there are 2 issues:
1) How to build an app with PhoneGap Build which runs on Motorola Moto G ?
2) How to code a simple Java Android Plugin ?

1) I tested again your code (I uploaded the code zip file (that you sent to me by email) in the PhoneGap Build interface and then downloaded the new version of the app on my device).
-> On a Samsung Ace 3 device, Android 4.2.2 : it works as expected,
-> On Motorola Moto G Android 4.4.2: it does not work :
- I get the error message "[ERROR] Error initializing Cordova: Class not found" when opening the app,
- I get the message "Scanning failed: Class not found" when pressing on the "Scan Code" button.
=> How to fix that ?

I will no longer make tests on this Motorola Moto G device as long as this is not fixed.

2) As I told you in my first post, I just want to code a simple Java Android plugin.
(I give up with the Insomnia Plugin code.)
I just want to make the "Echo Android Plugin Example" from the PhoneGap Documentation run (on the Samsung Ace 3 device).
With this code, I get a "Oopsie! Class not found" error message (on the Samsung Ace 3 device).
=> What is wrong with my code ?

Thanks in advance
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Hi Raymond,

Unfortunately I do not have a Moto G at hand to test things with(will try and get my hand on one and see if I can help though expect build process to be the same).

To code a plugin have a look at this, you can also have a look at a sample plugin here(focus on content on SRC and WWW folders as well as plugin.xml).

Hope it helps.

Photo of Muthuraj T

Muthuraj T

  • 10 Posts
  • 0 Reply Likes
Hi,
Add class name in AndroidManifest file and give a try.
Photo of grs 225

grs 225

  • 1 Post
  • 0 Reply Likes
Thank You, helped me a lot
ismael jimoh