READ_PHONE_STATE Permission isn’t in my Manifest or Plugins!
Unity automatically adds the READ_PHONE_STATE permission into builds when either:
- Your scripts contain code which require the permission.
- The target SDK version isn’t set (or set below 4) which causes the manifest merger to assume the SDK version is lower than 4 and the system will implicitly grant the READ_PHONE_STATE permission to the app. (in later versions of Unity 5 and Unity 2017 the target SDK version is now set in the editor making managing it much easier)
- You have a plugin in your project which has its own manifest file requesting the permission (the manifest can be contained within your jar or aar files, they’re not always simply in your project) – Note that if a plugin is requesting a permission then it’s probably required and may cause issues if removed, check the plugin documentation if you’re unsure!
Code which causes Unity to automatically add the permission
In the case of your scripts containing code which need the permission. Unity automatically adds the permission when using functions which require it.
Some SystemInfo properties such as SystemInfo.deviceUniqueIdentifier require the READ_PHONE_STATE permission so referencing it in a script will force Unity to add it.
As an alternative to SystemInfo.deviceUniqueIdentifier when needing a unique device identifier and it’s not important to keep it the same between wiping save data. Consider generating a unique value based on System.DateTime.Now.Ticks and storing in the playerprefs instead.
Why am I asked to allow/deny the permission at app launch?
Starting with Android 6.0 the user was given more control over permissions apps were allowed to use at runtime; rather than a blanket list of confusing permission being included with the app at installation. Permissions which are prompted for the user to allow are classified as dangerous permissions as they can allow the app to access sensitive data. (in this case READ_PHONE_STATE can allow reading of the phone number, call statuses or list phone accounts registered on the device)
If you want to manually control when the permissions are requested at runtime rather than all dangerous permissions just being prompted at startup then you can add:
<meta-data android:name=”unityplayer.SkipPermissionsDialog” android:value=”true” />
Between the <application> tags of your manifest file.
With the permission dialog skipped all dangerous permissions will remain defaulted as denied! But this allows you to request permissions manually when you need them, rather than all at once at app launch. (However note that you’ll either need to write a Java plugin yourself to control this or find an already built plugin from the store such as Android Buddy which fits this exact purpose!)
Learn more about the READ_PHONE_STATE android permission!
You can read more about the READ_PHONE_STATE permission on the android developer site at https://developer.android.com/reference/android/Manifest.permission.html#READ_PHONE_STATE