Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: I migrated the cordova version, and index.html will be lost after hot update. But when the app is opened again, index.html returns to normal. #442

Open
waliu opened this issue Aug 26, 2024 · 19 comments

Comments

@waliu
Copy link

waliu commented Aug 26, 2024

Feature Request

Description

After many updates, occasionally when entering the plug-in, a prompt will appear that the version is built-in. Index.html will be lost. I guess the reason is that I didn't set setServerBasePath and setServerAssetPath, but cordova I don't know how to set

Open the APP again, and index.html magically appears again.

Platform(s)

cordova 13.0.0
Android

Preferred Solution

Can you help me deal with this error?
How can I contact you?
My Discord account : galen_90942
Thanks

@waliu
Copy link
Author

waliu commented Aug 26, 2024

lost index.html app
image
image

Restart app Normal display
image

@waliu
Copy link
Author

waliu commented Aug 26, 2024

 public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    this.counterActivityCreate++;
    this.prefs = this.cordova.getContext()
      .getSharedPreferences(WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE);
    this.editor = this.prefs.edit();

    try {
      this.implementation = new CapacitorUpdater() {
        @Override
        public void notifyDownload(final String id, final int percent) {
          HotUpdate.this.notifyDownload(id, percent);
        }

        @Override
        public void directUpdateFinish(final BundleInfo latest) {
          try {
            HotUpdate.this.directUpdateFinish(latest);
          } catch (JSONException e) {
            throw new RuntimeException(e);
          }
        }

        @Override
        public void notifyListeners(final String id, final JSONObject res) {
          HotUpdate.this.notifyListeners(id, res);
        }
      };
      final PackageInfo pInfo = this.cordova.getContext().getPackageManager().getPackageInfo(this.cordova.getContext().getPackageName(), 0);
      this.implementation.activity = this.cordova.getActivity();
// TODO I suspect the error is here 
      this.implementation.versionBuild = preferences.getString("version", pInfo.versionName);
      this.implementation.PLUGIN_VERSION = this.PLUGIN_VERSION;
      this.implementation.versionCode = Integer.toString(pInfo.versionCode);
      this.implementation.requestQueue = Volley.newRequestQueue(
        this.cordova.getContext()
      );
      final String signKeyStr = preferences.getString("signKey", "");
      if (signKeyStr.length() > 0) {
        this.implementation.signKey = CryptoCipher.stringToPublicKey(
          signKeyStr
        );
      }

      this.implementation.directUpdate = preferences.getBoolean("directUpdate", false);
      this.currentVersionNative = new Version(preferences.getString("version", pInfo.versionName));
    } catch (final PackageManager.NameNotFoundException e) {
      Log.e(CapacitorUpdater.TAG, "Error instantiating implementation", e);
      return;
    } catch (final Exception e) {
      Log.e(
        CapacitorUpdater.TAG,
        "Error getting current native app version",
        e
      );
      return;
    }

    this.implementation.appId = InternalUtils.getPackageName(
      this.cordova.getContext().getPackageManager(),
      this.cordova.getContext().getPackageName()
    );
    this.implementation.appId = preferences.getString("appId", this.implementation.appId);
    if (this.implementation.appId == null || this.implementation.appId.isEmpty()) {
      // crash the app
      throw new RuntimeException(
        "appId is missing in capacitor.config.json or plugin config, and cannot be retrieved from the native app, please add it globally or in the plugin config"
      );
    }
    Log.i(CapacitorUpdater.TAG, "appId: " + implementation.appId);
    this.implementation.privateKey = preferences.getString("privateKey", defaultPrivateKey);
    this.implementation.statsUrl = preferences.getString("statsUrl", statsUrlDefault);
    this.implementation.channelUrl = preferences.getString("channelUrl", channelUrlDefault);
    int userValue = preferences.getInteger("periodCheckDelay", 0);
    this.implementation.defaultChannel = preferences.getString("defaultChannel", "");
    if (userValue >= 0 && userValue <= 600) {
      this.periodCheckDelay = 600 * 1000;
    } else if (userValue > 600) {
      this.periodCheckDelay = userValue * 1000;
    }

    this.implementation.documentsDir = this.cordova.getContext().getFilesDir();
    this.implementation.prefs = this.prefs;
    this.implementation.editor = this.editor;
    this.implementation.versionOs = Build.VERSION.RELEASE;
    this.implementation.deviceID = this.prefs.getString(
      "appUUID",
      UUID.randomUUID().toString()
    );
    this.editor.putString("appUUID", this.implementation.deviceID);
    this.editor.commit();
    Log.i(
      CapacitorUpdater.TAG,
      "init for device " + this.implementation.deviceID
    );
    Log.i(
      CapacitorUpdater.TAG,
      "version native " + this.currentVersionNative.getOriginalString()
    );
    this.autoDeleteFailed = preferences
      .getBoolean("autoDeleteFailed", true);
    this.autoDeletePrevious = preferences
      .getBoolean("autoDeletePrevious", true);
    this.updateUrl = preferences.getString("updateUrl", updateUrlDefault);
    this.autoUpdate = preferences.getBoolean("autoUpdate", true);
    this.appReadyTimeout = preferences.getInteger("appReadyTimeout", 10000);
    this.implementation.timeout = preferences.getInteger("responseTimeout", 20) * 1000;
    boolean resetWhenUpdate = preferences.getBoolean("resetWhenUpdate", true);
    this.implementation.autoReset();
    if (resetWhenUpdate) {
      this.cleanupObsoleteVersions();
    }
    this.checkForUpdateAfterDelay();
  }

  private void semaphoreWait(Number waitTime) {
    Log.i(CapacitorUpdater.TAG, "semaphoreWait " + waitTime);
    try {
      //        Log.i(CapacitorUpdater.TAG, "semaphoreReady count " + CapacitorUpdaterPlugin.this.semaphoreReady.getCount());
      semaphoreReady.awaitAdvanceInterruptibly(
        semaphoreReady.getPhase(),
        waitTime.longValue(),
        TimeUnit.SECONDS
      );
      //        Log.i(CapacitorUpdater.TAG, "semaphoreReady await " + res);
      Log.i(
        CapacitorUpdater.TAG,
        "semaphoreReady count " + semaphoreReady.getPhase()
      );
    } catch (InterruptedException e) {
      Log.i(CapacitorUpdater.TAG, "semaphoreWait InterruptedException");
      e.printStackTrace();
    } catch (TimeoutException e) {
      throw new RuntimeException(e);
    }
  }

