apache / cordova-lib

Apache Cordova Tooling Library
https://cordova.apache.org/
Apache License 2.0
221 stars 242 forks source link

ondeviceready won't fire because plugin does not cleanup after itself properly when removed(?) #927

Open igorsantos07 opened 11 months ago

igorsantos07 commented 11 months ago

Bug Report

Originally reported at apache/cordova-plugin-device#125

Problem

If an application has this plugin installed and, for some reason, it's not needed anymore and removed, ondeviceready won't fire anymore. This seems to be caused by some leftover code... That's probably why the previous reporter mentioned the usual shenanigans of add/remove platform/plugins solved it.

What is expected to happen?

The event should fire as usual, since it's seemingly unrelated to this plugin.

What does actually happen?

ondeviceready never gets fired: image

Logcat displays the following (easier to find if you filter logcat by the package name):

2023-10-14 22:59:13.783 29167-29267 PluginManager           io.cordova.hellocordova              D  exec() call to unknown plugin: Device
2023-10-14 22:59:13.834 29167-29167 chromium                io.cordova.hellocordova              I  [INFO:CONSOLE(80)] "[ERROR] Error initializing cordova-plugin-device: Class not found", source: https://localhost/plugins/cordova-plugin-device/www/device.js (80)
2023-10-14 22:59:18.645 29167-29167 chromium                io.cordova.hellocordova              I  [INFO:CONSOLE(1237)] "deviceready has not fired after 5 seconds.", source: https://localhost/cordova.js (1237)
2023-10-14 22:59:18.645 29167-29167 chromium                io.cordova.hellocordova              I  [INFO:CONSOLE(1230)] "Channel not fired: onCordovaInfoReady", source: https://localhost/cordova.js (1230)

Command or Code

$ cordova create sample-reproduction
$ cd sample-reproduction
$ cordova platform add android
$ cordova plugin add cordova-plugin-device
$ cordova plugin rm cordova-plugin-device
$ cordova run android

Environment, Platform, Device

Version information

Nothing besides what was described above seems relevant.

Checklist

TheNotorius0 commented 1 month ago

I just had this bug too. I updated the plugin using cordova plugin remove cordova-plugin-device --force and then cordova plugin add cordova-plugin-device and I got this bug.

It worked again after I did cordova platform remove android, cordova plugin remove cordova-plugin-device, cordova plugin add cordova-plugin-device, and then cordova platform add android, but it was a bit of a hassle.

breautek commented 1 month ago

I've confirmed that the repro steps reliably reproduces the issue. Seems weird it would only affect this plugin though.

There are 3 main paths (relative to the root of the cordova project) that cordova tracks installed plugins:

  1. /plugins/android.json
  2. /plugins/fetch.json
  3. /platforms/android/platform_www/cordova_plugins.js
  4. also /platforms/android/platform_www/plugins/cordova-plugin-device/

The first 2 is cleaned up as expected. The third one instructs the cordova runtime about what is "installed" and that isn't being cleaned up, so as far as the app runtime is concerned the plugin does exist. The 4th bonus one is the plugin JS code which is also not cleaned up.

The native java code however is cleaned up as expected.

breautek commented 1 month ago

Some additional information I've found when testing other plugins and will explain:

We also removed other plugins and didn't had this kind of trouble.

So first this bug does occur on all plugins it seems like. However only the last plugin removed is left in a "broken" state.

So what I did was did the steps to reproduce the issue on the device plugin, and had my environment in a broken state with the device plugin.

I repeated the steps for the file plugin. When I removed the file plugin, it ended up "correcting" the state with the device plugin, but the file plugin state was broken (all the file plugin JS modules and module entries were lingered).

Repeating the steps for the device plugin again, fixed the broken state of the file plugin, but the broken state of the device plugin returned.

This bug is probably more suitable for cordova-lib so I'll move it.

TheNotorius0 commented 1 month ago

The bug is reproducible on the iOS platform as well, not just on Android.