apache / cordova-android

Apache Cordova Android
https://cordova.apache.org/
Apache License 2.0
3.64k stars 1.54k forks source link

onDeviceReady doesn't run #1725

Open nbruley opened 1 month ago

nbruley commented 1 month ago

Bug Report

Problem

What is expected to happen?

onDeviceReady function should fire

What does actually happen?

For at least some Android devices, the onDeviceReady function does not initiate, although it works fine in Android Studio.

Information

This may relate to this reported unresolved issue: https://github.com/apache/cordova-android/issues/604

Strangely, the issue does not exist on another app I have with, as far as I can tell, exactly the same settings, versions and code, so this may be related to some upgrade step that I missed for a given project. Both apps I recreated from scratch after upgrading.

Command or Code

I tested with this index.html file, without plugins.

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" charset="utf-8" src="resources/jquery.js"></script>
    <script type="text/javascript" src="cordova.js"></script><!--load phonegap script -->
    <!--
    Gradle 8.7
    Android Gradle Plugin 8.5.1
    Jetbrains Runtime 17.0.11
    Target version 34
    Using minify/proguard
    -->
    <script>
      $(document).ready(function(){
          alert('before onDeviceReady');
          document.addEventListener("deviceready", onDeviceReady, false);
          function onDeviceReady() {
              alert('onDeviceReady fired.'); //this doesn't fire for some devices
          }
      });
    </script>
</head>
<body>
<p>This is a test of onDeviceReady.</p>
</body>
</html>

Environment, Platform, Device

Cordova 12.0.0 Gradle 8.7 Android Gradle Plugin 8.5.1 Jetbrains Runtime 17.0.11 Target version 34 Using minify/proguard

Testing with BrowserStack due to reported issues from actual users. The output from BrowserStack log appears to be the same for a working app vs. non-working app. Pixel 6 Pro, Pixel 8, Pixel 5, etc. from Android versions (at least) 11 to 14. My device is a Galaxy Tab A7 SM-T500 and everything works fine on it, so it seems this issue is either device specific or else it happens after going through Google Play Store. (I cannot reproduce the problem in Android Studio so that may be the case.)

Version information

See above. Not using Ionic Windows 11 Android Studio Koala 2024.1.1 Patch 1

Checklist

breautek commented 1 month ago

Two potential causes.

If you removed any plugins, without reintalling them, then you might have triggered known bug in the cordova lib: https://github.com/apache/cordova-lib/issues/927. If you can confirm if this is the case, then I'd prefer to close this as a duplicate.

Using minify/proguard

Using proguard has a high risk of breaking plugins since they are often referred by string and looked up via reflection. So you cannot minify or mangle any cordova plugin symbols, especially the class symbols. If you use proguard, it will be best to exclude the cordova framework, the project activity template code, and cordova plugin code, including third-party cordova plugins.

If you maintain your own plugins, you can separate the plugin logic from the cordova API so that you can proguard the implementation without proguarding the cordova glue bits.

I'd disable proguard & minifying completely temporary to see if that actually solves your issue.

nbruley commented 1 month ago

Thank you for the information.

I'm pretty sure the issue isn't related to proguard directly. I have used that without any issues in the past, and nothing changed there. I just add plugins to a proguard rules file. Those types of issues seem to be evident within Android Studio before publishing. The apps that work use proguard.

The known bug you reference is more promising as a potential reason, although even when completely starting fresh and installing the plugins from the beginning, I still had the issue. I just can't be 100% sure that I didn't uninstall and reinstall a plugin at some point in attempts to troubleshoot the issue. Regardless, the issue was present without having a plugin removed that had been previously installed--one may have been removed and then added back. What I can say is that uninstalling the plugins, then removing the Android platform and then readding the Android platform with the plugins installed via the config file DID resolve the issue.

I note again that onDeviceReady fired fine in Android Studio when testing and emulating on my Android tablet. The problem only presented itself when using the file built by Android Studio in actual devices. I did not see any of the errors mentioned at https://github.com/apache/cordova-lib/issues/927 in the browserstack or Android Studio logs. I still have an example of an app with the issue, and comparing both logcat logs from Android studio yields no significant difference in the one that fails, the best I can see based on a Notepad++ compare evaluation.

breautek commented 1 month ago

Ok, as it stands right now I think it's hard to say if you're experiencing the same cordova-lib bug or not.

Cordova will print a warning if deviceready isn't fired within 5 seconds of cordova initialization, and hitting the warning is usually an indication of a plugin misbehaving.

Most of the time if it does happen, it's a sync issue between plugin metadata (json files that tells the cordova framework which plugins are installed and how to locate them). If cordova cannot find a symbol for a plugin, you should see a logcat message accordingly (e.g. class not found message, which is the exact side-effect of https://github.com/apache/cordova-lib/issues/927).

But if you're not seeing anything and deviceready appears to never fire, then honestly I'm not sure, we'll probably will need a reproduction sample application, but you'll also need to have it reproduce using local simulators ideally. E.g. I can't use browserstack nor I can test uploading apps to google play but I do have an API 29 device on hand.

nbruley commented 1 month ago

I don't think I can reproduce it in Studio, which makes it difficult to troubleshoot. (FYI, I did reproduce it with Google's free Firebase also with Pixel 5.) Would it work to put this on hold until the other issue is resolved and see if that resolution also resolves this one? I don't think it's worth our time trying to fix if the other bug might be fixed soon. Let me know if I need to try to reproduce it again in a new app or if I can feel free to fix my present apps that have the issue vs leaving something that has the bug.