I recently upgraded my app from Phonegap 1.6 to 2.2.
I've refactored everything (ex Plugin --> CordovaPlugin) and I'm also using the new config.xml.
The app compiles and builds, however I constantly am getting an error in logcat and an error dialog box. The dialog says [ERROR] Error initializing Cordova: Class not found.
The logcat error is:
Line 6048 : Error initializing Network Connection: Class not found
What could I be missing?
The issue happens on multiple devices.
Update
I've already changed the config.xml for the network status plugin from Network Status to NetworkStatus. It reads: <plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager"/>
I had this problem and made it go away by adding the following to the config.xml:
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager"/>
And adding this to the Manifest file:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
I originally took them out because I "knew" that my app wouldn't be using any communications. I had forgotten about the emulator "communicating" with the ADK.
I'll take these 2 lines out when I convert the project over to "production" so that the app is ready for Google Play.
I had this problem using phonegap 3.x and the problem turned out to be that phonegap hadn't properly installed the plugins, or they just messed up along the way. Basically when you install the plugins and build for a platform it takes the javascript files from plugins/org.apache.cordova.core.specific-plugin/www and puts them in platforms/android/assets/www/plugins/org.apache.cordova.core.specific-plugin/www and then it takes the Java files (or objective C for iOS) and puts them in platforms/android/src/org/apache/cordova/specificplugin
And all of this is specified by plugins/org.apache.cordova.core.specific-plugin/plugin.xml. If you look in a plugins.xml you should see something like:
<source-file src="src/android/NetworkManager.java" target-dir="src/org/apache/cordova/networkinformation" />
So this tells you that in platforms/android/src/org/apache/cordova/networkinformation, there should be NetworkManager.java. And right that file can be copied from plugins/org.apache.cordova.core.network-information/src/android/NetworkManager.java
Now all of this is supposed to happen automatically without having to touch the platforms folder. But if it messes up you can fix it by copying the correct java files into the correct folders.
The following line in config.xml works for cordova 6.2 with android
<plugin name="cordova-plugin-network-information" value="org.apache.cordova.networkinformation"/>
The name should be the same as in what your plugin contains. You can find it in the your project folder at the following path plugins/name-of-the-plugin/package.json.
The value should be the package name for the plugin which can be found in the native implementation file.
Related
I'm using cordova-plugin-mfilechose in my Ionic 4 application. It works fine till Android version 9. But in Android 10 whenever we trigger the plugin it just opens a window saying Internal storage but no file is listed, though there are many files inside the phone. I have used the same code as per the official documentation, added below. Thanks in advance.
window.plugins.mfilechooser.open(['.doc', '.xls', '.ppt'], function (uri) {
alert(uri);
}, function (error) {
alert(error);
});
The plugin looks like it is using standard Java File operations and paths to choose files, in Android 10 and above you cannot use file paths or Java File operations any more on anything but your App's private directory.
See https://developer.android.com/training/data-storage/files/external-scoped
Also not sure why you would use such a plugin as Android provides the functions natively with Storage Access FrameWork(SAF) or MediaStore
See https://developer.android.com/guide/topics/providers/document-provider#client
on how.
Everything can now only work with Java FileDescriptor
I found a workaround to my problem. Though it is not a solution in long run. In my Ionic 4 project project/config.xml android-targetSdkVersion was 29. I changed it to 28. Which works on Android 10 and also on all the lower versions. Below is the code.
<preference name="android-targetSdkVersion" value="28" />
I think all ionic plugins like cordova-plugin-camera and cordova-plugin-telerik-imagepicker needs to update to fully compatible with android 10 for scoped storage.
My internet radio station player app failed with the upgrade to Android 6 (Marshmallow). To test I copied the example code (below) from the docs for the Media plugin and built a tiny test app on latest Cordova (6.1.1), Android platform (5.1.1), Browser platform (4.1.0) and Media plugin (2.3.0). I also added necessary whitelist statements (below). On the browser platform it plays both internet files and internet streams. On the Android platform it fails to start mp3 internet streams. The console log shows that a stream is terminated within 1 second and the OnSuccess callback is called. No error messages, just logs "playAudio():Audio Success" and terminates.
Player code is
var src = "http://audio.wgbh.org:8104";
my_media = new Media(src,
// success callback
function () {
console.log("playAudio():Audio Success");
},
// error callback
function (err) {
console.log("playAudio():Audio Error: " + err);
}
config.xml whitelist is (yes unsafe, I'll fix that later)
<plugin name="cordova-plugin-whitelist" source="npm" spec="1" />
<allow-navigation href="*" />
<allow-intent href="*" />
<access origin="*" launch-external="yes" /> <!-- Required for iOS9 -->
A previous Stack Overflow question from Dec 2015 had the same problem "cordova media plugin stopped working on Android 6". It suggests a java patch to the AudioPlayer. I made that change and rebuilt my test app. But that didn't change anything. Is a Cordova build sufficient to make that change?
BTW, testing on a real phone plugged into USB, seeing log via Chrome dev tools.
What am I missing? Or is this a bug in the Media plugin java code or in the Android platform?
To answer my own question, the patch provided previously in cordova media plugin stopped working on Android 6 does answer my problem.
My mistake was applying it in the wrong place. Changing code directly in platforms/android/src/org/apache/cordova/media/AudioPlayer.java and rebuilding the app does work. (I was patching the plugin code instead). Comment out line 354, so it becomes,
//this.seekToPlaying(this.seekOnPrepared);
That does the trick.
Let me start with some context. I have a Java file and a .jar library which contains an Android project with an Activity, its layout, and another jar library. I also have the javascript interface for my plugin, of course. In other words, this is what I have:
plugins/com.phonegap.example.activity.plugin/www/myplugin.js
plugins/com.phonegap.example.activity.plugin/src/android/MyPlugin.java
plugins/com.phonegap.example.activity.plugin/src/android/libs/myJar.jar
plugins/plugin.xml
My plugin is supposed to execute a method within my MyPlugin.java file which is supposed to then launch the Activity that is within the myJar.jar file.
The plugin.xml for this plugin reads:
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
id="com.phonegap.example.activity.plugin"
version="0.1.0">
<name>MyPlugin</name>
<description>Launch an Android Activity from PhoneGap 3</description>
<author>Author</author>
<license>MIT</license>
<keywords>phonegap,activity</keywords>
<js-module src="www/myplugin.js" name="MyPlugin">
<clobbers target="window.MyPlugin" />
</js-module>
<!-- android -->
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="MyPlugin">
<param name="android-package" value="com.phonegap.example.activity.plugin"/>
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<activity
android:name="com.phonegap.example.activity.ExampleActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.phonegap.example.action.EXAMPLE" />
</intent-filter>
</activity>
</config-file>
<source-file src="src/android/MyPlugin.java" target-dir="src/com/phonegap/example/activity" />
<source-file src="src/android/libs/myJar.jar" target-dir="libs" />
</platform>
</plugin>
I have installed Phonegap 3.3 and Cordova 3.4 using npm install, and I have Ruby 2.0.0 installed as well. I'm running all this on a Mac OS X.
Onto the problem:
I create a phonegap app to test this plugin (phonegap create TestApp)
I add the Android platform (cordova platform add android)
I run phonegap local plugin add /path/to/plugin/folder from within my newly created app
I run phonegap build android and the compile fails with the following error:
[error] An error occurred while building the android project. Error executing "ant debug -f "/path/to/TestApp/platforms/android/build.xml"":
BUILD FAIL
/path/to/ant/build.xml:720: The following error occurred while executing this line:
/path/to/ant/build.xml:734: Compile failed; see the compiler error output for details.
Total time: 4 seconds
I don't understand why this is happening at all. When I install the plugin it seems like it's been added successfully and it appears within the plugins/android.json file.
At one point, the project automagically decided to compile properly (I don't know what made it work). I tried to access the plugin I had added by adding a line that reads var p = window.MyPlugin within the www/js/index.js, right after the app.receivedEvent('deviceready'); in the onDeviceReady function. I used console.log(p); and it turned out to be undefined, as if my plugin had never been loaded/initialized in the first place.
I suppose this is a problem within my plugin definition, since the Java part has been tested on an Android native app and it was working properly. Where did I go wrong with my plugin? What can I do to troubleshoot it? Why wasn't it initialized? Any help/guidance is very well-appreciated.
======UPDATE=====
I followed Dawson's suggestion to use cordova build android -d and here's the output I received:
[javac] /path/to/TestApp/platforms/android/src/com/phonegap/example/MyPlugin.java:10:
error: package com.phonegap.example.activity does not exist
[javac] import com.phonegap.example.activity.ExampleActivity;
This com.phonegap.example.activity package is defined within the myJar.jar file, so it seems that MyPlugin.java can't find the myJar.jar file.
I think the plugin.xml should be correctly taking care of the including the jar file during plugin installation. This is how it's being added in the plugin.xml:
<source-file src="src/android/libs/myJar.jar" target-dir="libs" />
Any ideas of what may be wrong here?
It's solved thanks to Dawson.
I ran cordova build android -d and viewed its output. Noticed misplaced libraries (myPlugin.java couldn't find myJar.jar) so I just moved them to the proper place as Dawson suggested.
Thanks a lot!
I have a problem using the Facebook Connect plugin with PhoneGap.
I have added this line to file plugins.xml:
<plugin name="com.phonegap.facebook.Connect" value="com.phonegap.facebook.ConnectPlugin" />
And this include on in file FbDialog.java:
import com.phonegap.helloworld.R;
I have these files on src:
com
/facebook/android/
AsyncFacebookRunner.java
DialogError.java
Facebook.java
FacebookError.java
FbDialog.java
Util.java
/phonegap/
facebook/ConnectPlugin.java
helloworld/HelloPhoneGapActivity.java
The ressources files close and the icon is copied. In HTML, I include some JavaScript code:
cordova-1.6.1.js
cdv-plugin-fb-connect.js
facebook_js_sdk.js
I use the default HTML example with my appId.
When I run application on my Android phone, a dialog says:
Cordova Facebook connect plugin fail on init!
and
Cordova Facebook connect plugin fail on auth.status!
If I click the login bouton, the dialog says:
Cordova Facebook connect plugin fail on login!Class not found
And the Eclipse console log is:
I tried to install this with the officiel Git readme and the
tutorial Add Facebook login to PhoneGap/Cordova Android app Easiest way.
When I compile the application, I don't get the error.
How do I fix this problem?
After the switch to Cordova, everything got renamed. You need the new class location:
<plugin name="org.apache.cordova.facebook.Connect" value="org.apache.cordova.facebook.ConnectPlugin" />
You'll have to put your plugin java (back?) to the correct location as well in org/apache/cordova/facebook/ .
Unfortunately, the error for this is pretty vague, but you can see it if you know what to look for. You can see in your logs the line with Message=Class not found in it. It would be better if this line had the name of the class. This error happens when you call the JS for a plugin, but then the right class name can't be found in the plugins.xml file. This is also why you notice that any call fails the same way.
Verify that android studio isn't giving an error like:
A valid Facebook app id must be set in the AndroidManifest.xml or set by calling FacebookSdk.setApplicationId before initializing the sdk.
If so, then edit androidmanifest. See: A valid Facebook app id must be set in the AndroidManifest.xml
Trying to make an app using phonegap 1.4.1 + phonegap's childbrowser plugin.
I've been following guides and digging a lot on the subject, but I'm stuck on an error I can't understand.
I've moved the childbrowser.java to the correct location and added this to the plugins.xml:
<plugin name="ChildBrowser" value="com.phonegap.plugins.childBrowser.ChildBrowser"/>
added this to the androidmanifest.xml:
<activity android:name="com.phonegap.DroidGap" android:label="#string/app_name">
<intent-filter>
</intent-filter>
</activity>
But I get an error on the ChildBrowser.java on this line:
package com.phonegap.plugins.childBrowser;
telling me that the declared package com.phonegap.plugins.childBrowser does not match the expected package com.phonegap.plugins.ChildBrowser.
I changed it to capital C in childbrowser.java, and the error went away, but ofc, the plugin didn't work.
i found this topic, discussing it, but no appearant fix
http://comments.gmane.org/gmane.comp.handhelds.phonegap/11993
I have tried renaming the plugin name in plugins.xml
Earlier, i was trying to whitelist some url's, but kept getting them blocked, so i'm thinking there's something wrong with my /res/xml directory? its meant to be projectroot/res/xml, right?
Any help will be greatly appreciated.
If you've created the package "com.phonegap.plugins.ChildBrowser" in your Eclipse project then you need to modify the plugins.xml line to be:
<plugin name="ChildBrowser" value="com.phonegap.plugins.ChildBrowser.ChildBrowser"/>
to match the actual package of the plugin.
Make sure the ChildBrowser java file is in the package you declared in plugins.xml, which is in root/res/xml/ folder.
You need to add the javascript file to your main page, so the page can call it's methods. Check that the javascript file contents are consistent with you java file location - for example the packages may be different.
Add the right permissions
Make sure you call the plugin from javascript the right way, for example in phonegap 1.3 it goes like this:
window.plugins.childBrowser.onLocationChange = function(loc)
{
...