android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.4k stars 8.29k forks source link

Bug: WorkManagerSample can't be build #954

Closed AndroidDeveloperLB closed 3 years ago

AndroidDeveloperLB commented 3 years ago

/* Remove this comment

Reporting an issue with the Architecture Components or the samples? Yes

Question about Architecture Components?

No.

Bug report or feature request for Architecture Components?

Bug report.

Before filing a new issue, please check if it is listed in the Release Notes or reported in the issues list.

Issue with the samples?

Yes!

Please add the sample name ("all", "GithubBrowserSample", "BasicRxJavaSampleKotlin", etc.) and include it in the title if it applies. WorkManagerSample

WorkManagerSample.zip

Why so many questions of the same type for a simple thing?

I try to open the sample, and it can't be built: image

Android Studio 4.2 Beta 4 Build #AI-202.7660.26.42.7094744, built on January 21, 2021 Runtime version: 11.0.8+10-b944.6842174 amd64 VM: OpenJDK 64-Bit Server VM by N/A Windows 10 10.0 GC: G1 Young Generation, G1 Old Generation Memory: 4000M Cores: 12 Registry: external.system.auto.import.disabled=true, ide.settings.move.mouse.on.default.button=true, debugger.watches.in.variables=false Non-Bundled Plugins: String Manipulation, org.jetbrains.kotlin, com.google.mad-scorecard, org.intellij.plugins.markdown

keyboardsurfer commented 3 years ago

This has since been fixed.

AndroidDeveloperLB commented 3 years ago

@keyboardsurfer Seems fine now. May I ask if indeed a foreground Worker is the perfect replacement to IntentService? I want to run something in the background (with foreground notification if the app isn't in the foreground) right away, with a queue.

keyboardsurfer commented 3 years ago

With Developer Preview 1 of Android 12 we introduced the concept of expedited jobs. We recommend to use WorkManager for this as well and have a sample available.

And yes, we recommend using WorkManager for your intended use case.

AndroidDeveloperLB commented 3 years ago

@keyboardsurfer I've noticed some problematic issues with WorkManager though, when trying to migrate from IntentService :

  1. It offers foreground option (using foreground service, probably), but I wonder if there is a way to show/hide the notification of the foreground operation (automatically), based on when it's possible and needed. Meaning that if it sees it's possible without a notification (example is when the app is already in the foreground), don't use it. Or when the user left the app but it needs to continue, or came back to the app (and so the notification can go away). Not to mention that sadly starting from some Android version, even if you have a very short foreground service operation, the notification will appear for much more than it. I think on Android S it changed, but still: WorkManager is supposed to be for a wide range of Android versions. All of these I could kinda do with IntentService. If WorkManager is the perfect replacement, so much that IntentService became deprecated, it should allow this too, either manually or automatically (or both).

  2. As opposed to IntentService (and even JobScheduler, that WorkManager uses), it can cause really weird issues with AppWidget, and Google doesn't even confirm it as a bug, saying that it's expected (WorkManager with AppWidget? What's the connection between them, that one could damage the flow of the other?), and that as a workaround (they therefore admit that we need a workaround) we should always have a pending job: https://issuetracker.google.com/issues/115575872 https://commonsware.com/blog/2018/11/24/workmanager-app-widgets-side-effects.html (see what CommonsWare has to say about this too).

    When people asked for better alternative to a workaround (even just an optional feature to disable this behavior by disabling the optimization), it was also denied: https://issuetracker.google.com/issues/119920965

    That's while having 0 documentation about the issue anywhere, on both the websites of Android and on the IDE. I've spent hours on this, thinking it was my fault in using WorkManager (or app-widget) the wrong way, not understanding why the app-widget suddenly refreshing more often than it should.

So what exactly is so good about WorkManager that it was chosen to be the the perfect replacement? And why IntentService was deprecated?

keyboardsurfer commented 3 years ago
  1. WorkManager does take framework versions into account when it comes to showing a notification or not when using 2.7-alpha01 or newer. Foreground status can change quickly and we recommend you treat a WorkManager operation in a way that it can continue running when your app leaves foreground.
  2. You have received a clear response on one of your duplicate issues on the ACTION_PACKAGE_CHANGED broadcast. The RescheduleReceiver gets removed from WorkManager, when there is no more work pending.
AndroidDeveloperLB commented 3 years ago

@keyboardsurfer

  1. But it was possible using a Service, of showing and hiding the notification. Why can't we have this today? If the app is already in the foreground, why have a notification of having a foreground service?
  2. Just because I was answered doesn't mean it's not a bug and shouldn't be fixed. What I was given is a workaround (and it was also written as such, "workaround"). The issue tracker is also not the proper place for documentation and explaining about weird end cases. In all of WorkManager and app-widget websites, there is nothing that talks about this behavior. Also there is no way for app-widget to ignore it, and there is no such a flag for WorkManager to avoid this behavior. All alternatives to WorkManager (including JobScheduler) don't have this behavior. Only WorkManager has this issue. Google handled it very poorly.