状态:尚未解决
我使用GearVR构建并安装应用程序后,默认情况下会询问:
XXX正在请求访问您设备上的照片,媒体和文件的权限。允许?
允许/拒绝
我不需要我的应用访问上述任何内容。我怎么能以编程方式摆脱这个?
Unity似乎会因某种原因自动将 READ_EXTERNAL_STORAGE
添加到任何版本中。剥离权限对于较新版本的Unity没有帮助,因为构建失败并显示消息:
无法合并Android清单...
OVRPlugin会导致此问题。
https://forum.unity3d.com/threads/how-to-remove-android-permission-read_external_storage.429369/
Unity论坛表示不太可能修复。也许还有一些解决方法?
Oculus正在努力,我们应该在二月份收到修复
答案 0 :(得分:1)
前段时间我偶然发现了这一点,但仍然不确定为什么Unity构建会要求这些权限。无论如何要摆脱它们,您可以使用编辑器脚本作为解决方法:
protected void BuildAPKCustom(string buildPath)
{
bool manifestFileExists = false;
if(File.Exists(Path.Combine(Application.dataPath, "Plugins/Android/AndroidManifest.xml")))
{
manifestFileExists = true;
File.Copy(Path.Combine(Application.dataPath, "Plugins/Android/AndroidManifest.xml"), Path.Combine(Application.dataPath, "Plugins/Android/tmpManifestFile.xml"));
File.Delete(Path.Combine(Application.dataPath, "Plugins/Android/AndroidManifest.xml"));
}
string[] levels = new string[EditorSceneManager.sceneCount];
for (int i = 0; i < levels.Length; i++)
{
levels[i] = EditorSceneManager.GetSceneAt(i).path;
}
BuildPipeline.BuildPlayer(levels, buildPath, BuildTarget.Android, BuildOptions.None);
XNamespace android = "http://schemas.android.com/apk/res/android";
string generatedXMLFilePath = Path.Combine(Application.dataPath, "../Temp/StagingArea/AndroidManifest.xml");
XDocument doc = XDocument.Load(generatedXMLFilePath);
IEnumerable<XElement> permissionElements = doc.Root.Elements("uses-permission");
foreach(XElement permission in permissionElements)
{
int potentialIndex = PERMISSIONS_NAMES_TO_DELETE.IndexOf(permission.Attribute(android + "name").Value.Replace("android.permission.", ""));
if(potentialIndex >= 0)
{
UnityEngine.Debug.Log("Permission deleted : " + PERMISSIONS_NAMES_TO_DELETE[potentialIndex]);
permission.Remove();
}
}
XElement overwritenPermission;
for(int i = 0; i < PERMISSIONS_NAMES_TO_DELETE.Length; i++)
{
overwritenPermission = new XElement("uses-permission");
overwritenPermission.Add(new XAttribute(android + "name", "android.permission." + PERMISSIONS_NAMES_TO_DELETE[i]));
overwritenPermission.Add(new XAttribute(android + "maxSdkVersion", "18"));
doc.Element("manifest").Add(overwritenPermission);
}
if(!Directory.Exists(Path.Combine(Application.dataPath, "Plugins/Android")))
{
Directory.CreateDirectory(Path.Combine(Application.dataPath, "Plugins/Android"));
}
doc.Save(Path.Combine(Application.dataPath, "Plugins/Android/AndroidManifest.xml"));
BuildPipeline.BuildPlayer(levels, buildPath, BuildTarget.Android, BuildOptions.None);
if(manifestFileExists)
{
File.Delete(Path.Combine(Application.dataPath, "Plugins/Android/AndroidManifest.xml"));
File.Copy(Path.Combine(Application.dataPath, "Plugins/Android/tmpManifestFile.xml"), Path.Combine(Application.dataPath, "Plugins/Android/AndroidManifest.xml"));
File.Delete(Path.Combine(Application.dataPath, "Plugins/Android/tmpManifestFile.xml"));
}
Process.Start(Path.GetDirectoryName(buildPath));
}
(PERMISSIONS_NAMES_TO_DELETE
是要删除的权限名称的string[]
。要获取权限名称列表,您可以查看here)
它的作用是:
删除权限背后的想法是将最大API级别设置为较低级别,权限将无法使用(请注意此API级别上存在权限必须。)
希望这有帮助,