每个Android开发者第一次打开项目时,都会在app模块的根目录下看到这个特殊的XML文件。它就像Android应用的身份证和通行证,没有它,APK文件甚至无法被系统识别。我经历过无数次因为Manifest配置错误导致的诡异崩溃,比如忘记声明Activity导致点击图标无响应,或是权限漏声明引发安全异常。
这个文件的核心作用可以概括为三个维度:
关键提示:Android Studio 3.0之后,部分配置如权限声明会通过合并规则自动生成,但手动检查Manifest仍是必备技能。
xml复制<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0">
xml复制<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
xml复制<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="standard"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
xml复制<service
android:name=".MyService"
android:enabled="true"
android:exported="false">
</service>
xml复制<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
xml复制<permission
android:name="com.example.myapp.PRIVATE_PERMISSION"
android:protectionLevel="signature"/>
当引入第三方库时,经常遇到这些冲突:
解决方案:
xml复制<uses-permission
android:name="android.permission.READ_CONTACTS"
tools:node="remove"/>
在build.gradle中配置:
groovy复制productFlavors {
free {
manifestPlaceholders = [appName: "MyApp Free"]
}
pro {
manifestPlaceholders = [appName: "MyApp Pro"]
}
}
然后在Manifest中使用:
xml复制<application
android:label="${appName}">
xml复制<activity android:name=".DetailActivity">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="https"
android:host="example.com"
android:pathPrefix="/products"/>
</intent-filter>
</activity>
xml复制<application
android:fullBackupContent="@xml/backup_rules">
res/xml/backup_rules.xml示例:
xml复制<full-backup-content>
<exclude domain="sharedpref" path="sensitive_prefs.xml"/>
<include domain="database" path="user_data.db"/>
</full-backup-content>
| 问题现象 | 可能原因 | 解决方案 |
|---|---|---|
| 安装时提示"Conflict with existing package" | packageName重复 | 修改applicationId |
| 点击图标无响应 | 缺少LAUNCHER intent-filter | 检查MainActivity声明 |
| 权限请求不弹出 | targetSdkVersion>=23但未动态申请 | 添加运行时权限检查 |
| 跨应用调用Service失败 | exported未设置或权限不足 | 检查exported和自定义权限 |
我在实际项目中最常遇到的Manifest相关崩溃是:
建议在发布前使用这个adb命令验证:
bash复制adb shell dumpsys package your.package.name | grep -A 1 "Manifest:"