如何在Android(9)Pie中允许所有网络连接类型的HTTP和HTTPS?

218
自 Android 9 Pie 版本起,未加密的请求将无法正常工作。系统默认期望您使用 TLS 进行请求。您可以在此处了解此功能。因此,如果您仅通过 HTTPS 进行请求,则是安全的。但对于那些通过不同网站进行请求(例如类似浏览器的应用程序),该怎么办呢?
如何在 Android 9 Pie 中启用对所有类型连接(HTTP 和 HTTPS)的请求?
10个回答

417

实现这个的简单方法是将此属性添加到您的AndroidManifest.xml文件中,允许所有请求使用http

使用此属性是实现这一目的的简单方法,只需在您的AndroidManifest.xml文件中添加它,以允许所有请求使用http

(Two versions provided for reference, please choose the one that suits your needs.)
<application android:usesCleartextTraffic="true">
</application>

但是,如果你想要为不同的链接提供一些更多的配置(例如允许某些域使用http,而其他域则不行),你必须提供res/xml/networkSecurityConfig.xml文件。

要在Android 9 Pie中实现这一点,您需要在Manifest的application标记中设置一个networkSecurityConfig,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
    <application android:networkSecurityConfig="@xml/network_security_config">




    </application>
</manifest>

然后在你的xml文件夹中,你现在需要创建一个名为network_security_config的文件,就像你在清单中命名它的方式一样,然后你的文件内容应该如下,以启用所有未加密请求:

<?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>

从那里开始,您就可以放心使用了。现在您的应用程序将请求各种类型的连接。有关此主题的其他信息,请在此处阅读


3
在我做了这些更改(使用React Native应用程序)之后,它不再构建。出现“清单合并失败”和“Attribute application@networkSecurityConfig value=(@xml/react_native_config) from AndroidManifest.xml:7:7-67 is also present at AndroidManifest.xml:7:7-67 value=(@xml/network_security_config)”的错误信息。有什么想法吗? - Wyatt
1
@DanteCervantes请查看我上面的回答。 - HarshitMadhav
13
在React Native项目中,我该在哪里找到XML文件夹? - CraZyDroiD
2
浪费了很多时间才弄清楚这实际上是HTTP问题。通常它会在HTTP响应中显示错误。 - Mihir Bhatt
1
AndroidManifest和network_security_config文件位于React Native中的以下位置: android/app/src/main/AndroidManifest.xml android/app/src/main/res/xml/network_security_config.xml - Ahmed Lotfy
显示剩余5条评论

55

对于遇到此问题的AndroidReact-native用户,完全工作的解决方案是在AndroidManifest.xml文件中添加以下内容:android:usesCleartextTraffic="true"

android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<uses-library
    android:name="org.apache.http.legacy"
    android:required="false" />

像这样在<application></application>标签之间添加内容:

<application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="false"
      android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true"
        tools:ignore="GoogleAppIndexingWarning">
        <uses-library
            android:name="org.apache.http.legacy"
            android:required="false" />
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"/>
 </application>

2
哇,谢谢,它在我的应用程序中运行得很好,之前它显示 I/O 失败问题,现在已经解决了。 - Venkatesh
11
如果你遇到了 tools:ignore 错误,请确保在你的 application 中添加 xmlns:tools="http://schemas.android.com/tools"。像这样:<application xmlns:tools="http://schemas.android.com/tools" ... - Ziyo
2
我知道这是一个Android的问题,但可能对React Native开发人员有所帮助。iOS的解决方案是将NSAppTransportSecurity添加到info.plist中。https://dev59.com/oloT5IYBdhLWcg3wlgPn - Abdul Sadik Yalcin
@NarendraSingh 尝试检查pdf的url是否有效,然后使用react-native-fs访问应用程序的文件系统来下载pdf。 - HarshitMadhav
@NarendraSingh 请检查一下这是否与React Native中的Flipper问题有关。 - HarshitMadhav
显示剩余3条评论

27
一个简单的方法是在你的 AndroidManifest.xml 中设置 android:usesCleartextTraffic="true"
android:usesCleartextTraffic="true"

你的 AndroidManifest.xml 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.dww.drmanar">
   <application
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:usesCleartextTraffic="true"
       android:theme="@style/AppTheme"
       tools:targetApi="m">
       <activity
            android:name=".activity.SplashActivity"
            android:theme="@style/FullscreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
       </activity>
    </application>
