libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
9.95k stars 1.84k forks source link

Android: `ASensorManager_getInstance()` deprecated since API level 26 #11404

Open hhromic opened 1 week ago

hhromic commented 1 week ago

The SDL Sensor API in Android currently uses ASensorManager_getInstance() to initialize sensors. However, this function has been deprecated since API level 26 and replaced with ASensorManager_getInstanceForPackage() instead. More info here.

This deprecation generates the following warning when building:

C/C++: src/sensor/android/SDL_androidsensor.c:56:26: warning: 'ASensorManager_getInstance' is deprecated: first deprecated in Android 26 - Use ASensorManager_getInstanceForPackage instead [-Wdeprecated-declarations]
C/C++:    56 |     SDL_sensor_manager = ASensorManager_getInstance();
C/C++:       |                          ^
C/C++: /opt/android-sdk/ndk/27.2.12479018/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/android/sensor.h:751:17: note: 'ASensorManager_getInstance' has been explicitly marked deprecated here
C/C++:   751 | ASensorManager* ASensorManager_getInstance()
C/C++:       |                 ^
C/C++: 1 warning generated.

This is the only warning we can see about SDL (using version 2.30.9, but the same code is used in SDL3) at the moment.

Perhaps a pre-processor condition could be used to use the older function if API level < 26 and the new one otherwise?

slouken commented 1 week ago

It's not quite that simple, it needs to be a runtime check, and you need to dynamically load the new function so that SDL continues to function on older devices. We also need some way of identifying what package is used for loading.

Feel free to submit a PR. :)

hhromic commented 1 week ago

Doh, indeed it has to be a runtime check, not a compiler preprocessor one. Brain glitch! :) I also agree about the problem of having to determine what package name to use. I'm not really an Android developer, so I didn't know about also having to dynamically load one function or the other, but makes sense.

While I would love to contribute to fixing this deprecation warning, I'm probably not the right person given that I am not experienced enough in Android applications at the required level for these considerations.

I just checked the Deprecation Policy for Android 15, and looks like deprecated APIs are not expected to be removed:

Deprecation means that we've ended official support for the APIs, but they will continue to remain available to developers.

So at least fixing the deprecation has no immediate urgency.

slouken commented 1 week ago

Yep!