美文网首页
系统应用webview报错

系统应用webview报错

作者: Atlans | 来源:发表于2018-04-04 10:57 被阅读0次

    1.Hook思想

    应用在Application中初始化hookWebView

    /**

    * Hook思想,解决root和system应用第一次无法使用webview

    * 报错:java.lang.UnsupportedOperationException: For security reasons, WebView is not allowed in privileged processes

    */

    public 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) {

    LogUtils.d("sProviderInstance isn't null");

    return;

            }

    Method getProviderClassMethod;

            if (sdkInt >22) {

    getProviderClassMethod = factoryClass.getDeclaredMethod("getProviderClass");

            }else if (sdkInt ==22) {

    getProviderClassMethod = factoryClass.getDeclaredMethod("getFactoryClass");

            }else {

    LogUtils.d("Don't need to Hook WebView");

    return;

            }

    getProviderClassMethod.setAccessible(true);

            Class providerClass = (Class) getProviderClassMethod.invoke(factoryClass);

            Class delegateClass = Class.forName("android.webkit.WebViewDelegate");

            Constructor providerConstructor = providerClass.getConstructor(delegateClass);

            if (providerConstructor !=null) {

    providerConstructor.setAccessible(true);

                Constructor declaredConstructor = delegateClass.getDeclaredConstructor();

                declaredConstructor.setAccessible(true);

                sProviderInstance = providerConstructor.newInstance(declaredConstructor.newInstance());

                LogUtils.d("sProviderInstance:{}", sProviderInstance);

                field.set("sProviderInstance", sProviderInstance);

            }

    LogUtils.d("Hook done!");

        }catch (Throwable e) {

    }

    }

    2.修改framework

    framewor/base/core/java/android/webkit/WebViewFactory.java

    @@ -148,10 +148,10 @@ public final class WebViewFactory {

                if (sProviderInstance != null) return sProviderInstance;

                final int uid = android.os.Process.myUid();

    -            if (uid == android.os.Process.ROOT_UID || uid == android.os.Process.SYSTEM_UID) {

    +            /*if (uid == android.os.Process.ROOT_UID || uid == android.os.Process.SYSTEM_UID) {

                    throw new UnsupportedOperationException(

                            "For security reasons, WebView is not allowed in privileged processes");

    -            }

    +            }*/

                Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getProvider()");

    这种方式会导致系统不安全,不推荐;最好根据自己应用的包名加以判断

    相关文章

      网友评论

          本文标题:系统应用webview报错

          本文链接:https://www.haomeiwen.com/subject/qenavxtx.html