Android Fix CLEARTEXT communication to {IP} not permitted by network security policy

Android 6 พ.ค. 2020

java.net.UnknownServiceException: CLEARTEXT communication to 192.168.1.44 not permitted by network security policy


Add src\main\res\xml\network_security_config.xml with the following content:

<?xml version="1.0" encoding="utf-8"?><network-security-config>    <domain-config cleartextTrafficPermitted="true">        <domain includeSubdomains="true">your domain or ip address</domain>    </domain-config></network-security-config>

Then register policy file in AndroidManifest.xml by setting the android:networkSecurityConfig attribute.

<application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:networkSecurityConfig="@xml/network_security_config"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>

แท็ก

Onyx

Just a middle-aged programmer, Can do many things but not the most.