Adding the Layer library dependency in Android sup

2019-02-20 23:44发布

问题:

I made a very, very small demo app reproducing the bug on a Nexus 5 running Android version 6.0.1. The app is on github here: https://github.com/lexi-sr/LayerPermission

I recently added 2 commits in which it targets API 23 and requests permissions at run time, but it still didn't work.

In these 2 commits, it has these settings:

  • Target SDK: 23
  • compiles Layer 0.20.3

1) In the commit "Removing layer dependency allows the popup dialog to request the perm…", where it does NOT have layer dependency: The method ActivityCompat.requestPermissions opens a dialog that requests the Contacts permission, and a log statement within the onRequestPermissionsResult method logs that the permission has been granted.

2) In the commit "Requesting permissions does not work" where it DOES have layer dependency: ActivityCompat.requestPermissions does not open up any dialogs, but a log statement within onRequestPermissionsResult still prints, logging that it does not have the permission.

It seems like adding the layer dependency is suppressing the ability to request for permissions at run time. Why is this happening?

回答1:

Luckily, the layer support team was able to help me with this. It solved my problem in the demo app (which targeted SDK 23) and my real app (which targeted SDK 22, to avoid requesting permissions at runtime). After I put tools:node="replace" into my uses-permission line for GET_ACCOUNTS, the pop up dialog was able to appear and grant the permission in the demo app, and the permission was no longer missing in the real app which targeted SDK 22.

Here is the detailed explanation from the Layer support team:

The layer SDK requests the GET_ACCOUNTS permission using a maxSdkVersion of 18. It would appear that when the manifests get merged this is overwriting the permission request in your manifest, thus not requesting that permission for 19+. Could you try appending tools:node="replace" to the permission in your app's manifest? The line should read as:

<uses-permission android:name="android.permission.GET_ACCOUNTS" tools:node="replace" />

See here for the maxSdkVersion documentation: http://developer.android.com/guide/topics/manifest/uses-permission-element.html

See here for the tools:node documentation: http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger#TOC-tools:node-markers

Peter