Cannot get WAKE_LOCK permission on Android 8 Oreo

2020-03-24 02:53发布

问题:

My app has <uses-permission android:name="android.permission.WAKE_LOCK" /> added to the AndroidManifest.xml. However calls to acquire the lock PowerManager.PARTIAL_WAKE_LOCK crash the app on Oreo devices.

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.SecurityException: Neither user 10094 nor current process has android.permission.WAKE_LOCK.

回答1:

Your app is probably using a new version of ACRA library. In new the version they added <uses-permission android:name="android.permission.WAKE_LOCK" android:maxSdkVersion="25" /> to the lib AndroidManifest.xml file. The android:maxSdkVersion="25" is silently merged to main app AndroidManifest.xml file, hence the app doesn't have WAKE_LOCK permission on Oreo devices. The solution is add tools:node="replace" to your uses-permission declaration.

For example <uses-permission android:name="android.permission.WAKE_LOCK" tools:node="replace" />.

Update: in recent versions of ACRA library that unfortunate "feature" has been removed.