Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Deprecated API Usage in Vibration Plugin for Android #111

Closed
sourav-kundu443 opened this issue Oct 16, 2024 · 5 comments
Closed

Fix Deprecated API Usage in Vibration Plugin for Android #111

sourav-kundu443 opened this issue Oct 16, 2024 · 5 comments

Comments

@sourav-kundu443
Copy link

The VibrationPlugin.java file in the vibration plugin (version 2.0.1) is using or overriding deprecated APIs in Android, which triggers warnings during build. The plugin needs to be updated to use modern APIs, specifically handling Android API levels that have deprecated the older vibration methods (e.g., Vibrator.vibrate(long)).

The proposed fix is to replace the deprecated methods with newer equivalents, such as VibrationEffect.createOneShot() introduced in API level 26 (Android 8.0), while maintaining backward compatibility with older Android versions.

What to fix:

Replace deprecated Vibrator.vibrate(long) with VibrationEffect.createOneShot(long duration, int amplitude) for Android 8.0 and above.

Implement conditional logic to ensure backward compatibility for devices running Android versions below API 26.

@vladimir-boyko
Copy link

Any updates? Issue persists and we can see deprecation messages on app build if we use java compiler option -Xlint:deprecation

@benjamindean
Copy link
Owner

Any updates? Issue persists and we can see deprecation messages on app build if we use java compiler option -Xlint:deprecation

I'm still not sure if it's a good idea to remove the methods, since they're usage is already conditional and Flutter itself supports Android versions below 8.

@vladimir-boyko
Copy link

Any updates? Issue persists and we can see deprecation messages on app build if we use java compiler option -Xlint:deprecation

I'm still not sure if it's a good idea to remove the methods, since they're usage is already conditional and Flutter itself supports Android versions below 8.

Oh I see, maybe suppressing deprecation will work? Just looked for this problem on google and it seems that java compiler can't recognize that api is conditional unless there's an explicit annotation on it...

One more point, there's one warning that is not connected to vibrate method:
warning: [deprecation] VIBRATOR_SERVICE in Context has been deprecated
final Vibrator vibrator = (Vibrator) flutterPluginBinding.getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);

Found solution on stack overflow:
https://stackoverflow.com/questions/68466095/vibrator-service-string-is-deprecated-for-api-31

@benjamindean
Copy link
Owner

Oh I see, maybe suppressing deprecation will work? Just looked for this problem on google and it seems that java compiler can't recognize that api is conditional unless there's an explicit annotation on it...

I'll try using @TargetApi annotation tomorrow. Will see how it works. Suppression didn't help - it was there in the previous version of the plugin.

@benjamindean
Copy link
Owner

Sorry for the long wait. The recent release 3.1.0 have fixed a lot of previous issues. Here is the full changelog:

  • Add common vibration patterns for Android and iOS.
  • Add sharpness parameter for iOS.
  • Suppress deprecation warnings for vibrate method on Android.
  • The plugin has been recreated from scratch to align with the latest Flutter and Dart features.
  • The iOS version no longer depends on intensities and amplitude, and it now supports custom durations and patterns.
  • The example app is more intuitive and user-friendly.

Let me know if the issue persists. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants