LoginSignup
11
14

More than 3 years have passed since last update.

Android Emulatorでnet::ERR_CLEARTEXT_NOT_PERMITTEDが出た時の対処法

Last updated at Posted at 2020-02-16

WebViewでlocalhostを参照する実装をAndroid Emulatorで見る際、net::ERR_CLEARTEXT_NOT_PERMITTEDというエラーが表示された。
スクリーンショット 2020-02-16 18.47.53_.png

これはAndroid 9(API レベル 28)からTLS(Transport Layer Security)がデフォルトとなり、HTTPプロトコル通信許が可されなくなったことが原因。
Google Developers Japan: Android P で TLS のデフォルト化によるユーザー保護

対処法はいくつかあるが、今回はサンプルアプリということもあり、マニフェストファイルにHTTP通信を許可するようusesCleartextTrafficの設定を追加することで回避した。

AndroidManifest.xml
<application
    ...
    android:usesCleartextTraffic="true"
    ...
>
</application>

厳密に対応する場合は、特定ドメインのみを許可する設定などが推奨されている。

AndroidManifest.xml
    <?xml version="1.0" encoding="utf-8"?>
    <manifest ... >
        <application android:networkSecurityConfig="@xml/network_security_config"
                        ... >
            ...
        </application>
    </manifest>
res/xml/network_security_config.xml
    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <domain-config>
            <domain includeSubdomains="true">secure.example.com</domain>
            <domain includeSubdomains="true">cdn.example.com</domain>
            <trust-anchors>
                <certificates src="@raw/trusted_roots"/>
            </trust-anchors>
        </domain-config>
    </network-security-config>

ネットワーク セキュリティ構成  |  Android デベロッパー  |  Android Developers

11
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
14