Android HTML 应用程序:如何使用 WebViewAssetLoader 允许多个 JS 文件?
我正在使用 WebView 开发一个带有 HTML5 画布、CSS 和 JavaScript 的 Android 应用程序。所有内容都存储在设备本地。我使用这个简短的视频教程制作了一个简单、可运行的应用程序。简单的应用程序仅包含两个文件(除了 HTML、CSS 和 JS):
MainActivity.java:
package com.example.myappname;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setLayerType(WebView.LAYER_TYPE_HARDWARE, null);
webView.loadUrl("file:///android_asset/index.html");
}
}
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webview"/>
</LinearLayout>
当我扩展应用程序以包含多个 JS 文件时,由于 同源策略。
从这篇文章看来,标志allowFileAccessFromFileURLs
已弃用,根据 Android 官方文档,您现在必须使用 WebViewAssetLoader
来安全地加载本地内容。关于使用 WebViewAssetLoader 加载应用内内容的文档提供了一些代码片段来演示它是如何完成的,这是第一个:
private static class LocalContentWebViewClient extends WebViewClientCompat {
private final WebViewAssetLoader mAssetLoader;
LocalContentWebViewClient(WebViewAssetLoader assetLoader) {
mAssetLoader = assetLoader;
}
@Override
@RequiresApi(21)
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
return mAssetLoader.shouldInterceptRequest(request.getUrl());
}
@Override
@SuppressWarnings("deprecation") // to support API < 21
public WebResourceResponse shouldInterceptRequest(WebView view,
String url) {
return mAssetLoader.shouldInterceptRequest(Uri.parse(url));
}
}
我是一名经验丰富的开发人员,但这是我的第一个 Android 项目,我之前没有 Java 或 Kotlin 的经验。该文档没有提供大量上下文,并假设读者知道将片段放置在哪里。我需要一些关于在哪里放置代码以及如何使用它的指导。
I am developing an android app with HTML5 canvas, CSS, and JavaScript, using WebView. All content is stored locally on the device. I have made a simple, working app using this short video tutorial. The bare-bones app contains just two files (apart from HTML, CSS, and JS):
MainActivity.java:
package com.example.myappname;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setLayerType(WebView.LAYER_TYPE_HARDWARE, null);
webView.loadUrl("file:///android_asset/index.html");
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webview"/>
</LinearLayout>
When I expanded the app to include several JS files, I ran into CORS errors due to the same-origin policy.
From this post it appears that the flag allowFileAccessFromFileURLs
is deprecated, and according to the official Android documentation you now have to use WebViewAssetLoader
to load local content safely. The documentation on loading in-app content with WebViewAssetLoader provides several code snippets to demonstrate how it is done, this being the first:
private static class LocalContentWebViewClient extends WebViewClientCompat {
private final WebViewAssetLoader mAssetLoader;
LocalContentWebViewClient(WebViewAssetLoader assetLoader) {
mAssetLoader = assetLoader;
}
@Override
@RequiresApi(21)
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
return mAssetLoader.shouldInterceptRequest(request.getUrl());
}
@Override
@SuppressWarnings("deprecation") // to support API < 21
public WebResourceResponse shouldInterceptRequest(WebView view,
String url) {
return mAssetLoader.shouldInterceptRequest(Uri.parse(url));
}
}
I am an experienced developer, but this is my first Android project, and I have no prior experience with Java or Kotlin. The documentation does not provide a lot of context and assumes that the reader knows where to place the snippets. I need some guidance on where to place the code, and how to use it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很晚了,但仍然:您可以通过 http(s) 加载本地文件:使用 WebViewAssetLoader
请参阅 https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader,它从 https://appassets.androidplatform.net,它不是真正的主机,但为您的本地文件提供服务。
quite late, but still: you CAN load local files via http(s): using WebViewAssetLoader
see the example at https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader, it loads local resources from https://appassets.androidplatform.net, which is NOT a real host but serves your local files.