READ_PHONE_STATE Permission isn’t in my Manifest or Plugins!
Unity automatically adds theREAD_PHONE_STATEpermission into builds when either:
Your scriptscontain code which require the permission.
Thetarget 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 aplugin in your project which has its own manifest filerequesting 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.
SomeSystemInfoproperties such asSystemInfo.deviceUniqueIdentifierrequire 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 onSystem.DateTime.Now.Ticksand 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 asdangerous permissionsas 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 listphone accountsregistered 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 asAndroid Buddywhich fits this exact purpose!)
Learn more about the READ_PHONE_STATE android permission!
There are three ways permissions get added to your project.
They are specified in an Android Manifest file.
They are specified in library (a .aar file).
Unity adds the permission when you use a certain feature. (Added)
My examples use command-line tools on a Mac. I don't know Windows equivalents, but it is possible to find and run unix tools there (using the linux subsystem for windows 10, cygwin, custom binaries, etc.)
1. Find all permissions used in (uncompressed) Android Manifests.
cd /path/to/my/project/Assets grep -r "uses-permission"--include "AndroidManifest.xml".
This will find all files named AndroidManifest in the current folder (.) or any of its subfolders (-rtells it to search recursively) and spit out any line with the words 'uses-permission'.
In my current project, I get output something like this:
2. Find the permissions required in Android Libraries
Your project likely contains android libraries (.aar files) and java archives (.jar files). Some android libraries contain an android manifest and specify permissions needed to use the library. (I don't think .jar files actually do this, but .aar files absolutely do). Both .aar and .jar files are .zip files, with a different extension and with specific metadata in specific places.
Here's what this does. It finds any file (in the current folder (.) and its subfolders) has an extension of (something) a r, thus .jar, or .aar (-name "*.?ar"). It outputs the archive's file name (-print). It then runszipgrep(-exec). Zipgrep is told to search through any files in the archive ({}) named "AndroidManifest.xml", and output any line with the words "uses-permission". We then pipe the errors to the bit bucket (2> /dev/null) so we don't see lots of errors about archives that don't have android manifests in them.
The filenames all start with periods. I can thus see, for example, that the onesignal-unity.aar sets several permissions, several .jar files were searched with no permissions inside them, and some of the play services libraries specify permissions.
If I needed to change a library, I could rename the .aar to .zip, extract it, edit it, compress it, and rename it back. (It isn't necessarily wise to change the permissions inside a library, but possible.)
3. Unity Adds the Permission
I didn't have anything to add on this; as said above, if you use the Microphone API, Unity will add a permission for you so your app will work.
However, I've since realized that you can do the following:
bring up the Build Settings for Android
tick the 'Export Project' box
Export the project, noting the location
go to /my/project/export/src/main/AndroidManifest.xml. This is what Unity emits for the android manifest (before google's tools do all the merging).
compare it (using your favourite diff tool) to Assets/plugins/Android/AndroidManifest.xml; the differences come from Unity.
Unity v5.6.7, Android SDK 최신 버전에서 READ_PHONE_STATE 권한을 제거 했는데도 계속 추가되는 문제가 있어서 알아본 결과 Android SDK 최신 버전에서 필요하다고 판단되면 READ_PHONE_STATE를 강제로 추가하고 있다.
(Unity 최신버전(현재 Unity 2018이상 버전)에서는 해당 버그가 수정된 듯 하다.)
내 경우는 SystemInfo.deviceUniqueIdentifier를 사용하게 되면서 필요하다고 판단해서 READ_PHONE_STATE를 추가시키고 있었다.
아래 설명대로 Android SDK 25.2.5 버전에서는 강제로 추가하는 문제가 없어서 SDK 버전을 되돌렸는데
Manifest Merge 관련 문제가 많이 나와서(최신 Android SDK에서는 자동 정리해주는 중복 Manifest 관련 문제를 25.2.5에서는 자동으로 해결해주지 못해서 직접 수정함) 문제되는 aar 파일 안의 AndroidManifest 파일을 모두 수정해서 해결 했다.
한 10개정도 수정한 듯..
-------------------------------------------------
Steps to reproduce:
1) Update SDK to the latest version (26.0.2) 2) Download attached project 'Repo.zip' and open in Unity 3) Build .apk file 4) Open 'AndroidManifest.xml' file (Temp/StagingArea/AndroidManifest.xml)
Expected result:READ_PHONE_STATE permission should not be added in the AndroidManifest.xml (check attachment 'AndroidManifest_created_with_26.0.2.xml') Actual result: READ_PHONE_STATE permission is added in the AndroidManifest.xml (check attachment 'AndroidManifest_created_with.25.2.3.xml')
Note: READ_PHONE_STATE permission wasn't added using 25.2.5 sdk
RESOLUTION: By design. The project includes a plugin (com.nerd.TapdaqUnityPlugin) which does not specify targetSdkVersion in its manifest. Thus android manifest merger correctly assumes that the SDK version is lower than 4 and implicitly grants the READ_PHONE_STATE permission. Seehttps://developer.android.com/reference/android/Manifest.permission.html#READ_PHONE_STATE. The difference between 25.2.5 and 26.0.2 SDKs is that in the latter we use different manifest merger implementation which is more strict.