Requesting Android Marshmallow runtime permissions from Qt

In response to requests like this QTBUG-53759 starting with version 5.10 Qt now supports Android Marshmallow runtime permission requests.   Using the newly instroduced functionality the old approach described below may be substituted with the new one as shown in these updated files: permissions.h, permissions.cpp, and   Please note that the usage of the Java code insert is now only used to show a native Android dialog with the optional permission rationale as it looks a bit better than the dialog provided by Qt.

The old approach based on Java code inserts

Android Marshmallow (Android M) Version 6.0 introduced a new permission model where permissions such as reading/writing to external storage etc. need to be requested at runtime so the programmer is required to implement the code for such runtime permissions to be obtained.   Simply declaring these permissions in the manifest file is no longer enough.   Below is a description with original post links and sample code of how such requests can be implemented using Qt prior to version 5.8 Beta.

Requesting Android Marshmallow permissions from Qt requires Java code inserts using QAndroidJniObject. In addition, for such calls to native runtime permission requests to work, the calls need to be made on the Android UI thread.   You can read more about running Qt requested code on Android UI thread under this link Qt android: interface C++ and java through JNI or here How to access and use Android Java API from your Qt on Android using JNI in a safe way.

Since Qt needs to know the result of the permission request, the Java side of the Android implementation needs to provide the result of the permission request.   This can be done by invoking a Qt method from Java which is a bit more involved as outlined in the links above.   For the above reason, it is possible to start a QTimer loop in the Qt permissions class and query Java code to obtain the result of the permission request as an alternative.   The resulting Qt class to request external storage read/write permissions is outlined in these permissions.h and permissions.cpp files with the corresponding java code available here

A java "" library called "support-v4-##.#.#.jar" (where ##.#.# depends on the library version) is required to be included in the Qt project as a dependency to request android permissions from java code.   This library is not available as a *.JAR file directly.   However it may be extracted out of the compressed *.ARR file containing it as described here how to convert AAR to JAR in the accepted up-voted answer.   Under Windows the ARR file containing this support library may be found at C:\Users\USERNAME\AppData\Local\Android\sdk\extras\android\m2repository\com\android\support\support-v4\##.#.#\ or something similar.

Even though unrelated to Android Marshmallow runtime permission requests, the following operational example may be useful in facilitating Qt Java integration share from a QML app on iOS and Android. -|- 2016  
© 2016 - 2022 Dzianis Pliutau