androidsyntax-errorandroid-workmanagerconflicting-libraries

Job Scheduler and Work manager conflict when Configuration.Builder() called in MyApplication() Android


So, I declared my Worker Class NotifyWorker and called it in the MainActivity with the below code :

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.util.Log;

import com.birbit.android.jobqueue.JobManager;
import com.birbit.android.jobqueue.config.Configuration;
import com.birbit.android.jobqueue.log.CustomLogger;

public class MainActivity extends AppCompatActivity {

    //some code
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        if(checkForUpdate(this)){
            return;
        }
         //some code

        final OneTimeWorkRequest notificationWork = new OneTimeWorkRequest.Builder(NotifyWorker.class)
                .setInitialDelay(1, TimeUnit.MINUTES)
                .addTag(workTag)
                .build();


        //WorkManager.getInstance(getBaseContext()).beginUniqueWork(workTag, ExistingWorkPolicy.REPLACE, notificationWork);
        WorkManager.getInstance(getApplication()).enqueue(notificationWork);
        Log.v(TAG,"Worker executed");

        //Set content
        setContentView(R.layout.activity_main);
        //some code
}
}

So, I got the following error on running the app:

 Caused by: java.lang.IllegalStateException: WorkManager is not initialized properly.  You have explicitly disabled WorkManagerInitializer in your manifest, have not manually called WorkManager#initialize at this point, and your Application does not implement Configuration.Provider.

So, I followed The developer documentation , added the required code to my manifest, But i am unable to add the following method in myApplication() class :

class MyApplication extends Application implements Configuration.Provider {
    @Override
    public Configuration getWorkManagerConfiguration() {
        return Configuration.Builder()
                .setMinimumLoggingLevel(android.util.Log.INFO)
                .build();
    }
}

My application class looks like below :

public class xxxApplication extends Application implements androidx.work.Configuration.Provider {
    private static xxxApplication instance;
    private JobManager jobManager;
    private BroadcastReceiver broadcastReceiver;
    public xxxApplication(){
        instance = this;
    }

    @Override
    public void onCreate(){
        super.onCreate();
        //ensure jobmanager is configured
        getJobManager();
        broadcastReceiver=new NetworkChangeReceiver();
        IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(broadcastReceiver, intentFilter);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        unregisterReceiver(broadcastReceiver);
    }

    private void configureJobManager(){
        Configuration.Builder builder = new Configuration.Builder(this)
                .customLogger(new CustomLogger() {
                    private static final String TAG = "JOBS";
                    @Override
                    public boolean isDebugEnabled() {
                        return true;
                    }

                    @Override
                    public void d(String text, Object... args) {
                        Log.d(TAG, String.format(text, args));
                    }

                    @Override
                    public void e(Throwable t, String text, Object... args) {
                        Log.e(TAG, String.format(text, args), t);
                    }

                    @Override
                    public void e(String text, Object... args) {
                        Log.e(TAG, String.format(text, args));
                    }

                    @Override
                    public void v(String text, Object... args) {

                    }
                })
                .minConsumerCount(1)
                .maxConsumerCount(3)
                .loadFactor(3)
                .consumerKeepAlive(120);
        //configure here if job scheduler involved
        jobManager = new JobManager(builder.build());

    }
    public synchronized JobManager getJobManager() {
        if (jobManager == null) {
            configureJobManager();
        }
        return jobManager;
    }

    public static xxxApplication getInstance() {
        return instance;
    }

    @Override
    public **Configuration** getWorkManagerConfiguration() {   //I get some conflict here , resolved by using androidx.work.Configuration as a return type
        return  Configuration.Builder()    // Can't resolve the issue ?? what do i write here??
                    .setMinimumLoggingLevel(android.util.Log.INFO)
                    .build();

    }

}

I have commented the part where i get an issue , syntax error says "Method call expected" . How do I resolve this issue?? Why is it not working as expected? Is there a conflict between JobManager and WorkManager configurations?

UPDATE : So,there was actually a conflict between the configuration files of jobmanager and workmanager .Had to call the later using full location definition.


Solution

  • I am answering this myself, in case it is useful for others who have used Job-Schedulers before, and are now adding background processes using WorkManager . While adding the extension as specified in the Developer documentation for custom initialization , use the following code in case you have already used JobScheduler in your application class :

    public class xxxApplication extends Application implements androidx.work.Configuration.Provider {
    //OTHER CODE
    @Override
        public androidx.work.Configuration getWorkManagerConfiguration() {
            return new androidx.work.Configuration.Builder()
                    .setMinimumLoggingLevel(android.util.Log.INFO)
                    .build();
        }
    }
    

    However, it is recommended that you migrate all the processes to WorkManager to avoid further conflicts.