Requesting Android Marshmallow runtime permissions from Qt

In response to requests like this QTBUG-53759 Qt 5.8 Beta now allows Android Marshmallow runtime permission requests through private API as described here.   As such the approach described in this post may still be used but is being replaced with more up-to date methods using Qt 5.8 Beta and above.

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 RequestPermissions.java.

A java "android.support.v4" 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.

 

www.bytran.org -|- 2016