fluttercommunity / flutter_workmanager

A Flutter plugin which allows you to execute code in the background on Android and iOS.
853 stars 267 forks source link

Unhandled Exception: PlatformException - WorkManager is not initialized properly #14

Closed mehmetalpsumer closed 5 years ago

mehmetalpsumer commented 5 years ago

Hello, thanks for the package. I am trying to write data to Google's Firestore every x minutes let's say. I created App.kt and set it as android:name. And my main file is as the following:

void callbackDispatcher() {
  Workmanager.executeTask((backgroundTask) {
    Test.writeTestData();
    return Future.value(true);
  });
}

void main() {
  if (Platform.isAndroid) {
    Workmanager.initialize(
      callbackDispatcher, // The top level function, aka Flutter entry point
      isInDebugMode: true,
    );
    Workmanager.registerPeriodicTask("testSensor", "simplePeriodicTask", frequency: Duration(seconds: 10));
  }
  runApp(MyApp());
} 

And the error is:

Unhandled Exception: PlatformException(error, 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., null)

Cheers.

timrijckaert commented 5 years ago

Hi 👋,

This seems like the Android setup was not correct.
Could you post your complete AndroidManifest file for me. I think you might have disabled the automatic registration of WorkManager.

mehmetalpsumer commented 5 years ago

Here is my AndroidManifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.project">

    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application
        android:name=".App"
        android:label="dostum"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- This keeps the window background of the activity showing
                 until Flutter renders its first frame. It can be removed if
                 there is no splash screen (such as the default splash screen
                 defined in @style/LaunchTheme). -->
            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>
timrijckaert commented 5 years ago

Could you try and add tools:replace="android:name" in your application tag? Also just to make sure post your custom App class.

timrijckaert commented 5 years ago

@mehmetalpsumer Were you able to solve it?

mehmetalpsumer commented 5 years ago

Hello, Sorry for the delay, I was off yesterday. Unfortunately my problem is not solved. I will share my files.

My manifest file after adding what you have said:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.myapp.myapp">

    <application
        android:name=".App"
        android:label="myaPP"
        android:icon="@mipmap/ic_launcher"
        tools:replace="android:name">

        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">

            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

My App.kt:

package com.myapp.myapp

import be.tramckrijte.workmanager.WorkmanagerPlugin
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.GeneratedPluginRegistrant

class App : FlutterApplication(), PluginRegistry.PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        WorkmanagerPlugin.setPluginRegistrantCallback(this)
    }

    override fun registerWith(reg: PluginRegistry?) {
        GeneratedPluginRegistrant.registerWith(reg)
    }
}
timrijckaert commented 5 years ago

Did you try and run the sample app on the same phone/emulator? Does it work there?

mehmetalpsumer commented 5 years ago

Do I just install packages and call flutter run for the example project? Do I need extra settings?

If I don't need extra configuration, it returned me this:

* Error running Gradle:
ProcessException: Process "C:\Users\xxx\Desktop\flutter_workmanager-master\example\android\gradlew.bat" exited abnormally:

  Command: C:\Users\xxx\Desktop\flutter_workmanager-master\example\android\gradlew.bat app:properties

Please review your Gradle project setup in the android/ folder.
timrijckaert commented 5 years ago

Checkout the project.
cd example > flutter run

mehmetalpsumer commented 5 years ago

Checkout the project. cd example > flutter run

I can confirm that I am in the example directory. Also I have tried to launch with both my phone and emulator.

timrijckaert commented 5 years ago

What is the error? You should be in the example folder not the android folder within the example folder.

C:\Users\xxx\Desktop\flutter_workmanager-master\example\ instead of C:\Users\xxx\Desktop\flutter_workmanager-master\example\android\

mehmetalpsumer commented 5 years ago

I am in the example directory. This is how my terminal looks like:

PS C:\Users\xxx\Desktop\flutter_workmanager-master\example> flutter run
Using hardware rendering with device Android SDK built for x86. If you get graphics artifacts, consider enabling software rendering with
"--enable-software-rendering".
Launching lib/main.dart on Android SDK built for x86 in debug mode...
Initializing gradle...                                              0,8s
Resolving dependencies...
* Error running Gradle:
ProcessException: Process "C:\Users\xxx\Desktop\flutter_workmanager-master\example\android\gradlew.bat" exited abnormally:

  Command: C:\Users\xxx\Desktop\flutter_workmanager-master\example\android\gradlew.bat app:properties
Please review your Gradle project setup in the android/ folder.
timrijckaert commented 5 years ago

I'm sorry but this has nothing to do with this plugin... Be sure to first investigate this issue first. It seems like you don't even have a working Flutter installation.

Can you run other Flutter projects? How is your flutter doctor -v

mehmetalpsumer commented 5 years ago

I am nearly at the end of the project that I am developing with flutter, so it is sure not to be an issue related to that. But I can understand it is not related to the plugin as well. I will investigate further and try to update here in case I find the solution, thanks for your time and for the plugin @timrijckaert!

mehmetalpsumer commented 5 years ago

For anyone else having the same issue, I fixed my issue by switching from VS Code to Android Studio. I think it might be related to some Kotlin installations Android Studio made for me. Plugin works now, as intended.