This question already has an answer here:
- Android 6.0 Permission Error 5 answers
Yesterday I set upped target and compile sdk to 23 API level. When I launched the app on Nexus 9 with Preview Android M (MPA441) the app crashes with following SecurityException:
FATAL EXCEPTION: main
Process: com.grouplogic.mobilecho, PID: 22554
java.lang.RuntimeException: Unable to create application com.mypackage.MyApplication: java.lang.SecurityException: getDeviceId: Neither user 10098 nor current process has android.permission.READ_PHONE_STATE.
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4710)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.SecurityException: getDeviceId: Neither user 10098 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:4175)
at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:706)
at com.mypackage.utils.DeviceUtils.isRunOnEmulator(DeviceUtils.java:63)
at com.mypackage.utils.DeviceUtils.getDeviceID(DeviceUtils.java:49)
at com.mypackage.domain.service.SettingService.setupPreferences(SettingService.java:79)
at com.mypackage.app.BaseApplication.createSingletons(BaseApplication.java:84)
at com.mypackage.app.BaseApplication.onCreate(BaseApplication.java:66)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1013)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4707)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Does somebody know why the permission is disabled by default for new installed app when I install app through Android Studio? What is the correct way to avoid this crash, maybe there is way to check enable or not target permission and only if it is enabled the app should use some specific device API (read phone state, contacts and other permissions)? In the Internet I've just found this mention related to this problem1, but there I didn't find any clear explanation: is it bug in Android M or developer cannot understand how to use permission in a safe manner!? Also I've read this post but approach when user should go to settings and enable permission for app looks strange and not user friendly. I guess if user installs app from the Google Play everything will be fine because he explicitly to apply requested permission, but I need to confirm it.