Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova plugins not working

Edit for clarification: The <name> you see in some of the files below is not actually part of the files, but it includes the company name which I had to censor.

Test situation:

  • Cordova 4
  • OS X Yosemite
  • Android 4.4 device (coupled with USB)
  • Using Terminal; not Eclipse

What happens: Using cordova run --release successfully builds and signs the .apk and the mobile device installs and opens it without issues. But, none of the plugins work. For example:

    title.on('click', function() {
        if (!navigator.camera) {
            alert('no camera!');
        } else {
            navigator.camera.getPicture(function success(data) {
            // do something
            }, function error(data) {});
        }
    });

Always alerts that there is no camera, which means Cordova did not add a camera object to the window.navigator object.

Below is some information about my cordova installation and some xml files, as far as I can tell nothing looks out of place. Note that it is not just the camera that does not work, I just used it as an example.

Cordova info:

Node version: v0.10.29

Cordova version: 4.0.1-nightly.2014.9.29

Config.xml file: 

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.<name>.App" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name><name> Mini</name>
    <description>
        De <name> app voor je telefoon
    </description>
    <author email="stephan@<name>.nl" href="https://<name>.nl">
        Het <name> Team
    </author>
    <content src="index.html" />
    <access origin="*" />
    <preference name="SplashScreen" value="screen" />
    <preference name="SplashScreenDelay" value="2000" />
</widget>


Plugins: 

org.apache.cordova.camera,org.apache.cordova.dialogs,org.apache.cordova.file,org.apache.cordova.file-transfer,org.apache.cordova.media,org.apache.cordova.media-capture,org.apache.cordova.splashscreen,org.apache.cordova.vibration

Android platform:

Available Android targets:
----------
id: 1 or "android-19"
     Name: Android 4.4.2
     Type: Platform
     API level: 19
     Revision: 4
     Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in
 Tag/ABIs : default/armeabi-v7a, default/x86

cordova/config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.<name>.App" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name><name> Mini</name>
    <description>
        De <name> app voor je telefoon
    </description>
    <author email="stephan@<name>.nl" href="https://<name>.nl">
        Het <name> Team
    </author>
    <content src="index.html" />
    <access origin="*" />
    <preference name="SplashScreen" value="screen" />
    <preference name="SplashScreenDelay" value="2000" />
</widget>

cordova/platforms/android/AndroidManifest.xml <

?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.kaartje2go.App" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="CordovaApp" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.VIBRATE" />
</manifest>

cordova/platforms/android/res/xml/config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.<name>.App" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <preference name="loglevel" value="DEBUG" />
    <feature name="Camera">
        <param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
    </feature>
    <feature name="Notification">
        <param name="android-package" value="org.apache.cordova.dialogs.Notification" />
    </feature>
    <feature name="File">
        <param name="android-package" value="org.apache.cordova.file.FileUtils" />
        <param name="onload" value="true" />
    </feature>
    <feature name="FileTransfer">
        <param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" />
    </feature>
    <feature name="Media">
        <param name="android-package" value="org.apache.cordova.media.AudioHandler" />
    </feature>
    <feature name="Capture">
        <param name="android-package" value="org.apache.cordova.mediacapture.Capture" />
    </feature>
    <feature name="SplashScreen">
        <param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" />
    </feature>
    <feature name="Vibration">
        <param name="android-package" value="org.apache.cordova.vibration.Vibration" />
    </feature>
    <name><name> Mini</name>
    <description>
        De <name> app voor je telefoon
    </description>
    <author email="stephan@<name>.nl" href="https://<name>.nl">
        Het <name> Team
    </author>
    <content src="index.html" />
    <access origin="*" />
    <preference name="SplashScreen" value="screen" />
    <preference name="SplashScreenDelay" value="2000" />
</widget>
like image 781
Stephan Bijzitter Avatar asked Oct 30 '14 13:10

Stephan Bijzitter


People also ask

Is Cordova deprecated?

We are hereby announcing the deprecation of cordova-osx. This means that the Cordova development community will not be doing any more work on this platform. Please migrate to the cordova-electron platform or try Mac Catalyst with the cordova-ios platform.

Do Cordova plugins work with Capacitor?

This is why Capacitor is able to utilise Cordova plugins, because the idea is fundamentally the same. Capacitor can just take the same native files that the Cordova plugin is using and add them to the Capacitor project.


2 Answers

Checklist: Is your plugin loaded?

  • Does your index.html load the cordova.js script? (i.e. if you have www/index.html then you can just reference cordova.js. This file is not in the www directory, but automatically generated by Cordova and inserted in your app)
  • Is the plugin javascript loaded? Use Chrome or Safari to remote debug your android/iOS app.
  • cordova plugin ls: Is your plugin listed?
  • plugins/android.json: Is your plugin listed?
  • Does platforms/android/assets/www have all files? (i.e. a plugins folder with scripts, cordova_plugins.js should mention your plugin, a platform-specific cordova.js)

Can you listen to the deviceready event to check if Cordova initializes at all? (If all scripts are in place)

like image 72
markmarijnissen Avatar answered Oct 03 '22 23:10

markmarijnissen


If your plugins don't work you can also try these steps:

cordova plugin list;
cordova plugin remove my-cordova-plugin --save;
cordova plugin add my-cordova-plugin --nosave;

Also be aware of the following plugin dependencies:

  1. The file, file-transfer, and camera plugins are all dependent on the compat plugin; so compat needs to be removed last.
  2. The file-transfer plugin is dependent on file, so file needs to be removed first.
like image 36
tim-montague Avatar answered Oct 03 '22 21:10

tim-montague