Eilon / MauiHybridWebView

MIT License
206 stars 45 forks source link

MauiHybridWebView causes Android app to crash with R8 enabled - Any fix to make it R8 compatible? Bug project included to demonstrate. #66

Closed jonmdev closed 2 months ago

jonmdev commented 4 months ago

Bug Description

Apps featuring MauiHybridWebView always run perfectly on the first app start (whether R8 is enabled or disabled).

However, if Android stops the app in the background (ie. OnStop() occurs) and restarts the app in the background, it then always crashes when R8 is enabled. When R8 is disabled, it restarts smoothly.

How to Trigger Android App Restart

To make Android stop and restart the app and demonstrate this:

Using this method, every time, when the app is built with R8 in Release mode and Android tries to restart it, it crashes.

Bug Project

https://github.com/jonmdev/R8CrashBug

The Bug Project to demonstrate is just a default Maui app with MauiHybridWebView added. A simple index.html is copied and pasted in to the Resources folder. Main.xaml.cs is changed to:

namespace R8CrashBug {
    public partial class App : Application {
        public App() {

            ContentPage mainPage = new();
            this.MainPage = mainPage;

            AbsoluteLayout absRoot = new();
            mainPage.Content = absRoot; 

            HybridWebView.HybridWebView hybridWebView = new();
            hybridWebView.HybridAssetRoot = "hybridview";
            hybridWebView.MainFile = "index.html";
            hybridWebView.JSInvokeTarget = this;

            absRoot.Add(hybridWebView);
        }   
    }
}

No other changes are made.

Steps To Reproduce

1) Open Developer Options in Android Phone and set Background Process Limit to 0-1 range. 2) Build Bug Project in Release to Android device with R8 Enabled (as it is by default). 3) Press ||| button on Android bottom tray and navigate to a few other open apps sequentially (this will trigger Android to run onStop() and stop the Maui App process in the background). 4) Press ||| button on Android bottom tray and navigate back to the Maui App. 5) With R8 enabled, it will always then briefly show the splash icon and crash. 6) If you repeat the above steps with R8 disabled, no crash occurs - it will instead restart the app smoothly as expected.

I also note the plain default Maui app (without MauiHybridWebView) does not crash with R8 enabled or disabled. It only crashes with R8 enabled once MauiHybridWebView has been added. Thus this is specifically provoked by the addition of MauiHybridWebView.

Outlook

MauiHybridWebView is an absolute game changer for me which has solved some major problems I otherwise had no solution for. I absolutely love the project and appreciate your work on it. But we cannot have it crashing like this in real projects for rlease and R8 is necessary for modern Android release.

Any help or outlook on a fix would be very, very appreciated.

Thanks again @Eilon for your great work on the project and any solution you can offer.

jonmdev commented 3 months ago

For reference, I have investigated further after looking more into how MauiHybridWebView works. I saw MauiHybridWebView is essentially just inherited from WebView, so I tested, and the problem is triggered by WebView also. This suggests the problem must be fixed in WebView more fundamentally than MauiHybridWebView.

WebView bug report then posted here: https://github.com/dotnet/maui/issues/23027