Android的Flutter没有设置android:usesCleartextTraffic="true"?

1 人关注

我遇到了一些问题,从安卓模拟器获得与FireBase互动的请求。我最后设置了

android:usesCleartextTraffic="true"

在清单文件中。

快速搜索告诉我,这允许不安全的连接。

Android 6.0 introduced the `useCleartextTraffic` attribute under the application element in the android manifest. The default value in Android P is “false”. Setting this to true indicates that the app intends to use clear network traffic

为了在不设置android:usesCleartextTraffic="true"的情况下部署我的应用程序,我必须做什么?

3 个评论
如果你需要使用HTTP(而不是HTTPS)连接,那么你将需要设置android:usesClearTextTraffic="true"
因此,你应该检查你的代码,看看每个HTTP请求的协议。如果你发现有一个使用 "http://"而不是 "https://",那么你应该在可行的情况下把它改为 "s"。
如果你发现任何请求在你改变后无法调用,如果你有能力联系服务器端为你改变,就去做吧。否则你就需要做最后一步,将usesCleartextTraffic设置为true,并允许该特定HTTP请求的连接被嗅探到。(例如,Android Studio或Google Play商店的网络监控工具,PC上的Wireshark)
android
flutter
firebase
Michael
Michael
发布于 2022-11-16
1 个回答
Alexander N.
Alexander N.
发布于 2022-11-16
已采纳
0 人赞同

Option 1

res/xml/ 文件夹中创建一个 network_config.xml 文件,其中包含以下内容。

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

我相信10.0.0.2是模拟器在安卓设备上使用的ip地址。如果我错了,你可以将ip地址更新为任何可能是正确的地址。

然后在AndroidManifest.xml文件的应用标签中添加android:networkSecurityConfig="@xml/network_config"。输出结果将看起来像这样。

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

这应该允许来自模拟器的任何流量以明文形式出现,但阻止其他端点通过http/cleartext访问。

Option 2

假设你可以用gradle配置一个设置,为调试和发布的构建发布不同的清单。我刚刚查看了flutter的android文件夹,你可以做的是进入${PROJECT_FOLDER}/android/app/src/debug/AndroidManifest.xml并添加以下内容。

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

你在debug文件夹中的输出AndroidManifest.xml(应该只适用于debug构建,可能看起来类似于这样。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.package">
    <!-- The INTERNET permission is required for development. Specifically,
         the Flutter tool needs it to communicate with the running application
         to allow setting breakpoints, to provide hot reload, etc.
    <!-- ADD FROM HERE-->
    <application
        android:usesCleartextTraffic="true">
    </application>
    <!-- TO HERE-->