How to require android permission in C++

Is there a way to use C++ to acquire android permission?
Have an example?

For static premmission request you need to add it yo manifest and oyu can do that in Project Settings, inside Android section and Advance APK package

Fo dynamic request you need to use same functions that Blueprints are using:

those are static functions so you call them like that UAndroidPermissionFunctionLibrary::AcquirePermissions(MyPermissionArray);

Now when you request premission, enigne code does not halt waiting for user responce, it continue running and to get user reponce you needs bind in to delegates that AcquirePermissions return in UAndroidPermissionCallbackProxy (alternaticly you can check permission if user wants to access feature that requires permission)

The bind function with those arguments:

const TArray<FString>& Permissions, const TArray<bool>& GrantResults

index of one array matches the another, if you ask for one permission just read index 0

You can practice this in blueprint before trying in C++ since those functions are avable there too :slight_smile:

Remember that you need to add AndroidPermission module in to dependency in *.build.cs

Now if im not mistaken this should build on Windows build too considering there blueprint nodes for that, but if you get any linker errors, you need to isolate Android code with PLATFORM_ANDROID:

#if PLATFORM_ANDROID

//Android code goes here

#endif

Now final importent thing, in both cases the names of permission need to be in full java code permission names with proper namespace, same you would put them in manifest.xml in normal android application. So heres list of permissions:

You need to put in android.permission. namespace prefix so for example for BLUETOOTH it will be android.permission.BLUETOOTH and so on

#if PLATFORM_ANDROID
TArray permissionName = { “android.permission.CAMERA” };
if (!UAndroidPermissionFunctionLibrary::CheckPermission(permissionName[0]))
{
UAndroidPermissionFunctionLibrary::AcquirePermissions(permissionName);
}
#endif

A simple example shows above.