question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Workmanager library can't resolve WorkerParameters

See original GitHub issue

Describe the bug When the application tries to launch a particular worker in my closed source project the application crashes.

Caused by: org.koin.core.error.NoBeanDefFoundException: No definition found for class:‘androidx.work.WorkerParameters’. Check your definitions!

Subsequent launches of the same app will crash immediately when the app opens, with same error message, even if we don’t try to start any work. That’s because the work previously scheduled is trying to run as soon as the application starts.

Depending on how the work is scheduled it will also crash the application repeatedly, even if it’s not launched by user/debugger.

To Reproduce

Still under investigation.

One way to trigger the bug is changing the order of the constructor parameters on ListenableWorker. Then it happens at the moment the work is scheduled to start.

For example, in my project (closed source)

THIS worked code A:

class NewsDownloadWorker(
    context: Context,
    private val params: WorkerParameters,
    webService: WebService,
    private val settingsDao: RemoteTabDao,
    appDatabase: AppDatabase,
    workerTracker: WorkerTracker
)

while THIS failed

code B:

class NewsDownloadWorker(
    context: Context,
    webService: WebService,
    private val settingsDao: RemoteTabDao,
    appDatabase: AppDatabase,
    private val params: WorkerParameters,
    workerTracker: WorkerTracker
)

May be related to https://github.com/InsertKoinIO/koin/issues/988. Following the suggestions there, I found out that using named paramers I can work around the issue even when ‘‘code B’’ is used. This is the original dsl declaration:

code 1:

  worker {
            NewsDownloadWorker(
               get(),
               get(),
                 get(),
                 get(),
               get(),
              get()
            )
        } 

this is the workaround I found:

code 2:

  worker {
            NewsDownloadWorker(
                context = get(),
                params = get(),
                webService = get(),
                settingsDao = get(),
                appDatabase = get(),
                workerTracker = get()
            )
        } 

and this is the opposite of the workaround, meaning it will make it always fail: code 3:

  worker {
            NewsDownloadWorker(
                context = get(),
                webService = get(),
                settingsDao = get(),
                appDatabase = get(),
                params = get(),
                workerTracker = get()
            )
        } 

In short:

  • code A+1 works, but B+1 fails

  • code A+2 and B+2 both work (making code 2 a workaround)

  • code A+3 surprisingly fails - constructor in ideal order but dsl in “bad” order

  • code B+3 fails as expected - both constructor and dsl are in “bad” order

The interesting take away is that, no matter how NewsDownloadWorker constructor order, we can make it or break it by the order of parameters inside worker dsl.

Given more time I’ll do some testing in the koin project itself and use it as a unit/integration test.

Koin project used and used version (please complete the following information): koin-androidx-workmanager version 2.2.2

Mitigation Even when the source of this problem is found and solved I’d suggest wrapping the work manager factory code in a try catch to avoid crashing the app.

The main reason is that once the work is scheduled (and depending on the work constraints, such as periodic, internet availability etc) the app may crash immediately when launched, as well as repeated times when it’s not on the foreground.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Alexs784commented, May 23, 2022

I am on Koin 3.1.6 and I am also having the same exact issue mentioned in the original issue. I am now trying to used named parameters (I can’t reproduce the issue, the only way is to ship a new build and look at the crash reporting tool I guess hoping the number goes down 🤞 )

0reactions
wax911commented, Mar 9, 2021

Graph resolution should help you resolve parameters: https://github.com/InsertKoinIO/koin/blob/master/docs/reference/koin-core/injection-parameters.md

Thanks for the heads up, so far this seems to have solved the issue for me atleast, I will do more testing and report back. Also out of context but maybe it’s nothing big, but I was running v2.2.2 (prior 2.2.1) but I got a lot of GlobalContext._koin leaks on modules that scope other modules, so I reverted back to v2.2.1.

Thanks again!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android Work Manager: "Could not instantiate Worker"
This is the most correct answer. Deep inside the WorkManager code, there's an implementation of WorkerFactory interface, which creates a Worker ...
Read more >
WorkManager - Android Developers
Let us know if you discover new issues or have ideas for improving this library. Please take a look at the existing issues...
Read more >
Worker(Context context, WorkerParameters) constructor isn't ...
When using ProGuard, the Worker(Context context, WorkerParameters) constructor isn't ... PlatformImplementationsKt: can't find dynamically referenced class ...
Read more >
Work Manager: Getting Started - CodingWithMitch.com
WorkManager library is a part of Android JetPack and Architecture Components. It is used to run deferrable background task (work). Basically, it is...
Read more >
WorkManager Periodicity - Medium
WorkManager is an Android Jetpack library that makes it easy to schedule ... Worker classes cannot be chained in a PeriodicWorkRequest ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found