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 配置项,您可以灵活地定义应用程序的网络安全策略,以确保应用程序与网络资源之间的安全通信。

相关推荐
三年呀14 分钟前
标题:移动端安全加固:发散创新,筑牢安全防线引言:随着移动互联网
网络·python·安全
auxor18 分钟前
Android 开机动画音频播放优化方案
android
whysqwhw37 分钟前
安卓实现屏幕共享
android
x.Jessica1 小时前
网络的构成元素
网络·学习·计算机网络
深盾科技1 小时前
Kotlin Data Classes 快速上手
android·开发语言·kotlin
一条上岸小咸鱼1 小时前
Kotlin 基本数据类型(五):Array
android·前端·kotlin
tan77º1 小时前
【项目】分布式Json-RPC框架 - 项目介绍与前置知识准备
linux·网络·分布式·网络协议·tcp/ip·rpc·json
whysqwhw1 小时前
Room&Paging
android
whysqwhw2 小时前
RecyclerView超长列表优化
android
Tiger_Hu2 小时前
Android系统日历探索
android