</manifest>

我希望这能对你有所帮助。


1
我在2021年5月14日遇到了同样的问题,得到了@Mehul Solanki的帮助。问题已经解决。 - Raj Kanchan

19

简单方法

usesCleartextTraffic添加到AndroidManifest.xml

<application
...
android:usesCleartextTraffic="true"
...>

指示应用程序是否打算使用明文网络流量,例如明文HTTP。针对API级别27或更低版本的应用程序的默认值为"true"。针对API级别28或更高版本的应用程序的默认值为"false"。


1
是的,这很容易,但这是最不安全的解决方案。 - diAz

14

在调试运行 React Native 应用程序时,将 @Xenolion 提到的 xml 块 添加到 <project>/android/app/src/debug/res/xml 中的 react_native_config.xml 中。

类似以下片段:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="false">localhost</domain>
        <domain includeSubdomains="false">10.0.2.2</domain>
        <domain includeSubdomains="false">10.0.3.2</domain>
    </domain-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

在flutter中,它会抛出异常E/flutter(19798): [ERROR:flutter/shell/common/shell.cc(209)] Dart Unhandled Exception: Invalid argument(domain): Invalid domain name: "localhost", stack trace: #0 new _DomainNetworkPolicy(dart:io/network_policy.dart:85:7) - Amir Heshmati
1
现在它对我有效了,谢谢...顺便说一句,我正在使用React Native。 - Meiki Neumann
这对我来说在React Native 60.6、Android 10、Gradle 3.4.3上运行正常...如果在模拟器中运行仍然无法连接到打包服务器,请尝试在终端中输入adb reverse tcp:8081 tcp:8081(假设8081是Metro Bundler运行的端口)。 - Scott Plunkett

11

只需在AndroidManifest.xml文件的应用程序标签中设置usesCleartextTraffic标志即可,无需为Android创建配置文件。

 <application
   android:usesCleartextTraffic="true"
   .
   .
   .>

4

这对我有效,

将此xml文件添加到: android/app/src/main/res/xml/network_security_config.xml

network_security_config.xml

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>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">your_domain1</domain>
    </domain-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">your_domain2</domain>
    </domain-config>
</network-security-config>

然后将这段代码添加到AndroidMenifest.xml中。

<application
    ...
      android:usesCleartextTraffic="true"
      android:networkSecurityConfig="@xml/network_security_config"
    ...
      >
      <!-- for http support-->
      <uses-library android:name="org.apache.http.legacy" android:required="false"/>
      ...
</application>
        

4

我遇到了同样的问题,我注意到我的安全配置中有不同的标记,就像@Xenolion的回答所说的那样。

<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
    </domain-config>
</network-security-config>

我将标签“domain-config”改为“base-config”,然后就可以正常工作了,如下所示:

<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
    </base-config>
</network-security-config>

请问您能回答这个问题吗?https://stackoverflow.com/questions/59116787/failed-to-load-resource-neterr-cleartext-not-permitted - pratik jaiswal

1

在AndroidManifest.xml中添加usesCleartextTraffic

例如:

<application
...
android:usesCleartextTraffic="true"
...>

<application
    android: allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="AndroidLearning"
    android: supportsRtl="true"
    android:UsesCleartextTraffic="true" <-----
    android: theme="@style/Theme.AndroidLearning"
    tools:targetApi="31">
    <activity
    android:name=" MainActivity"
    android:exported="true">
    <intent-filter>
    <action android:name="android. intent.action.MAIN" />
    ‹catedorv android:name="android.intent.catedorv.LAUNCHFR" />

0

您可以检查是否通过HTTP发送了clearText 修复:https://medium.com/@son.rommer/fix-cleartext-traffic-error-in-android-9-pie-2f4e9e2235e6
或者
在 Apache HTTP客户端弃用的情况下(来自Google): 从Android 6.0开始,我们删除了对Apache HTTP客户端的支持。从Android 9开始,该库已从bootclasspath中删除,并且默认情况下不可用于应用程序。 要继续使用Apache HTTP客户端,针对Android 9及以上版本的应用程序可以将以下内容添加到其AndroidManifest.xml:

源代码 https://developer.android.com/about/versions/pie/android-9.0-changes-28


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接