ML Permissions in Unity C# scripts to call AAR methods

[We're working with Unity Ed. 2022.2.19f1, Win10, MLSDK v1.4.0-dev2 ML Unity v1.11.0.]

We've built an AAR and call it from a Unity method, in which we set up callbacks to request ML2 write permissions:

MLPermissions.RequestPermission(MLPermission.WriteExternalStorage, permissionCallbacks);

At runtime, this permission callback gives permissions as we'd expect, but the app fails to write to storage. Because the AAR is out of Unity's scope, we see no log messages.

Note that our Unity ‘AndroidManifest.xml’ in /Plugins/Android sets both Android and ML permissions as follows. As Unity's manifest overrides others this should be sufficient to allow the AAR methods to write to disk. Is this correct? The permissions block follows:

> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
> <uses-permission android:name="android.permission.CAMERA" />
> <uses-permission android:name="com.magicleap.permission.WRITE_EXTERNAL_STORAGE" />
> <uses-permission android:name="com.magicleap.permission.CAMERA" />
> <uses-permission android:name="com.magicleap.permission.EYE_TRACKING" />
> <uses-permission android:name="com.magicleap.permission.SPATIAL_MAPPING" />
> <uses-permission android:name="com.magicleap.permission.HAND_TRACKING" />

The ‘AndroidManifest.xml’ for the Java code also requests permissions in this way:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

When calling Java methods on their own, the above grants write permission effectively, but when packaged in a library as an AAR our Java methods can’t write to disk.

In Android, permissions are associated with the APK, not with individual pieces of code or libraries. For a Java Android app that calls native C++ code through JNI (Java Native Interface), the permissions are determined by the AndroidManifest.xml file of the Java Android app. The native code runs in the same process and under the same security context as the Java code, so the permissions for the native code are the same as those granted to the Java app.

As above, I believe Unity handles things in the same way -- the manifest for the C# code calling our AAR should determine our permissions.

Why then can't we write to the ML2 from our AAR methods?

Hi Kevin,

For logging- You should be able to write to logcat or perfetto in the Java plugin. Btw, in case you were looking to monitor the Unity debug log at the same time, on Android the Unity player pipes it to logcat, so you can just use logcat or Perfetto to monitor both.

Also I believe you are correct. I don't think you should have to declare/request permissions for your plugin separately from your Unity script code / whatever manifest you have assigned in Unity. Dealing with plugin libraries in Unity can be a little fiddly. You may have already tried this, but one way that you can sanity check what it is doing when you build for Android is to export your project when you build in Unity.

With this option enabled, the editor build process with produce a gradle project that you can open, build, and debug in Android Studio instead going straight to building an apk.

Note- it may have been fixed recently, but due to a long-standing bug in the XR Plugin Management package that will cause the manifest to be removed from the build output when exporting projects for Android, you may need to implement one of the work-arounds in this thread-
Commenting out the call to CleanupAndroidManifest() in OnPostprocessBuild() in\Editor\XRGeneralBuildProcessor.cs has worked for me in the past.

Assuming, something's off with the manifest, you can also sanity check that the manifest is correct simply by opening the apk that is output by Unity in Android Studio and reviewing the manifest.