WorkManager 1.0.0/1.0.1 中的 NoSuchMethodException

NoSuchMethodException in WorkManager 1.0.0/1.0.1

我在应用程序构建 gradle 中实现了 'android.arch.work:work-runtime:1.0.0(or 1.0.1)',但总是出现此异常并且我的代码不是 运行。任何人都可以告诉我这是什么问题吗,thx.

public class SplashActivity extends CommonActivity {

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        OneTimeWorkRequest request = new OneTimeWorkRequest.Builder(UploadWorker.class)
                .setInitialDelay(1, TimeUnit.SECONDS)
                .addTag("UploadWorker")
                .build();
        WorkManager.getInstance().beginWith(request).enqueue();

        WorkManager.getInstance().getWorkInfoByIdLiveData(request.getId()).observe(this, new Observer<WorkInfo>() {
            @Override
            public void onChanged(@Nullable WorkInfo workInfo) {
                Log.i("Work", "onChanged, workInfo = " + workInfo);
            }
        });
    }

    public class UploadWorker extends Worker {

        public UploadWorker(@NonNull Context context, @NonNull WorkerParameters params) {
            super(context, params);
            Log.i("Work", "UploadWorker");
        }

        @Override
        public Result doWork() {
            // Do the work here--in this case, upload the images.
            //uploadImages()

            Log.i("Work", "doWork");
            int x = 1 / 0; //an intended crash to test

            // Indicate whether the task finished successfully with the Result
            return Result.success();
        }
    }
public class App extends Application {
    private static final String TAG = "App";

    public static boolean debug = false;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
        SplitCompat.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        initWorkManager();
    }

    private void initWorkManager() {
        Configuration configuration = new Configuration.Builder().setMinimumLoggingLevel(Log.VERBOSE).build();
        WorkManager.initialize(getApplicationContext(), configuration);
    }
2019-11-15 16:15:10.103 27635-27690/com.package.name E/WM-WorkerFactory: Could not instantiate com.package.name.SplashActivity$UploadWorker
2019-11-15 16:15:10.103 27635-27690/com.package.name E/WM-WorkerFactory: Could not instantiate com.package.name.SplashActivity$UploadWorker
    java.lang.NoSuchMethodException: <init> [class android.content.Context, class androidx.work.WorkerParameters]
        at java.lang.Class.getConstructor0(Class.java:2327)
        at java.lang.Class.getDeclaredConstructor(Class.java:2166)
        at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:91)
        at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:233)
        at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:127)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)

更新:

AndroidManifest.xml

        <!-- https://github.com/android/architecture-components-samples/blob/master/WorkManagerSample/app/src/main/AndroidManifest.xml -->
        <provider
            android:name="androidx.work.impl.WorkManagerInitializer"
            android:authorities="${applicationId}.workmanager-init"
            android:directBootAware="false"
            android:exported="false"
            android:multiprocess="true"
            tools:node="remove"
            tools:targetApi="n" />

proguard-rules.pro

# 
 -keepclassmembers class * extends androidx.work.Worker {
     public <init>(android.content.Context,androidx.work.WorkerParameters);
 }

已解决!你的 Worker class 必须是独立的 class(.java),虽然有点奇怪。