protected boolean _reload() {
    final String path = this.implementation.getCurrentBundlePath();
    this.semaphoreUp();
    Log.i(CapacitorUpdater.TAG, "Reloading: " + path);
    if (this.implementation.isUsingBuiltin()) {
// TODO I suspect the error is here 
//      this.bridge.setServerAssetPath(path);
    } else {
// TODO I suspect the error is here 
//      this.bridge.setServerBasePath(path);
    }
    this.checkAppReady();
    this.notifyListeners("appReloaded", new JSONObject());
    return true;
  }

I suspect the error is here
but i can't solve it
How to set up Cordoba Set server base path

@riderx
Copy link
Collaborator

riderx commented Aug 26, 2024

Wait you mean you back ported the code in cordova ?
I think it will be way easier for you to migrate to capacitor, we tried to make the plugin for cordova but that hard things do not work the same in cordova :/

@waliu
Copy link
Author

waliu commented Aug 26, 2024

Yes, our project cannot be quickly migrated to Capacitor because it has heavy historical baggage. So a cordova plugin is needed. I know capacitor development plugin is much simpler.

@waliu
Copy link
Author

waliu commented Aug 26, 2024

At present, we can only complete the cordova plug-in first, and then do the capacitor migration when time is enough.

@waliu
Copy link
Author

waliu commented Aug 26, 2024

In fact, hot updates have been implemented。Currently, index.html is missing in the builtin version. So I would like your help.

@WcaleNieWolny
Copy link
Contributor

Let me confirm, you have been able to hot-reload but after you reopen the app the update doesn't persist?

What platform (IOS/Android) do you use?

@WcaleNieWolny
Copy link
Contributor

Oh, I see the issue. Let me look for an IMPL

@WcaleNieWolny
Copy link
Contributor

I would likely try to use this method

Although, the cordova-plugin-code-push is deprecated I would still give it a try. Other than this plugin I don't know where you could look for some code

@waliu
Copy link
Author

waliu commented Aug 29, 2024

@WcaleNieWolny
It was because cordova-plugin-code-push was no longer maintained that I migrated from the capacitive updater to cordova. Although capacitors are now recommended, I had to get cordova running. I have solved the Cordoba Fever related issues.

@waliu
Copy link
Author

waliu commented Aug 29, 2024

@WcaleNieWolny
The plug-in is made according to capacitor-updater.

@WcaleNieWolny
Copy link
Contributor

Would you mind sharing your solution for using capacitor updater with Cordova? How did you end up resolving the issue?

@waliu
Copy link
Author

waliu commented Aug 29, 2024

I will consider sharing it later

@waliu
Copy link
Author

waliu commented Aug 29, 2024

Currently I have only migrated the Android version

@WcaleNieWolny
Copy link
Contributor

That is okay, your knowledge on this issue is still really useful.

@waliu
Copy link
Author

waliu commented Aug 29, 2024

Excuse me, after the capacitor-updater is hot updated, can it be restarted and loaded with index.html?

@WcaleNieWolny
Copy link
Contributor

Yes, I recommend copying the IOS initialLoad logic into android

@riderx
Copy link
Collaborator

riderx commented Aug 29, 2024

@waliu let's create a repo and @WcaleNieWolny can help you make it happen

@riderx
Copy link
Collaborator

riderx commented Sep 16, 2024

@waliu where are you with this ? did you made it work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants