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

Make MAUI blazor which required android api 24 compatible to api 21 #8375

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions eng/AndroidX.targets
Expand Up @@ -61,5 +61,10 @@
Update="Xamarin.AndroidX.Window.WindowJava"
Version="1.0.0.9"
/>
<PackageReference
Update="Xamarin.AndroidX.WebKit"
Version="1.4.0.8"
/>
Comment on lines +64 to +67
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is an Update, how does this line do anything? It must already be a transitive dependency, and we can remove it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javiercn would you mind filing an issue first explaining the motivation for the change as well as the impact so that the team can assess whether or not we want to take this change in?

Please forgive me for not having time to reply a few days ago.

This is not necessary, but just a suggestion for the following reasons.

  1. From Android API 24 to 23, it is not difficult for Android. They are all official.

  2. API 21 app will run on approximately 98.6% of devices, and API 24 app only has 91.7%. The is from Android studio.

  3. Using androidx.webkit increases the size of Android installation package a little and the performance loss is almost negligible.

  4. It makes the Android device compatibility of Maui Blazor consistent with Maui.

  5. My test method is temporarily modified the namespace of Microsoft.AspNetCore.Components.WebView.Maui.csproj and only compile it with min Android API 21, so that it can be directly added to the Maui blazor app project, and then tested on the real machine of Android 5(API 21). The startup speed is acceptable, which is 40% faster than blazor WebAssembly. This is stupid, but simple and effective.


</ItemGroup>
</Project>
Expand Up @@ -4,6 +4,7 @@
using System.Runtime.Versioning;
using System.Threading.Tasks;
using Android.Webkit;
using AndroidX.WebKit;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
Expand All @@ -16,7 +17,7 @@ namespace Microsoft.AspNetCore.Components.WebView.Maui
/// An implementation of <see cref="WebViewManager"/> that uses the Android WebKit WebView browser control
/// to render web content.
/// </summary>
[SupportedOSPlatform("android23.0")]
[SupportedOSPlatform("android21.0")]
Comment on lines -19 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove these attributes now since we have a min version in .NET itself that is 21. .NET 6 does not run on lower than 21.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

internal class AndroidWebKitWebViewManager : WebViewManager
{
// Using an IP address means that WebView doesn't wait for any DNS resolution,
Expand Down Expand Up @@ -63,7 +64,7 @@ protected override void NavigateCore(Uri absoluteUri)
/// <inheritdoc />
protected override void SendMessage(string message)
{
_webview.PostWebMessage(new WebMessage(message), AndroidAppOriginUri);
WebViewCompat.PostWebMessage(_webview, new WebMessageCompat(message), AndroidAppOriginUri);
}

internal bool TryGetResponseContentInternal(string uri, bool allowFallbackOnHostPage, out int statusCode, out string statusMessage, out Stream content, out IDictionary<string, string> headers)
Expand All @@ -76,7 +77,7 @@ internal bool TryGetResponseContentInternal(string uri, bool allowFallbackOnHost
internal void SetUpMessageChannel()
{
// These ports will be closed automatically when the webview gets disposed.
var nativeToJSPorts = _webview.CreateWebMessageChannel();
var nativeToJSPorts = WebViewCompat.CreateWebMessageChannel(_webview);

var nativeToJs = new BlazorWebMessageCallback(message =>
{
Expand All @@ -87,10 +88,10 @@ internal void SetUpMessageChannel()

nativeToJSPorts[0].SetWebMessageCallback(nativeToJs);

_webview.PostWebMessage(new WebMessage("capturePort", destPort), AndroidAppOriginUri);
WebViewCompat.PostWebMessage(_webview, new WebMessageCompat("capturePort", destPort), AndroidAppOriginUri);
}

private class BlazorWebMessageCallback : WebMessagePort.WebMessageCallback
private class BlazorWebMessageCallback : WebMessagePortCompat.WebMessageCallbackCompat
{
private readonly Action<string?> _onMessageReceived;

Expand All @@ -99,7 +100,7 @@ public BlazorWebMessageCallback(Action<string?> onMessageReceived)
_onMessageReceived = onMessageReceived ?? throw new ArgumentNullException(nameof(onMessageReceived));
}

public override void OnMessage(WebMessagePort? port, WebMessage? message)
public override void OnMessage(WebMessagePortCompat? port, WebMessageCompat? message)
{
if (message is null)
{
Expand All @@ -108,6 +109,7 @@ public override void OnMessage(WebMessagePort? port, WebMessage? message)

_onMessageReceived(message.Data);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra line

}
}
}
Expand Up @@ -8,7 +8,7 @@

namespace Microsoft.AspNetCore.Components.WebView.Maui
{
[SupportedOSPlatform("android23.0")]
[SupportedOSPlatform("android21.0")]
internal class WebKitWebViewClient : WebViewClient
{
// Using an IP address means that WebView doesn't wait for any DNS resolution,
Expand Down
2 changes: 1 addition & 1 deletion src/BlazorWebView/src/Maui/BlazorWebView.cs
Expand Up @@ -51,7 +51,7 @@ public BlazorWebView()

/// <inheritdoc />
#if ANDROID
[System.Runtime.Versioning.SupportedOSPlatform("android23.0")]
[System.Runtime.Versioning.SupportedOSPlatform("android21.0")]
#elif IOS
[System.Runtime.Versioning.SupportedOSPlatform("ios11.0")]
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/BlazorWebView/src/Maui/BlazorWebViewHandler.cs
Expand Up @@ -9,7 +9,7 @@
namespace Microsoft.AspNetCore.Components.WebView.Maui
{
#if ANDROID
[SupportedOSPlatform("android23.0")]
[SupportedOSPlatform("android21.0")]
#endif
public partial class BlazorWebViewHandler
{
Expand Down
Expand Up @@ -30,7 +30,7 @@ public static IWindowsFormsBlazorWebViewBuilder AddWindowsFormsBlazorWebView(thi
public static IWpfBlazorWebViewBuilder AddWpfBlazorWebView(this IServiceCollection services)
#elif WEBVIEW2_MAUI
#if ANDROID
[System.Runtime.Versioning.SupportedOSPlatform("android23.0")]
[System.Runtime.Versioning.SupportedOSPlatform("android21.0")]
#elif IOS
[System.Runtime.Versioning.SupportedOSPlatform("ios11.0")]
#endif
Expand Down