How to solve Android P DownloadManager stopping with "Cleartext HTTP traffic to 127.0.0.1 not permitted"?

12,921

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"
Share:
12,921

Related videos on Youtube

spartygw
Author by

spartygw

Updated on September 15, 2022

Comments

  • spartygw
    spartygw almost 2 years

    I have already defined a custom network security config and included it in my manifest as recommended here

    res/xml/network_security_config.xml:

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

    and this is in my Android.manifest:

        <application android:icon="@drawable/icon" 
                 android:allowBackup="false"
                 android:usesCleartextTraffic="true"
                 android:networkSecurityConfig="@xml/network_security_config"
                 android:label="@string/app_name"
                 android:theme="@style/AppTheme"
                 android:persistent="true" >
    

    Even with these changes when attempting to communicate via HTTP to 127.0.0.1 I see this in logcat:

    08-09 10:50:34.395 30791  3607 D NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
    08-09 10:50:34.397 30791  3607 D NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
    08-09 10:50:34.401 30791  3607 W DownloadManager: [647] Stop requested with status HTTP_DATA_ERROR: Cleartext HTTP traffic to 127.0.0.1 not permitted
    08-09 10:50:34.402 30791  3607 D DownloadManager: [647] Finished with status WAITING_TO_RETRY
    

    EDIT: Update (21 Aug 2018) after "fixing" this issue it seems that a couple of hours after the app is installed the DownloadManager mysteriously stops accepting cleartext HTTP.

    I can't explain what's happening. If I reboot the device things work great for a period of time then DownloadManager refuses cleartext again. I'm writing this off as an Android P bug that I hope gets resolved.

    EDIT 2: Update (17 Jan 2019) my pixel2 is now running android 9 with a patch from 5 Jan 2019 and I no longer see the issue (so far). I'm guessing this got resolved in some patch since August.

    • ares777
      ares777 almost 6 years
      add also <base-config cleartextTrafficPermitted="true"/> in <network-security-config> ... close it with </base-config>
    • spartygw
      spartygw almost 6 years
      @user3344236 replacing domain-config with base-config solved it! Submit your response as an answer and I'll mark it accepted.
    • hokielife
      hokielife almost 6 years
      Spartygw, in response to "EDIT: Update (21 Aug 2018)" I see the same thing as you. It works, then stops working after a few hours. To temporarily resolve the issue I can force quit the Download Manager app, restart my app and try the download again and it will work. I agree with you. It does seem like a bug in Android P's code in Download Manager.
    • LeonLu
      LeonLu almost 6 years
      @spartygw I've filed an issue to Google regarding your Update (21 Aug 2018). issuetracker.google.com/issues/114143692
  • John Smith
    John Smith almost 6 years
    Are you sure that's right? shouldn't it either be a closing tag or />?
  • ares777
    ares777 almost 6 years
    As you can already know the structure is XML and closing tag is standardized as </
  • John Smith
    John Smith almost 6 years
    Thanks for the lesson on closing tags! Now remove the trailing / from < base-config cleartextTrafficPermitted="true" / > ;)
  • ares777
    ares777 almost 6 years
    No need because is not a node John Smith :)) the usual html closing is used also. Please read again the link.
  • Loganathan
    Loganathan over 5 years
    After spending so many ways the clear text communication problem was resolved by using above code. Thank you very much.@user3344236
  • Jalpesh
    Jalpesh over 5 years
    SOmehow it does not work for me, Any idea If there is something I need to add?
  • isabsent
    isabsent over 5 years
    @Jalpesh For me too.
  • Jaydeep Kataria
    Jaydeep Kataria almost 5 years
    Thanks a lot @user3344236
  • Danish Ansari
    Danish Ansari over 4 years
    If we set cleartextTrafficPermitted to true for base-config, aren't we defeating the purpose of network security policy? I mean every HTTP will be accessed with this configuration.
  • Danish Ansari
    Danish Ansari over 4 years
    How can we allow only our website for downloading files?
  • ares777
    ares777 over 4 years
    Not related to topic, but you can make rules with SSL pinning and allowed domains.
  • Sfili_81
    Sfili_81 about 4 years
    Please explain why thi replacement can answer to the question