Gradle 故障排除
Requesting Permissions

Android 清单

Android 清单是一个 XML 文件,其中包含有关 Android 应用程序的重要元数据。此文件包含包名称、活动名称、主活动(应用程序的入口点)、配置、Android 版本支持、硬件功能支持和权限。

有关 Android 清单文件的更多信息,请参阅有关 Android 清单 (Android Manifests) 的 Android 开发者文档。

Unity 如何生成 Android 清单

在构建应用程序时,Unity 会按照以下步骤自动生成 Android 清单:

1.Unity 采用主 Android 清单。

2.Unity 查找插件(AAR 和 Android 库)的所有 Android 清单。

3.来自插件的清单通过 Google 的 manifmerger 类合并到主清单中。

4.Unity 修改清单,自动向清单添加权限、配置选项、使用的功能以及其他信息。

覆盖 Android 清单

尽管 Unity 会为您生成正确的清单,但在某些情况下,您可能希望直接控制其内容。

要使用您在 Unity 之外创建的 Android 清单,请将 Android 清单文件导入以下位置:_Assets/Plugins/Android/AndroidManifest.xml_。这将覆盖默认的由 Unity 创建的清单。

在这种情况下,Android 库的清单随后将合并到主清单中,得到的清单仍由 Unity 进行调整以确保配置正确。要完全控制清单(包括权限),必须导出项目并在 Android Studio 中修改最终的清单。请注意,我们仅支持 launchMode - singleTask

权限

Unity automatically adds the necessary permissions to the Manifest based on the Android Player settings and Unity APIs that your app calls from the script. For example:

有关权限的更多信息,请参阅 Android 开发者文档的 Android 清单权限 (Android Manifest Permissions) 页面。

请注意,如果插件通过在其清单中声明权限来请求权限,则在合并阶段会自动将权限添加到生成的 Android 清单中。插件调用的所有 Unity API 也会对权限列表有影响。

Android 6.0 (Marshmallow) 中的运行时权限

如果应用程序在 Android 6.0 (Marshmallow) 或更高版本的设备上运行,并且还以 Android API 级别 23 或更高级别为目标,则该应用程序会使用 Android 运行时权限系统 (Runtime Permission System)

The Android Runtime Permission System asks the app’s user to grant permissions while the app is running, instead of when the app is first installed. App users can usually grant or deny each permission when the app needs them while the app is running (for example, requesting camera permission before taking a picture). This allows an app to run with limited functionality without permissions. The Android.Permission class allows you to check whether specific permissions have been granted or denied. If a permission your app relies upon has been denied, you can inform the user why you need it and ask them to approve the permission. See Requesting Permissions.

Normally, your app prompts the user to allow what Android calls “dangerous” permissions on startup. See Android’s documentation on dangerous permissions for more information. However, if you don’t want your Unity Android app to ask for permissions on startup, you can add the following to your Manifest in either the Application or Activity sections.

<meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" /> 

添加此代码会完全禁止启动时显示权限对话框,但您必须小心处理运行时权限以避免崩溃。此方法仅适用于高级 Android 应用程序开发者。

有关运行时权限系统和权限处理的更多信息,请参阅 Android 开发者文档的请求权限 (Requesting Permissions) 部分。

检查生成的 Android 清单

要检查 Unity 为应用程序生成的最终 Android 清单,请在构建项目之后但在退出 Unity Editor 之前打开 Temp/StagingArea/AndroidManifest.xml 文件。

清单以二进制格式存储在输出包 (APK) 中。要检查 APK 中的清单内容,可使用 Android Studio APK Analyzer 或其他第三方工具(例如 Apktool)。


  • 2018–10–05 Page published with editorial review

  • 5.5 版中的更新功能

  • Added support for Android Runtime Permissions in 2018.3.

Gradle 故障排除
Requesting Permissions
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961