targetSdkVersion升到31后,编译提示需要android:exported

最近要上架google,要求编译版本最低是30。我想着那就一步到位弄到31吧。

结果报错

/Users/erone/项目/xxx For Google/app/src/main/AndroidManifest.xml Error:
	android:exported needs to be explicitly specified for <activity>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
/Users/erone/项目/xxx For Google/app/src/main/AndroidManifest.xml Error:
	android:exported needs to be explicitly specified for <activity>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
/Users/erone/项目/xxx For Google/app/src/main/AndroidManifest.xml Error:
	android:exported needs to be explicitly specified for <activity>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

先搜了一下,说是android12以上manifest里所有activity都要加上android:exported描述。问题是我已经加上了,manifest里的四大组件都有了这个描述

再搜了搜发现是依赖库的问题https://stackoverflow.com/questions/67654506/manifest-merger-failed-targeting-android-12/67668116#67668116

大概意思,检查一下以androidx.test为前缀的依赖是不是最新版本,如果不是升级一下应该就没问题了。我这里包括以下三个

androidx.test:runner
androidx.test.espresso:espresso-core
androidx.test.ext:junit

版本太低,升级下版本就可以了。

要升一起升,要死一起死,难受啊~~

Androidstudio查看这段在AndroidManifest.xml中的代码是否需要修改或者优化<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@drawable/tubiao" android:label="@string/app_name" android:roundIcon="@drawable/tubiao" android:supportsRtl="true" android:networkSecurityConfig="@xml/network_security_config" android:theme="@style/Theme.MyExamProject"> <activity android:name=".AddActivityActivity" android:exported="false" /> <activity android:name=".AddFurnitureActivity" android:exported="false" /> <activity android:name=".AddCaregiverActivity" android:exported="false" /> <activity android:name=".AddOldActivity" android:exported="false" /> <activity android:name=".FacilityActivity" android:exported="false" /> <activity android:name=".EventsActivity" android:exported="false" /> <activity android:name=".AddActivity2" android:exported="false" /> <activity android:name=".UpdateIFActivity" android:exported="false" /> <activity android:name=".MyIFActivity" android:exported="false" /> <activity android:name=".MainActivity" android:exported="false" /> <activity android:name=".UpdateGamesActivity" android:exported="false" /> <activity android:name=".RegisterActivity" android:exported="false" /> <activity android:name=".PersonalCenterActivity" android:exported="false" /> <activity android:name=".LoginActivity" android:exported="false" /> <activity android:name=".DeleteGamesActivity" android:exported="false" /> <activity android:name=".BackActivity" android:exported="false" /> <activity android:name=".AddGamesActivity" android:exported="false" /> <activity android:name=".HomeActivity" android:exported="true" /> <activity android:name=".WelcomeActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
最新发布
03-11
<think>嗯,用户让我看一下他们的AndroidManifest.xml文件,看看是否需要修改或优化。首先,我得仔细分析这个文件的结构和各个部分的内容。让我先把整个代码过一遍,看看有没有明显的错误或者可以改进的地方。 首先,用户声明了INTERNET权限,这个没问题,因为如果应用需要网络访问的话,这个权限是必须的。不过,我需要确认他们是否真的需要这个权限,如果应用不需要联网,那就可以移除,但看起来像是有网络相关的配置,比如networkSecurityConfig,可能用到了HTTPS之类的,所以应该没问题。 接下来看看application标签里的属性。allowBackup设置为true,这默认是允许的,但有些情况下可能需要关闭,比如处理敏感数据时。不过用户这里设为true,如果没有特殊需求,保持默认也行。图标和主题的设置看起来没问题,引用了资源文件,没问题。 然后注意到networkSecurityConfig属性指向了@xml/network_security_config,这说明用户可能配置了网络安全策略,比如允许明文通信或者自定义CA证书。这在Android 9及以上是必须的,如果应用需要访问非HTTPS的链接,必须配置这个。不过需要确认network_security_config.xml文件是否存在且配置正确,但这里只能看到manifest,所以可能需要用户自己检查。 接下来是各个activity的声明。大部分activity的exported属性都设为false,但HomeActivity和WelcomeActivity的exported是true。对于exported属性,Android要求如果activity有intent-filter,exported默认是true,否则默认是false。所以WelcomeActivity有LAUNCHER的intent-filter,所以必须exported为true,这是正确的。HomeActivity的exported设为true,但这里没有intent-filter,所以可能有问题。如果HomeActivity不需要被其他应用启动,应该将exported设为false,避免安全风险。 然后检查每个activity是否有必要的intent-filter。除了WelcomeActivity作为启动器外,其他activity都没有intent-filter,所以大部分exported设为false是对的,但HomeActivity的exported是true,可能需要修正。 另外,检查是否有重复或者无用的activity声明,用户列了很多activity,名称看起来都相关,可能没问题,但需要确认是否有不再使用的activity可以移除。 还有,应用的主入口是WelcomeActivity,因为它是LAUNCHER,而MainActivity的exported是false,这可能没问题,但需要确认应用流程是否正确,比如WelcomeActivity是否正确地跳转到MainActivity或其他页面。 再看看是否有其他权限缺失,比如如果应用需要访问网络,除了INTERNET,是否还需要其他权限,比如ACCESS_NETWORK_STATE,但用户可能不需要,所以暂时没问题。 总结下来,主要的问题点可能在HomeActivity的exported属性和networkSecurityConfig的配置是否正确,以及是否有不必要的权限。此外,检查所有activity是否都正确设置了exported属性,特别是没有intent-filter的应该设为false。</think>根据提供的AndroidManifest.xml代码,以下是需要检查和优化的关键点(按优先级排序): --- ### **一、安全性优化** 1. **HomeActivity导出风险** `android:exported="true"` 但未设置intent-filter $\rightarrow$ 建议改为: ```xml <activity android:name=".HomeActivity" android:exported="false" /> ``` *除非明确需要被其他应用调用* 2. **网络安全配置验证** `android:networkSecurityConfig="@xml/network_security_config"` $\rightarrow$ 检查是否在`res/xml/network_security_config.xml`中配置了明文通信允许规则: ```xml <!-- 如果必须支持HTTP --> <network-security-config> <base-config cleartextTrafficPermitted="true"/> </network-security-config> ``` --- ### **二、组件声明规范** 1. **MainActivity矛盾状态** 主活动名称但`exported="false"`,实际入口是WelcomeActivity $\rightarrow$ 确认是否符合设计逻辑 2. **重复Activity后缀** 存在`AddActivity`和`AddActivity2` $\rightarrow$ 建议重命名为语义化名称(如`AddEventActivity`) --- ### **三、基础优化项** 1. **API级别适配** ```xml <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="34"/> ``` $\rightarrow$ 若未显式声明,建议补充(Gradle中的配置需与manifest一致) 2. **备份策略明确化** `android:allowBackup="true"` $\rightarrow$ 若含敏感数据建议设为`false`并补充: ```xml <application ... android:fullBackupContent="@xml/backup_rules"> ``` --- ### **四、代码可读性改进** 1. **命名规范化** 存在中英混合命名如`AddOldActivity` $\rightarrow$ 建议统一为英文命名(如`AddElderActivity`) --- ### **五、必要补充说明** 1. **权限声明完整性** 当前只有`INTERNET`权限 $\rightarrow$ 若涉及网络状态检测需补充: ```xml <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> ``` --- ### **优化后代码示例** ```xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.yourdomain.app"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application android:allowBackup="false" android:fullBackupContent="@xml/backup_rules" android:icon="@drawable/tubiao" android:label="@string/app_name" android:networkSecurityConfig="@xml/network_security_config" android:roundIcon="@drawable/tubiao" android:supportsRtl="true" android:theme="@style/Theme.MyExamProject" tools:targetApi="34"> <!-- 主入口 --> <activity android:name=".WelcomeActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 其他Activity(示例修改两个) --> <activity android:name=".AddEventActivity" android:exported="false" /> <activity android:name=".HomeActivity" android:exported="false" /> </application> </manifest> ``` --- ### **操作建议** 1. 使用Android Studio的**Manifest Validation**工具(通过右键点击文件选择"Validate") 2. 运行`adb shell dumpsys package your.package.name`检查实际生效配置 3. 通过Lint检查(菜单栏 > Analyze > Inspect Code)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值