赞
踩
最近客户提出一个问题,就是由于Android的内置版本WebView版本过低,用户希望能使用高版本的WebView。这个问题有两种解决办法,第一就是系统将内置WebView版本提高,第二就是客户需要使用什么版本的WebView,自行下载然后固件将客户下载的版本作为默认版本。下面分别介绍在Android 5.1和7.1的版本上面的实施步骤。
该版本使用客户下载的谷歌WebView作为默认的WebView版本,比较简单,原理就是加载默认WebView的时候判断当前是否有谷歌的WebView,如果有就是用谷歌的。
diff --git a/frameworks/base/core/java/android/webkit/WebViewFactory.java b/frameworks/base/core/java/android/webkit/WebViewFactory.java old mode 100644 new mode 100755 index 474ef422d0..6f652ae061 --- a/frameworks/base/core/java/android/webkit/WebViewFactory.java +++ b/frameworks/base/core/java/android/webkit/WebViewFactory.java @@ -69,6 +69,8 @@ public final class WebViewFactory { private static final boolean DEBUG = false; + private static final String GOOGLE_WEBVIEW_NAME = "com.google.android.webview"; + // Cache the factory both for efficiency, and ensure any one process gets all webviews from the // same provider. private static WebViewFactoryProvider sProviderInstance; @@ -77,10 +79,31 @@ public final class WebViewFactory { private static PackageInfo sPackageInfo; public static String getWebViewPackageName() { + if(isGoogleWebViewExist()){ + return GOOGLE_WEBVIEW_NAME; + } return AppGlobals.getInitialApplication().getString( com.android.internal.R.string.config_webViewPackageName); } + public static boolean isGoogleWebViewExist(){ + Application initialApplication = AppGlobals.getInitialApplication(); + PackageInfo packageInfo; + try { + packageInfo = initialApplication.getPackageManager().getPackageInfo(GOOGLE_WEBVIEW_NAME, 0); + }catch(Exception e){ + packageInfo = null; + Log.e(LOGTAG, "error instantiating provider", e); + } + + if(packageInfo == null){ + return false; + }else{ + return true; + } + + } +
该版本使用客户下载的谷歌WebView作为默认的WebView版本,修改逻辑要比5.xx版本复杂,但是原理是一样的就是加载默认WebView的时候判断当前是否有谷歌的WebView,如果有就是用谷歌的,反之就是用系统默认的版本。
diff --git a/frameworks/base/core/res/res/xml/config_webview_packages.xml b/frameworks/base/core/res/res/xml/config_webview_packages.xml old mode 100644 new mode 100755 index f062b59a00..5d51f62a0f --- a/frameworks/base/core/res/res/xml/config_webview_packages.xml +++ b/frameworks/base/core/res/res/xml/config_webview_packages.xml @@ -16,6 +16,8 @@ <webviewproviders> <!-- The default WebView implementation --> + <webviewprovider description="Google WebView" packageName="com.google.android.webview"> + </webviewprovider> <webviewprovider description="Android WebView" packageName="com.android.webview" availableByDefault="true"> </webviewprovider> </webviewproviders> diff --git a/frameworks/base/services/core/java/com/android/server/webkit/WebViewUpdateService.java b/frameworks/base/services/core/java/com/android/server/webkit/WebViewUpdateService.java old mode 100644 new mode 100755 index 846169cbf9..58bce04a69 --- a/frameworks/base/services/core/java/com/android/server/webkit/WebViewUpdateService.java +++ b/frameworks/base/services/core/java/com/android/server/webkit/WebViewUpdateService.java @@ -85,6 +85,13 @@ public class WebViewUpdateService extends SystemService { } break; case Intent.ACTION_PACKAGE_ADDED: + + String pkgName = packageNameFromIntent(intent); + if(pkgName != null && pkgName.equals("com.google.android.webview")){ + Slog.i(TAG,"change webview to "+pkgName); + mImpl.changeProviderAndSetting(packageNameFromIntent(intent)); + } + mImpl.packageStateChanged(packageNameFromIntent(intent), (intent.getExtras().getBoolean(Intent.EXTRA_REPLACING) ? PACKAGE_ADDED_REPLACED : PACKAGE_ADDED), userId); diff --git a/frameworks/base/services/core/java/com/android/server/webkit/WebViewUpdateServiceImpl.java b/frameworks/base/services/core/java/com/android/server/webkit/WebViewUpdateService Impl.java old mode 100644 new mode 100755 index 2cf17229a5..a390ca9c7e --- a/frameworks/base/services/core/java/com/android/server/webkit/WebViewUpdateServiceImpl.java +++ b/frameworks/base/services/core/java/com/android/server/webkit/WebViewUpdateServiceImpl.java @@ -630,6 +630,11 @@ public class WebViewUpdateServiceImpl { if (systemInterface.systemIsDebuggable()) { return true; } + + if(packageInfo != null && packageInfo.packageName.equals("com.google.android.webview")){ + return true; + } + Signature[] packageSignatures; // If no signature is declared, instead check whether the package is included in the // system. (END)
如上就是在Android 5.xx和7.xx版本上面相关实施的git提交记录,即使用客户下载的谷歌WebView作为默认WebView的功能。当然前提也参见了一些博客,譬如WebView如何加载的,下面放上相关的博客链接。
https://www.jianshu.com/p/dea3743da4aa
https://blog.csdn.net/ansondroider/article/details/88892932
https://www.cnblogs.com/mini-monkey/p/11066346.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。