Android API 30及更高版本网络权限设置

目录

  • 一、网络权限设置
  • 二、配置步骤
    • [1、在 AndroidManifest.xml 文件中添加网络权限声明](#1、在 AndroidManifest.xml 文件中添加网络权限声明)
    • [2、在 AndroidManifest.xml 文件中的 application 节点下配置网络安全策略](#2、在 AndroidManifest.xml 文件中的 application 节点下配置网络安全策略)

一、网络权限设置

在 Android API 30 及更高版本中,Google 引入了更严格的网络安全策略,其中包括对应用程序的网络访问权限进行了更多的限制。如果您的应用程序需要进行网络访问:

  • 需要确保在清单文件(AndroidManifest.xml)中正确声明网络权限;

  • 并且在运行时请求网络权限。

二、配置步骤

1、在 AndroidManifest.xml 文件中添加网络权限声明

AndroidManifest.xml 文件中添加网络权限声明:

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.xxx" >
    
	<uses-permission android:name="android.permission.INTERNET" />

	...
</manifest>

2、在 AndroidManifest.xml 文件中的 application 节点下配置网络安全策略

AndroidManifest.xml 文件中的 application 节点下配置网络安全策略:

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.xxx" >
    
	<application 
		android:usesCleartextTraffic="true"
    	...>
    	...
	</application>

	...
</manifest>

usesCleartextTraffic 是清单文件(AndroidManifest.xml)中的配置项,用于指定应用程序是否允许明文流量(非加密流量)的网络请求,明文流量是指未经加密的网络流量,可能存在安全风险。

  • true:应用程序可以发送明文流量的网络请求;

  • false :应用程序将被限制只能发送加密流量的请求(即 HTTPS 请求)。

注意事项:

  • 开启明文流量可能会带来安全风险,因为未经加密的网络通信可能会被中间人攻击截取和窃听。
  • Google 在 Android 9(API 30)及之后的版本中默认禁止应用程序发送明文流量的网络请求,因此如果您的应用程序需要与不支持 HTTPS 的服务器通信,您可能需要显式地配置 android:usesCleartextTraffic="true"。

除了 usesCleartextTraffic 配置项外。还可以在网络安全配置文件(res/xml/network_security_config.xml)中配置网络安全策略,然后在清单文件中引用该配置文件:

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.xxx" >
    
	<application 
		android:networkSecurityConfig="@xml/network_security_config"
    	...>
    	...
	</application>

	...
</manifest>

network_security_config.xml

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">com.xxx</domain>
    </domain-config>
</network-security-config>

配置文件允许 com.xxx 及其 所有子域名 的明文流量(非加密流量),通过使用 android:networkSecurityConfig 配置项,您可以灵活地定义应用程序的网络安全策略,以确保应用程序与网络资源之间的安全通信。

相关推荐
用户693717500138416 分钟前
Android 手机终于能当电脑用了
android·前端
用户5172231574801 小时前
android资源类型与布局资源详细介绍
android
志栋智能1 小时前
告别高昂投入:超自动化IT运维的轻量化实践
大数据·运维·网络·人工智能·自动化
优选资源分享1 小时前
GKD v1.11.6 | 安卓开屏广告跳过工具 可用版
android
robotx2 小时前
安卓zygote启动相关
android
黄昏晓x2 小时前
Linux----网络
linux·网络·arm开发
sensen_kiss2 小时前
CAN302 电子商务技术 Pt.1 Web技术导论
前端·网络·学习
攻城狮在此3 小时前
华为LACP链路聚合配置(三层聚合)
网络·华为
Mac的实验室3 小时前
2026年最新真实社交怎么注册?手把手教你如何成功注册Truth Social账号
android
AttaGain3 小时前
【Ubuntu配置VLAN网络】
linux·网络·ubuntu