-
-
Notifications
You must be signed in to change notification settings - Fork 481
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
v2 Android embedder plugins #145
Comments
I believe it is possible, although it would be difficult at this stage. The migration guide is limited, the Flutter team has so far updated only 2 of its 24 1st class plugins to demonstrate how to support v2, and unfortunately neither of those 2 happen to serve as demonstrative examples for plugins with background tasks. As such, I would prefer to wait for at least one of their 1st class plugins to be updated that supports background tasks. e.g. Although, I would welcome a pull request from anyone who is motivated to figure it out from the API documentation. There also may be a way to support legacy plugins as is (see #138) although again the documentation is a bit lacking at this stage. |
well than it seems i will have to wait for udates..Thanks a lot you were pretty supportive towards issues thanks again and all the best |
https://github.com/flutter/flutter/projects/59 is the first party migration state. Most of them are updated but you're right in that |
Does this mean that I can't use audio_service and android_alarm_manager together for now ?
The android_alarm_manager README says it supports v2 embedding with Flutter >= 1.12. I am on v1.13.6. |
I believe it is related to this issue, yes. Currently, it seems that those who created their projects pre-1.12 are having no issues (like myself), while those who created their projects post-1.12 are facing the problem. So the workaround for now is to use an old project template for your project. I would ideally like documentation from the Flutter team since time is too short to spend figuring undocumented things out, but if that documentation doesn't come out within the next two weeks (or sooner) I will begin those efforts. In terms of attracting more attention to this issue, these are the GitHub issues and Medium articles where I have left a comment already: flutter/flutter#32164 Simply upvoting/clapping those comments may help increase visibility. |
Thank you for your quick answer, I will try with an old project. |
I tried with a Flutter 1.5.4 project, but unfortunately it didn't work. |
Just to update this issue, I will have a crack at a v2 implementation this weekend. |
I've just committed a v2 implementation to Git. Hopefully this doesn't break projects still using the old project template, but I would appreciate some feedback if anything is broken for both new and old-style projects. |
Hi @ryanheise To Reproduce
|
Thanks, @rohansohonee I'm looking into it. |
I tried the v2 implementation and the app works like before. However I am still having the issue described in my previous message. I tried in a new empty project and with a different alarm plugin (workmanager). A MissingPluginException is thrown no matter what. |
Visit Upgrading-pre-1.12-Android-projects and follow the Full-Flutter app migration steps. MissingPluginException might be fixed if you add this to your public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
} Hope this is helpful. |
Also I believe this can be fixed by using a sufficiently recent version of Flutter where plugin registration will be done automatically through reflection. If it's not in stable now, it should be soon (or you can try beta). |
Hi @rohansohonee, thank you for the answer. I followed the guide but it didn't solve the issue.
And this is the code I use in the new testing project :
The exception thrown is identical in both projects :
|
@MickaelHrndz confirmed on both |
@dkobia @MickaelHrndz can you reproduce this on the I added If it works when modifying the example in this way but does not work in your own project, then you can compare the two projects to spot the difference and maybe re-check everything in the v2 migration guide: https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects I am not sure what specifically is the cause, but the most important thing is to add this to your manifest:
But there are other more subtle things such as to remove this:
The problem is that this references the old (pre-v2) class name and may drag in classes that can cause problems. |
Using the audio_service example project, with the code :
If I don't add the android_alarm_manager service and receivers in the manifest, # 1 # and # 2 # are never printed and no error is thrown. "AlarmService started!" is still printed though. If I do, # 1 # is printed but the same error is thrown by the same line, and therefore # 2 # is not printed. So I guess the issue really stems from android_alarm_manager ? |
@MickaelHrndz I didn't realise you were using Stepping back and looking at what you're trying to do, it seems you want to periodically play a sound in the background every second. There are a few problems with using
|
@rohansohonee regarding the memory leak, this appears to be a bug in the Flutter engine (see flutter/engine#16204) but if there is a workaround before that pull request lands, I'll implement it. |
@rohansohonee Here is a workaround for the leak: public class MainActivity extends FlutterActivity {
@Override
public FlutterEngine provideFlutterEngine(Context context) {
// Instantiate a FlutterEngine.
FlutterEngine flutterEngine = new FlutterEngine(context.getApplicationContext());
// Start executing Dart code to pre-warm the FlutterEngine.
flutterEngine.getDartExecutor().executeDartEntrypoint(
DartExecutor.DartEntrypoint.createDefault()
);
return flutterEngine;
}
} What this does is override the instantiation of the main activity's This workaround should not be needed once they fix it upstream. If that works for you, I'll likely publish a new release with the v2 implementation, and can sort out any remaining issues after that release. |
@ryanheise The 1s delay was to avoid waiting while testing. I am building a radio app that lets you optionally set an alarm clock to wake up to it. That's why I need to use audio_service in android_alarm_manager.
What would I use if not |
You might have to implement your own media notification. |
@rohansohonee I guess but I'm already using just_audio wrapped with audio_service and it is working just fine. I don't want to rewrite my code and a whole plugin, just to run into the same issue I have here. But yeah, just_audio is working in the alarm callback. If you meant a different notification specifically for the alarm, it might actually work and be good enough. I will give it a try, thanks for the idea. |
Hey guys just letting you all know a new version of Flutter has been pushed on stable channel. |
@ryanheise |
@MickaelHrndz OK, I have a clearer picture of your use case now. That is not actually allowed on Android due to background service limitations: https://developer.android.com/about/versions/oreo/background.html#services So the only way you can really achieve what you're trying to achieve is to keep the audio service running the whole time in the background, effectively on "pause", and start playing at the scheduled time, and then you would essentially do away with alarm manager. I know this is not an ideal solution since it will unnecessarily use battery when it could be sleeping. To implement this properly, you may need to get your hands dirty with some Android programming and research whether there is a certain way to build this use case on Android. |
@ryanheise Interesting. Is there a way to hide the audio_service notification ? I can make it work with another notification from flutter_local_notifications, but the player doesn't stop when the alarm in cancelled. |
@MickaelHrndz You can't hide the notification. This is another requirement imposed by Android when you start a foreground service. You have to let the user know that you're consuming resources in the background, and this notification is the way this is done. I don't know a way to hook into hot restart and dispose resources. |
I think i might have a solution for this use case. Follow these steps:
You may need to do some android coding to launch an Activity from the background isolate. But once you manage to launch the activity along with some parameter in the activity's intent you should be easily able to start the audio_service without any issues. |
@MickaelHrndz @dkobia Are either of you still getting the |
@rohansohonee That's an even better idea, thank you. I tried with both the flutter_appavailability and a custom method channel, but I still get this dreaded MissingPluginException. I would think I can't interact with the platform code at all inside the alarm callback, if it weren't for just_audio actually working. I'm pretty clueless. @ryanheise since the ones I get are not really related to audio_service itself, it's fine by me. Thank you again for your help. |
@MickaelHrndz
IMPORTANT <meta-data
android:name="flutterEmbedding"
android:value="2" /> and your MainActivity.java public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
} If you still keep getting MissingPluginException then take this issue up with the flutter team.
|
@ryanheise |
Closing. Note: this is feature has now been published as release 0.6.0. |
@ryanheise yes thanks the issue was resolved. Interestingly the MissingPluginException was caused by another audio plugin that seems to break v2 reflection altogether -- https://pub.dev/packages/audiofileplayer -- not sure how or why. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs, or use StackOverflow if you need help with audio_service. |
Hi,
Is it possible to update your plugin for being compatible with v2 Android embedder plugins ?
Thx.
The text was updated successfully, but these errors were encountered: