webview升级为系统app

使用webview的app想升级为系统级app,需要绕过一个认证。

public static void hookWebView() {
        int sdkInt = Build.VERSION.SDK_INT;
        try {
            Class factoryClass = Class.forName("android.webkit.WebViewFactory");
            Field field = factoryClass.getDeclaredField("sProviderInstance");
            field.setAccessible(true);
            Object sProviderInstance = field.get(null);
            if (sProviderInstance != null) {

                return;
            }
            Method getProviderClassMethod;
            if (sdkInt > ManualUtil.BUILD_VERSION_SDK_INT_22) {
                getProviderClassMethod = factoryClass.getDeclaredMethod("getProviderClass");
            } else if (sdkInt == ManualUtil.BUILD_VERSION_SDK_INT_22) {
                getProviderClassMethod = factoryClass.getDeclaredMethod("getFactoryClass");
            } else {

                return;
            }
            getProviderClassMethod.setAccessible(true);
            Class providerClass = (Class) getProviderClassMethod.invoke(factoryClass);
            Class delegateClass = Class.forName("android.webkit.WebViewDelegate");
            Method createMethod =providerClass.getDeclaredMethod("create", delegateClass);

            if (createMethod != null) {
                Constructor declaredConstructor = delegateClass.getDeclaredConstructor();
                declaredConstructor.setAccessible(true);
                sProviderInstance =  createMethod.invoke(null, declaredConstructor.newInstance());
                field.set("sProviderInstance", sProviderInstance);
            }

        } catch (Throwable e) {
            Log.e("HOOK_WEB",e.toString());
        }
    }

在加载layout前使用

super.onCreate(savedInstanceState);
//        hookWebView();
setContentView(R.layout.activity_manu);

如果出现白屏问题,可能是证书没有通过,需要在onReceivedSslError函数中接受所有证书验证。

webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                Log.i(TAG, "onReceivedSslError: ");
                handler.proceed(); //接受所有证书验证
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }
        });

注意:sslError中的自动生成的父类要删除。

你可能感兴趣的:(webview升级为系统app)