我在从Android模拟器获得与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"
的情况下部署应用程序,我必须做什么?
发布于 2022-11-16 03:57:43
备选案文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/明文访问其他端点。
选项2
您可以假设使用gradle配置一个安装程序,以发布用于调试和发布版本的不同清单。我刚刚查看了android文件夹中的颤振信息,您可以做的是转到${PROJECT_FOLDER}/android/app/src/debug/AndroidManifest.xml并添加以下内容:
<application
android:usesCleartextTraffic="true">
</application>
调试文件夹中的输出AndroidManifest.xml
(只应应用于调试生成)可能类似于以下内容:
<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-->
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
备注
我自己还没有测试选项2,但根据我的理解,这应该是可行的。这个中篇可能会有所帮助。
https://stackoverflow.com/questions/74453294
复制相似问题