- Home /
[ANDROID] How to remove READ_PHONE_STATE
Hi all, I am currently facing a problem where the android build will ask the user "Allow (Project name) to make and manage phone calls" After doing some Google search, I've tried out following
Add
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="25"/>
to every androidmanifest.Add
<uses-permission android:name="android.permission.READ_PHONE_STATE" tools:node="remove" />
Remove SystemInfo.deviceUniqueIdentifier from the codes.
However, the build still asks for the permission. When I checked the AndroidManifest on temp/StagingArea, the file did NOT include <uses-permission android:name="android.permission.READ_PHONE_STATE" />
Here is my current set up.
Unity 5.6.3
Android SDK tools version 26.1.1
I am also using following plugins
Unity IAP (from the asset store)
Facebook SDK version 7.10.1
Cross Platform Native Plugins
Twitter SDK
Firebase Analytics
Please give us any suggestions on how to remove this permission. Thanks!
Answer by yasirkula · Feb 27, 2018 at 05:44 PM
There are 3 possible solutions. Here we go:
Add
<uses-sdk android:minSdkVersion="5" />
to your AndroidManifest and change the number with your Unity project's "Minimum API Level". If you don't have an AndroidManifest in your project, grab the default one from UNITY_INSTALLATION_PATH\Editor\Data\PlaybackEngines\AndroidPlayer\Apk (or somewhere around here) and copy it to the Assets\Plugins\Android folder of your projectInside your Android SDK installation path, delete the "tools" folder completely and replace it with version 25.2.3, officially available here: https://dl.google.com/android/repository/tools_r25.2.3-windows.zip (click here for all download options)(this one did the trick for me)
If none works, add
<uses-permission android:name="android.permission.READ_PHONE_STATE" tools:node="remove" />
to your AndroidManifest and build your project using Gradle build system
Credit: https://forum.unity.com/threads/unity-2017-read_phone_state-permission-cant-remove.486732/
Please DON'T downgrade the SD$$anonymous$$ tools to 25.2.3.
Answer by DavidSWu · Feb 21, 2018 at 02:25 AM
We are facing a similar problem with 2017.3.1, but we have the line:
In the output file
Answer by skdev3 · Aug 16, 2019 at 12:03 PM
#if UNITY_ANDROID
using UnityEngine;
using UnityEditor;
using UnityEditor.Build;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
class AndroidManifestFixBuildProcessor : MonoBehaviour, IPostprocessBuild
{
public int callbackOrder { get { return 1; } }
static string dataPath = "";
private static Thread _OnPostprocessBuildThread;
public void OnPostprocessBuild(BuildTarget target, string path)
{
Debug.Log("OnPostprocessBuild");
dataPath = Application.dataPath + "/../Temp";
Application.logMessageReceived += buildLogCallback;
if (_OnPostprocessBuildThread != null && _OnPostprocessBuildThread.IsAlive)
{
_OnPostprocessBuildThread.Interrupt();
_OnPostprocessBuildThread.Abort();
_OnPostprocessBuildThread = null;
}
_OnPostprocessBuildThread = new Thread(OnPostprocessBuildThread);
_OnPostprocessBuildThread.Start();
Debug.Log("Start OnPostprocessBuildThread");
}
private void OnPostprocessBuildThread()
{
while (true)
{
Debug.Log("Run AndroidManifestFixBuildProcessor");
AndroidManifestFixBuildProcessor.AndroidManifest(dataPath);
Thread.Sleep(200);
}
}
public static void buildLogCallback(string condition, string stackTrace, LogType type)
{
AndroidManifestFixBuildProcessor.AndroidManifest(dataPath);
if (condition.Contains("Build completed"))
{
if (_OnPostprocessBuildThread != null && _OnPostprocessBuildThread.IsAlive)
{
_OnPostprocessBuildThread.Interrupt();
_OnPostprocessBuildThread.Abort();
_OnPostprocessBuildThread = null;
}
Debug.Log("Stop OnPostprocessBuildThread");
Application.logMessageReceived -= buildLogCallback;
};
}
//
public static void AndroidManifestFinished(object sender, FinishedArgs args)
{
if (args.LinesRemoved > 0)
{
Debug.Log("<color=#FF8000>File:" + args.Filename + " LinesRemoved:" + args.LinesRemoved + " TotalLines:" + args.TotalLines + " </color>");
}
}
public static void AndroidManifest(string buildDir)
{
List<string> removedParams = new List<string>();
removedParams.Add("android.permission.BLUETOOTH");
removedParams.Add("android.permission.CHANGE_WIFI_MULTICAST_STATE");
removedParams.Add("android.permission.READ_PHONE_STATE");
TextLineRemover.OnFinished += AndroidManifestFinished;
// Debug.Log(buildDir);
string[] files = Directory.GetFiles(buildDir, "*.xml", SearchOption.AllDirectories);
foreach (string file in files)
{
string patchfile = Path.GetFullPath(file);
string TempPath = Path.GetTempPath();
string patchfileTMP = TempPath + Path.GetFileNameWithoutExtension(file) + System.DateTime.Now.ToString() + ".tmp";
TextLineRemover.RemoveTextLines(removedParams, patchfile, patchfileTMP);
}
TextLineRemover.OnFinished -= AndroidManifestFinished;
}
}
/*
public class FixAndroidManifest
{
[MenuItem("Tools/FixAndroidManifest")]
static void FixAndroidManifestRun()
{
AndroidManifestFixBuildProcessor.AndroidManifest(Application.dataPath + "/../Temp");
}
}
*/
public static class TextLineRemover
{
public static void RemoveTextLines(IList<string> linesToRemove, string filename, string tempFilename)
{
// Initial values
int lineNumber = 0;
int linesRemoved = 0;
System.DateTime startTime = System.DateTime.Now;
// Read file
using (var sr = new StreamReader(filename))
{
// Write new file
using (var sw = new StreamWriter(tempFilename))
{
// Read lines
string line;
while ((line = sr.ReadLine()) != null)
{
lineNumber++;
// Look for text to remove
if (!ContainsString(line, linesToRemove))
{
// Keep lines that does not match
sw.WriteLine(line);
}
else
{
// Ignore lines that DO match
linesRemoved++;
InvokeOnRemovedLine(new RemovedLineArgs { RemovedLine = line, RemovedLineNumber = lineNumber });
}
}
}
}
// Delete original file
File.Delete(filename);
// ... and put the temp file in its place.
File.Move(tempFilename, filename);
// Final calculations
System.DateTime endTime = System.DateTime.Now;
InvokeOnFinished(new FinishedArgs { LinesRemoved = linesRemoved, TotalLines = lineNumber, TotalTime = endTime.Subtract(startTime), Filename = filename });
}
private static bool ContainsString(string line, IEnumerable<string> linesToRemove)
{
foreach (var lineToRemove in linesToRemove)
{
if (line.Contains(lineToRemove))
return true;
}
return false;
}
public static event RemovedLine OnRemovedLine;
public static event Finished OnFinished;
public static void InvokeOnFinished(FinishedArgs args)
{
Finished handler = OnFinished;
if (handler != null) handler(null, args);
}
public static void InvokeOnRemovedLine(RemovedLineArgs args)
{
RemovedLine handler = OnRemovedLine;
if (handler != null) handler(null, args);
}
}
public delegate void Finished(object sender, FinishedArgs args);
public class FinishedArgs
{
public int TotalLines { get; set; }
public int LinesRemoved { get; set; }
public System.TimeSpan TotalTime { get; set; }
public string Filename { get; set; }
}
public delegate void RemovedLine(object sender, RemovedLineArgs args);
public class RemovedLineArgs
{
public string RemovedLine { get; set; }
public int RemovedLineNumber { get; set; }
}
#endif //UNITY_ANDROID
Your answer
Follow this Question
Related Questions
How to increase number of supported devices when publishing to the google play store 0 Answers
CommandInvokationFailure: Gradle build failed. unity 2019.4.22 1 Answer
CommandInvokationFailure: Failed to re-package resources. 1 Answer
weird error prevent unity building apk PLZ help 2 Answers
com.android.tools.r8 compilation error please help 0 Answers