Search code examples
androidandroid-9.0-pieandroid-workmanagerandroid-8.1-oreo

PeriodicWorkRequest is not initiating by WorkManager on realtime devices except emulator


I am enqueuing a PeriodicWorkRequest through WorkManager, the code mentioned below is working on device having Android OS Nougat also on emulators of Android OS versions 8.1, 9 & 10 but not on OnePlus (Android 9), Redmi 5 (Android 8.1) & Google Pixel (Android 9.1).

The dependency I have incorporated is,

implementation "android.arch.work:work-runtime:1.0.1" (Non Androidx)

Also

implementation "android.arch.work:work-runtime:2.1.0-beta02" (Androidx)

Code snippet,

PeriodicWorkRequest.Builder builder = new PeriodicWorkRequest.Builder(MyWorker.class,
                PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS, TimeUnit.MILLISECONDS)
            .addTag(Utils.TAG_WORKER)
            .setInputData(createInputData(config));
WorkManager.getInstance(Context).enqueueUniquePeriodicWork(Utils.TAG_WORKER, ExistingPeriodicWorkPolicy.KEEP, builder.build());

private Data createInputData(Config config) {
    return new Data.Builder()
            .putString(Utils.USER_CONFIG, new Gson().toJson(config))
            .putString(Utils.LOCATION_CONFIG, new Gson().toJson(Preferences.getInstance(fragmentActivity).getConfiguration()))
            .build();
}

I have tried and searched a lot, any help regarding will be much appreciated.

Sample Implementation: https://db.tt/gFEJi39Ofz

Google Issue Tracker link: https://issuetracker.google.com/issues/135865377


Solution

  • After so many tries, I have created an issue on Google Issue Tracker under component, also submitted the code sample to the team and they replied as:

    Your Worker is package protected, and hence we cannot instantiate it using the default WorkerFactory.

    If you looked at Logcat, you would see something like:

    2019-06-24 10:49:18.501 14687-14786/com.example.workmanager.periodicworksample E/WM-WorkerFactory: Could not instantiate com.example.workmanager.periodicworksample.MyWorker
        java.lang.IllegalAccessException: java.lang.Class<com.example.workmanager.periodicworksample.MyWorker> is not accessible from java.lang.Class<androidx.work.WorkerFactory>
            at java.lang.reflect.Constructor.newInstance0(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
            at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:97)
            at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:228)
            at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:127)
            at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:75)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
            at java.lang.Thread.run(Thread.java:764)
    2019-06-24 10:49:18.501 14687-14786/com.example.workmanager.periodicworksample E/WM-WorkerWrapper: Could not create Worker com.example.workmanager.periodicworksample.MyWorker
    

    Your Worker needs to be public

    And by making My Worker class public I got resolved the issue.

    Reference of Google's Reply on the issue: https://issuetracker.google.com/issues/135865377#comment4