RC0: "plugin is not installed", even though it is ... (Android only)

After upgrading my beta.11 app to the latest version RC0 (2.0.0-rc.0) I am having problems using native plugins on Android. On iOS the plugins are working.

See my log:
09-30 11:49:43.422 20751 20751 I art : Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.ServiceWorkerControllerAdapter>
09-30 11:49:43.422 20751 20751 I art : Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.ServiceWorkerControllerAdapter>
09-30 11:49:43.422 20751 20751 I art : Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.TokenBindingManagerAdapter>
09-30 11:49:43.422 20751 20751 I art : Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.TokenBindingManagerAdapter>
09-30 09:39:09.169 685 685 I chromium: [INFO:CONSOLE(22)] “Native: tried calling t.getCurrentHeading, but the t plugin is not installed.”, source: file:///android_asset/www/build/main.js (22)
09-30 09:39:09.169 685 685 I chromium: [INFO:CONSOLE(22)] “Install the t plugin: ‘ionic plugin add cordova-plugin-device-orientation’”, source: file:///android_asset/www/build/main.js (22)
09-30 09:39:09.239 685 685 I chromium: [INFO:CONSOLE(27)] “plugin_not_installed”, source: file:///android_asset/www/build/main.js (27)

So created a fresh new app to test native plugins, but I am still having the same problem.

Here is my environment:
Cordova CLI: 6.3.1
Ionic Framework Version: 2.0.0-rc.0
Ionic CLI Version: 2.1.0
Ionic App Lib Version: 2.1.0-beta.1
ios-deploy version: 1.9.0
ios-sim version: 5.0.8
OS: Mac OS X El Capitan
Node Version: v4.6.0
Xcode version: Xcode 8.0 Build version 8A218a

Here is what I did:

I created a new ionic 2 app:
ionic start --v2 TestApp2RC

added the plugin:
ionic plugin add cordova-plugin-device-orientation

When I call “ionic plugins”, I see this:
cordova-plugin-console 1.0.4 "Console"
cordova-plugin-device 1.1.3 "Device"
cordova-plugin-device-orientation 1.0.4 "Device Orientation"
cordova-plugin-globalization 1.0.4 "Globalization"
cordova-plugin-splashscreen 4.0.0 "Splashscreen"
cordova-plugin-statusbar 2.2.0 "StatusBar"
cordova-plugin-whitelist 1.3.0 "Whitelist"
ionic-plugin-keyboard 2.2.1 “Keyboard”

Then I use it according to the documentation:
import { DeviceOrientation, CompassHeading } from ‘ionic-native’;

and then within the constructor:
// Get the device current compass heading
DeviceOrientation.getCurrentHeading().then(
(data: CompassHeading) => console.log(data),
(error: any) => console.log(error)
);

After building and running the app, it says in my logs: plugin is not installed (se logs above)

Any help is appreciated,
Phil

Did you already tried to remove the plugin and add it back in? Or removing the platform and adding it back in for Android only?

UPDATE (SOLVED): The error only happens when the plugin call

DeviceOrientation.getCurrentHeading().then(
(data: CompassHeading) => console.log(data),
(error: any) => console.log(error)
);

is in the constructor of a page, without waiting for the platform to be ready.

It works if I wrap the the plugin call code in the platform.ready().

constructor(public navCtrl: NavController, platform: Platform) {

      platform.ready().then(() => {

        console.log("HOME XXXXXXX PLATFORM READY!");

        // get the preferred language      
        Globalization.getPreferredLanguage().then((p) => {
          var preferredLangage = p.value.substring(0, 2);
          console.log("preferredLangage retrieved: "+preferredLangage);
        });

        // Get the device current compass heading
        DeviceOrientation.getCurrentHeading().then(
          (data: CompassHeading) => console.log(data),
          (error: any) => console.log(error)
        );


      });


  }

This is perhaps a new “feature” of RC0 to make it faster. This issue is solved for me.
(Also: the issue happens on iOS as well)

It’s a best practice to not put difficult or important code inside your constructor (better for testing). And yes you should be sure that device code is wrapped inside a device ready event if you’re not sure that your device is ready to prevent premature loading. Good to know it’s fixed now!