Tag Archives: Error inflating class android.webkit.WebView / For security reasons

[Solved] Android O Error: Error inflating class android.webkit.WebView/For security reasons, WebView is not allowed in privileged processes

Device environment:Android O ,   android:sharedUserId=”android.uid.system”  进程
Error: android.view.InflateException: Binary XML file line #24: Error inflating class android.webkit.WebView…
…For security reasons, WebView is not allowed in privileged processes
Solution(Found Online):
1. Add the below codes at the end of Application In manifest:

<meta-data android:name="android.webkit.WebView.EnableSafeBrowsing"
                   android:value="true"/>
NOT WORK

two   If the appcompat version in build.gradle under the app file is 1.1.0, WebView will make an error and change it to 1.0.2

NOT WORK

three   (the effect is the same as point 4) the application attribute is added:   android:usesCleartextTraffic=”true”

NOT WORK  (NOT android:sharedUserId="android.uid.system" process)

4. (The effect is the same as point 3.) The application attribute increases.android:networkSecurityConfig=”@xml/network_security_config”
The contents of network_security_config.xml placed under res/xml/.
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config cleartextTrafficPermitted="true" /> </network-security-config>

NOT WORK  (NOT android:sharedUserId="android.uid.system"  process)

5. Working Method: Call this method first when the Application starts (also add point 3 or 4)

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) {
        Log.i(TAG,"sProviderInstance isn't null");
        return;
      }
 
      Method getProviderClassMethod;
      if (sdkInt > 22) {
        getProviderClassMethod = factoryClass.getDeclaredMethod("getProviderClass");
      } else if (sdkInt == 22) {
        getProviderClassMethod = factoryClass.getDeclaredMethod("getFactoryClass");
      } else {
        Log.i(TAG,"Don't need to Hook WebView");
        return;
      }
      getProviderClassMethod.setAccessible(true);
      Class<?> factoryProviderClass = (Class<?>) getProviderClassMethod.invoke(factoryClass);
      Class<?> delegateClass = Class.forName("android.webkit.WebViewDelegate");
      Constructor<?> delegateConstructor = delegateClass.getDeclaredConstructor();
      delegateConstructor.setAccessible(true);
      if(sdkInt < 26){//低于Android O版本
        Constructor<?> providerConstructor = factoryProviderClass.getConstructor(delegateClass);
        if (providerConstructor != null) {
          providerConstructor.setAccessible(true);
          sProviderInstance = providerConstructor.newInstance(delegateConstructor.newInstance());
        }
      } else {
        Field chromiumMethodName = factoryClass.getDeclaredField("CHROMIUM_WEBVIEW_FACTORY_METHOD");
        chromiumMethodName.setAccessible(true);
        String chromiumMethodNameStr = (String)chromiumMethodName.get(null);
        if (chromiumMethodNameStr == null) {
          chromiumMethodNameStr = "create";
        }
        Method staticFactory = factoryProviderClass.getMethod(chromiumMethodNameStr, delegateClass);
        if (staticFactory!=null){
          sProviderInstance = staticFactory.invoke(null, delegateConstructor.newInstance());
        }
      }
 
      if (sProviderInstance != null){
        field.set("sProviderInstance", sProviderInstance);
        Log.i(TAG,"Hook success!");
      } else {
        Log.i(TAG,"Hook failed!");
      }
    } catch (Throwable e) {
      Log.w(TAG,e);
    }
  }