[Open Source] AndroidRuntimePermissions - manage runtime permissions "synchronously" on Android M+

AndroidRuntimePermissions helps you query/request runtime permissions synchronously on Android M and later. It also works on older Android versions and detects whether a requested permission is declared in AndroidManifest or not.

Asset Store: Android Runtime Permissions | Integration | Unity Asset Store
Also available at: GitHub - yasirkula/UnityAndroidRuntimePermissions: A native Unity plugin to handle runtime permissions on Android M+
Discord: yasirkula Unity Assets
GitHub Sponsors :coffee:

3477101--1249870--permission.png

Enjoy!

1 Like

Now available on Asset Store!

This is really a good and simple plugin to manage the permissions on several Android devices!

1 Like

Hi , thanks for the plugin.
I don’t have much experience in the permissions , and i can’t get it to work.
I tried :

AndroidRuntimePermissions.RequestPermission("android.permission.WRITE_EXTERNAL_STORAGE");
        Debug.Log(AndroidRuntimePermissions.CheckPermission("android.permission.WRITE_EXTERNAL_STORAGE") + " <==permission status");

I never got a “permission box” on android and keep getting “Denied”.
Is the code right? can you add an example how to do it right?
Thanks again .
Dror

1 Like

Yes your code is correct. Though, you can directly Debug.Log the result of RequestPermission. So, CheckPermission is redundant here.

For runtime permissions to work correctly, you have to declare the permission in AndroidManifest first. For WRITE_EXTERNAL_STORAGE, this can be achieved by setting Write Permission to External (SDCard) in Player Settings.

I got “AndroidManifest” in the assets/Plugins/Android folder, with the line :

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

As you suggested I went to the player setting and changed the “Write Permission” to “External (SDCard)”, and still no Permission popup and the debug show “Denied”.
Can you assume the reason?

After building your project to Android, can you check the contents of PROJECT_PATH/Temp/StagingArea/AndroidManifest.xml and see if android:maxSdkVersion=“18” is still there? If so, try removing that part from your manifest. It basically adds WRITE_EXTERNAL_STORAGE permission to devices with API version less than or equal to 18. So, if your device has a newer Android version, it won’t have WRITE_EXTERNAL_STORAGE permission. BTW, may I ask your Android version?

P.S. Also check Settings/YOUR_APP/Permissions to see if Storage permission is available there.

Ok, finally things are starting to happen.
I removed the "android:maxSdkVersion=“18” from the manifest in the SDK
And now permission is Granted! Will I have a side effects from removing that line?
I still cant save my screenshot into my phone , but at list now I know I have permission :-).
p.s - I have Android version 8.0.0

Other than adding a “Storage: Modify or delete the contents of your USB storage” permission description to your permissions list in Google Play, there won’t be any side effects.

If your app saves screenshots to a public directory like Gallery, then you have to have WRITE_EXTERNAL_STORAGE permission no matter which API it is, so you should be good to go. But if your app saves the screenshots to a private directory like Application.temporaryCachePath, then you won’t be needing WRITE_EXTERNAL_STORAGE permission for this.

I’m saving to “persistentDataPath”, and I got WRITE_EXTERNAL_STORAGE permission and still it’s fail (Works great in unity).

It’s working!!! Thanks a lot for you’re patinas and help!

1 Like

Hello, I am trying to get coarse gps for a lwp made with uLiveWallpaper. I get this when the Check for permissions is called:

AndroidJavaException: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkSelfPermission(java.lang.String)' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkSelfPermission(java.lang.String)' on a null object reference

Hmm, probably wallpaper apps don’t run on a UnityActivity, which is what is used by default by this plugin. If wallpaper apps do run on an activity, you can try changing these lines to use the correct one (you can request help from the uLiveWallpaper developer for this): UnityAndroidRuntimePermissions/Plugins/AndroidRuntimePermissions/AndroidRuntimePermissions.cs at 2bc145099f273447a3c0dbb7546c8f488b3da873 · yasirkula/UnityAndroidRuntimePermissions · GitHub

Thank you for your prompt reply!
I got curious and checked how Google has their Earth LWP since I know it uses the gps. They call it on the preview activity so I image you can’t call it from the wallpaper service. I was able to quickly write a call for the gps on the preview activity.

Excuse me. For Some reason asking for Camera Permission also asking to access multimedia files in the device, which is odd, and I was wondering if it is a common occurence
Any clues?

The only call I am making is:

public void OnFindAFactPressed(){
        #if UNITY_IOS && !UNITY_EDITOR
        iOSCameraPermission.VerifyPermission(gameObject.name, "OnIOSPermisionCallback");
        #elif UNITY_ANDROID
        AndroidRuntimePermissions.Permission result = AndroidRuntimePermissions.RequestPermission( "android.permission.CAMERA" );
        if( result == AndroidRuntimePermissions.Permission.Granted ) {
            OnAndroidPermission(true);
        }
        else {
            OnAndroidPermission(false);
        }
           
        #elif UNITY_EDITOR
        _uiManager.Pop();
        _uiManager.Push("SnapTarget");
        #endif
    }

Thank you beforehand.

That’s unexpected for me. I couldn’t find any leftover code that might have caused this issue, so I’m really unsure about the cause of it.

You are the man!

Thanks!
JD

1 Like

Hello
I have a problem. I added the following line to manifest:

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

but permission dialog had shown wneh i start the app and i dont want this
I googled on Unity Documentation and add the following:

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

But now this code
Debug.Log("Permission" + AndroidRuntimePermissions.CheckPermission("android.permission.READ_EXTERNAL_STORAGE"));
just always returns denied
I think the SkipPermissionDialog option just skip dialog any time and not only on app start, but how to make it correctly?

Probably the permission result is cached as Denied. Try uninstalling the app and then reinstalling it.

Awesome asset!! Many thanks :slight_smile: for IOS do you plan to have anything similar? Thanks