Qt sensors just got better on Android A rewrite with huge performance gains for the Android sensors plug-in
Each month KDAB schedules time for me to maintain the Qt for Android port. Usually I use it to review the pending patches or fix bugs, but sometimes there is a quiet month and I have time to add new functionalities or improve the existing ones.
April was quite quiet so I decided to rewrite the Android sensors plugin (no, this is not a late April fool’s joke).
Why on earth did I want to do that? Why try to fix something that works? After all, the current implementation faithfully served us for years!
I decided to rewrite it after I did some performance tests that showed some surprising numbers. When I used the NDK API to get sensors data, the CPU usage dropped from over 60% to even less than 20% (it fluctuates from 15% up to 30%)! Yup, that’s a surprising performance improvement, which TBH I hadn’t expected at all!
I expected it to be faster, as the NDK API doesn’t need to move the data to JAVA and then back to C/C++ world (via JNI) but, I never dreamed it would be that fast!
Are you wondering why we didn’t use the NDK API in the first place? It’s because the sensors NDK API was not introduced until API-16, which means that when we first implemented the sensors API, we were supporting Android from API-9. However, starting with Qt 5.9, the minimum API has been raised to API-16, so, now I can safely use NDK API for this job.
Here you can find the pending patch. It targets dev branch which will be part of Qt 5.14.