简体   繁体   中英

Cleartext HTTP traffic to myserver.com not permitted on Android N preview

Yesterday I got a new upgrade for the Android N preview. Ever since I upgraded, I cannot start my app anymore.

java.io.IOException: Cleartext HTTP traffic to myserver.com not permitted

I have tried to set the usesCleartextTraffic to true in the manifest or to add a network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">myserver.com</domain>
    </domain-config>
</network-security-config>

Neither did work. Any ideas about what is going on there?

When I try to define networkSecurityConfig in the manifest, I get a compile error

Error:(35) No resource identifier found for attribute 'networkSecurityConfig' in package 'android'

Not really sure why. The file is there and everything looks good.

Found this suggestion in the Android issue tracker from Google. They suggest to move the network_security_config definition to the meta-data . I still get the same exception though.

android:usesCleartextTraffic="true"

将此行放在清单文件的应用程序标记中

There is a known issue in Android N Developer Preview 4 where, if an app modifies its ApplicationInfo.flags , it triggers the blocking of cleartext traffic from the app, even if the app didn't request cleartext traffic to be blocked. The fix is in the next Developer Preview. Thus, this has nothing to do with your Network Security Config. In fact, it looks like you don't even need to declare a custom Network Security Config.

If you can't wait until the next Android N Developer Preview, check your app for places where it modifies its own ApplicationInfo.flags . Typically this takes the form of getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE or getApplicationInfo().flags = ApplicationInfo.FLAG_DEBUGGABLE . The fix for these usages is (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) .

Alternatively, as a workaround, invoke NetworkSecurityPolicy.isCleartextTrafficPermitted() as early in your app's lifecycle as possible. This workaround should work if invoked before the code which tampers with ApplicationInfo.flags .

Try just one line in your Application Tag

android:usesCleartextTraffic="true"

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
       ...
       android:usesCleartextTraffic="true"
       ...>
    </application>
</manifest>

在此处输入图片说明

res/xml/network_security_config.xml
 <?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config cleartextTrafficPermitted="true"> <trust-anchors> <certificates src="system" /> </trust-anchors> </base-config> </network-security-config>
AndroidManifest.xml
 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" tools:ignore="GoogleAppIndexingWarning" android:networkSecurityConfig="@xml/network_security_config"> <activity android:name=".activity.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
       ...
       android:usesCleartextTraffic="true"
       ...>
    </application>
</manifest>

This one worked perfectly for me :)

我在这里提出的最佳解决方案是以下代码应该在清单中

android:usesCleartextTraffic="true"

From API level 28 onwards the usesCleartextTraffic is disabled by default. So, If you want to enable it, then set the usesCleartextTraffic as true in the androidManifest file like this,

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
       ...
       android:usesCleartextTraffic="true"
       ...>
    </application>
</manifest>

Create a XML res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

Reference this file in your tag Application, inside AndroidManifest.xml. Like:

android:networkSecurityConfig="@xml/network_security_config"

I add this " android:usesCleartextTraffic="true" "

but my app crashed ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM