Anshul Tyagi
Anshul Tyagi

Reputation: 2196

Android Webview page could not be loaded with HTTP hyperlink because ERR_CLEARTEXT_NOT_PERMITTED

I'm working on an android native app where I'm loading a HTML string in WebView from strings.xml which has some hyperlink texts all. One hyperlink out of all of them is having http url and which is not getting opened within webView and throwing error as ERR_CLEARTEXT_NOT_PERMITTED. I'm already using usesCleartextTraffic settings in my app but still unable to use that.

AndroidManifest.xml

 <application
    android:name=".com.app"
    android:allowBackup="false"
    android:icon="@mipmap/ic_hp_launcher"
    android:networkSecurityConfig="@xml/network_security_config"
    android:roundIcon="@mipmap/ic_hp_launcher_round"
    android:supportsRtl="false"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">

network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<debug-overrides>
    <trust-anchors>
        <!-- Trust user added CAs while debuggable only -->
        <certificates src="user" />
        <certificates src="system" />
    </trust-anchors>
</debug-overrides>

<base-config cleartextTrafficPermitted="true">
    <trust-anchors>
        <certificates src="system" />
    </trust-anchors>
</base-config>

<domain-config cleartextTrafficPermitted="true">
    <!-- Make sure your URL Server here -->
    <domain includeSubdomains="true">https://xxxxxx.base.url</domain>
    <domain includeSubdomains="true">http://.xxxx.org</domain>
    <trust-anchors>
        <certificates src="user"/>
        <certificates src="system"/>
    </trust-anchors>
</domain-config>

UIClass.kt

//Enabling JS for allowing all hyperlinks
    binding.webView.settings.javaScriptEnabled = true
    binding.webView.settings.javaScriptCanOpenWindowsAutomatically = true

build.gradle

minSdkVersion 28
targetSdkVersion 30

Please let me know if I'm doing any mistake. Thanks in advance.

Upvotes: 1

Views: 3533

Answers (1)

Anshul Tyagi
Anshul Tyagi

Reputation: 2196

I resolved this problem by two options.

Option 1:

I removed one line from the AndroidManifest.xml file

android:networkSecurityConfig="@xml/network_security_config" 

Option 2:

Added http legacy library in the build.gradle file under defaultConfig column as

useLibrary 'org.apache.http.legacy'

Upvotes: 1

Related Questions