Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Attempt to read from field 'java.lang.String io.flutter.embedding.engine.loader.FlutterApplicationInfo.flutterAssetsDir' on a null object reference #160

Closed
KorbinianMossandl opened this issue Oct 19, 2020 · 47 comments

Comments

@KorbinianMossandl
Copy link

KorbinianMossandl commented Oct 19, 2020

Your Environment

  • Plugin version: 0.6.0
  • Platform: Android
  • OS version: 10.0
  • Device manufacturer / model: any
  • Flutter info (flutter info, flutter doctor):
[✓] Flutter (Channel master, 1.23.0-19.0.pre.95, on Linux, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Android Studio (version 4.0)
[✓] Connected device (1 available)
  • Plugin config
BackgroundFetchConfig(
          minimumFetchInterval: 30,
          stopOnTerminate: false,
          enableHeadless: true,
          startOnBoot: true,
          requiredNetworkType: NetworkType.ANY,
        )

To Reproduce
Steps to reproduce the behavior:

  1. register headless task with flutter version >=1.22.0
  2. close app
  3. trigger headless task

Debug logs

2020-10-19 14:26:10.786 6164-6164/com.test.app.demo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.test.app.demo, PID: 6164
    java.lang.NullPointerException: Attempt to read from field 'java.lang.String io.flutter.embedding.engine.loader.FlutterApplicationInfo.flutterAssetsDir' on a null object reference
        at io.flutter.embedding.engine.loader.FlutterLoader.findAppBundlePath(FlutterLoader.java:324)
        at io.flutter.view.FlutterMain.findAppBundlePath(FlutterMain.java:98)
        at com.transistorsoft.flutter.backgroundfetch.HeadlessTask.startBackgroundIsolate(HeadlessTask.java:128)
        at com.transistorsoft.flutter.backgroundfetch.HeadlessTask.dispatch(HeadlessTask.java:101)
        at com.transistorsoft.flutter.backgroundfetch.HeadlessTask.run(HeadlessTask.java:95)
        at android.os.Handler.handleCallback(Handler.java:907)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7506)
        at java.lang.reflect.Method.invoke(Native Method)
    	at com.android.internal.os.RuntimeInit$MethodAndArgsCal

Additional context
At first glance when you search via google this seems to be a bug in flutter itself
flutter/flutter#64458
but the fix is already on master and as you can see, i still run into this issue.
everything works fine as long as the app is still running and a backround task is triggered. only the headless one crashes with above error.

@kanczler
Copy link

kanczler commented Oct 21, 2020

I'm having the same issue. On my own device (Huawei P30 Pro), there are no problems, headless tasks run just fine, but on an android 7.1.1 emulator and several other devices whenever the headless task is triggered, the application crashes with the exact same exception.

@christocracy
Copy link
Member

@kanczler flutter doctor

@kanczler
Copy link

My apologies!

flutter doctor:

[✓] Flutter (Channel stable, 1.22.2, on Mac OS X 10.15.7 19H2, locale en-GB)  
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)  
[✓] Xcode - develop for iOS and macOS (Xcode 12.1)  
[✓] Android Studio (version 4.1)  
[✓] IntelliJ IDEA Ultimate Edition (version 2020.2.3)  
[✓] VS Code (version 1.50.0)  
[✓] Connected device (1 available)  

@christocracy
Copy link
Member

I'm having the same issue

Post the exact stacktrace

@kanczler
Copy link

Stack trace:

2020-10-21 15:49:48.261 32397-32397/hu.bravobased E/AndroidRuntime: FATAL EXCEPTION: main
    Process: hu.bravobased, PID: 32397
    java.lang.NullPointerException: Attempt to read from field 'java.lang.String io.flutter.embedding.engine.loader.FlutterApplicationInfo.flutterAssetsDir' on a null object reference
        at io.flutter.embedding.engine.loader.FlutterLoader.findAppBundlePath(FlutterLoader.java:324)
        at io.flutter.view.FlutterMain.findAppBundlePath(FlutterMain.java:97)
        at com.transistorsoft.flutter.backgroundfetch.HeadlessTask.startBackgroundIsolate(HeadlessTask.java:128)
        at com.transistorsoft.flutter.backgroundfetch.HeadlessTask.dispatch(HeadlessTask.java:101)
        at com.transistorsoft.flutter.backgroundfetch.HeadlessTask.run(HeadlessTask.java:95)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

@christocracy
Copy link
Member

Boot the /example in this repo.

@christocracy
Copy link
Member

I have just used flutter create foo to create a fresh new Hello World app. with flutter doctor:

[✓] Flutter (Channel stable, 1.22.2, on Mac OS X 10.15.6 19G2021, locale en-CA)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 12.0.1)
[✓] Android Studio (version 4.0)
[✓] VS Code (version 1.45.1)
[✓] Connected device (3 available)

I cannot reproduce this. How old is your app? You might have old code in your app from a previously generated flutter app that hasn't been properly upgraded.

@KorbinianMossandl
Copy link
Author

yes, our app is about 1 year old, be we regularly maintain it, especially updates to flutter and the project structure.
the problem has only recently started with our latest release which made a switch from flutter 1.12 to 1.22.0.
Also as @kanczler mentioned it does not happen on every device.
Any ideas what this could be causing and why it only happens with flutter 1.22.0+ are greatly appreciated.
Will play around with the example project in the mean time and see if i can repdroduce.

@christocracy
Copy link
Member

If your app was generated quite a long time ago, I suggest using flutter create to generate a fresh new app and migrate your /lib code into the new app and migrate your dependencies into the fresh pubspec.yaml.

@KorbinianMossandl
Copy link
Author

not an option. we made so many customizations to the project structure for flavors and such. it would take forever to get everything to this point again.

@christocracy
Copy link
Member

christocracy commented Oct 21, 2020

Then I suggest you flutter create foo and observe the difference between key android files, such as:

  • android/build.gradle
  • android/settings.gradle
  • android/app/build.gradle

You will want to migrate changes into your app.

Are you using a custom Application.java / Application.kt? If so, post it.

@christocracy
Copy link
Member

we made so many customizations to the project structure for flavors and such

This is not hard to migrate.

@KorbinianMossandl
Copy link
Author

KorbinianMossandl commented Oct 21, 2020

Then I suggest you flutter create foo and observe the difference between key android files, such as:

  • android/build.gradle
  • android/settings.gradle
  • android/app/build.gradle

You will want to migrate changes into your app.

Are you using a custom Application.java / Application.kt? If so, post it.

we don't use a custom application.kt
but i will try to spot any differences between a fresh project and ours. that is the best lead so far.
will update with the results.

@christocracy
Copy link
Member

I made a mistake in the files to check. Corrected above (ie: android/app/build.gradle)

@KorbinianMossandl
Copy link
Author

ok, i looked at all the files, made changes so everything is exactly like in a newly created project, but the issue still exists.

but what i did is take our current project and move back to flutter 1.20.0 and the issue is completely gone.
so something in flutter 1.22.x broke/changed something that is causing the issues with background_fetch headless tasks.

it would be greatly appreciated if you could take another look.

@christocracy
Copy link
Member

I have just created a fresh new Hello World app flutter create foo.

The app boots fine and simulated fetch events respond when app is running and headless.

$ flutter doctor
[✓] Flutter (Channel stable, 1.22.2, on Mac OS X 10.15.6 19G2021, locale en-CA)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 12.1)
[✓] Android Studio (version 4.0)
[✓] VS Code (version 1.45.1)
[✓] Connected device (3 available)

@KorbinianMossandl
Copy link
Author

i did the same and i know that it is working if you do it like this. but maybe flavors are the problem? All i can tell you is that the exact same code works flawless with flutter 1.20.0 but crashes with flutter 1.22.2 and that my android project looks like a newly created project just with all the necessary adaptions we need to make.

@christocracy
Copy link
Member

The last reference to background_fetch in your stacktrace is HeadlessTask.java:128

at io.flutter.embedding.engine.loader.FlutterLoader.findAppBundlePath(FlutterLoader.java:324)
        at io.flutter.view.FlutterMain.findAppBundlePath(FlutterMain.java:97)
        at com.transistorsoft.flutter.backgroundfetch.HeadlessTask.startBackgroundIsolate(HeadlessTask.java:128

@KorbinianMossandl
Copy link
Author

yes, as i said. only headless crashes. non headless works perfectly fine

@KorbinianMossandl
Copy link
Author

i will try to reduce our project to a minimal example and share it with you, if that is ok?

@christocracy
Copy link
Member

christocracy commented Oct 22, 2020

It seems this whole FlutterMain class has been marked @deprecated.

The docs say "Replaced by FlutterLoader":

Ok, try this.

  • In Android Studio, open your_flutter_app/android
  • Edit HeadlessTask.java in background_fetch module.
  • Modify line 128 to:
String appBundlePath =  FlutterInjector.instance().flutterLoader().findAppBundlePath();

@KorbinianMossandl
Copy link
Author

then i get this error when trying to run:

flutter/.pub-cache/hosted/pub.dartlang.org/background_fetch-0.6.0/android/src/main/java/com/transistorsoft/flutter/backgroundfetch/HeadlessTask.java:128: error: cannot find symbol
        String appBundlePath =  FlutterInjector.instance().flutterLoader().findAppBundlePath();
                                ^
  symbol:   variable FlutterInjector
  location: class HeadlessTask
1 error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':background_fetch:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 14s
Exception: Gradle task assembleStagingDebug failed with exit code 1

@KorbinianMossandl
Copy link
Author

missing import?

@christocracy
Copy link
Member

christocracy commented Oct 22, 2020

Did you open your /android app in Android Studio?

When you add that line, the IDE will prompt you to add the required import io.flutter.FlutterInjector. On a mac, pressing [option]+[enter] will automatically add the required import.

Add the following import to the top of HeadlessTask.java:

import io.flutter.FlutterInjector;

@KorbinianMossandl
Copy link
Author

still the same error :-/

@christocracy
Copy link
Member

I don’t know. You must be doing it wrong.

Are you operating upon the /android app in Android Studio (not your flutter app)?

@KorbinianMossandl
Copy link
Author

image

@KorbinianMossandl
Copy link
Author

KorbinianMossandl commented Oct 22, 2020

with same error i meant that i get the same error when i run a headlessTask

the compile worked
sorry if that was not clear.

@KorbinianMossandl
Copy link
Author

i think falvors are the problem. i stripped everything from our project except flavors and use your example code for the main()
and still get the error for headless tasks. i need to to take another look at the project later on, just to make sure i stripped anything confidential. after that i will share with you, if that is ok?

@christocracy
Copy link
Member

and use your example code for the main() and still get the error for headless tasks

Dart code is not responsible for your problem.

after that i will share with you, if that is ok?

That's fine.

@KorbinianMossandl
Copy link
Author

@christocracy ok, after some more tinkering i found the solution:
for what ever reasons with flutter 1.22.x you need an Application.kt/java for headless to work. it can just be a basic file with

package com.package.name

import io.flutter.app.FlutterApplication

class Application : FlutterApplication() {
    override fun onCreate() {
        super.onCreate()
    }
}

and the corresponding line
android:name=".Application"
in the manifest.

without that it will not work even though it should not be needed and was not needed when running flutter 1.20.x

@christocracy
Copy link
Member

And before you provided an override, your AndroidManifest contained this?

<application
        android:name="io.flutter.app.FlutterApplication"

@KorbinianMossandl
Copy link
Author

KorbinianMossandl commented Oct 27, 2020

that seems to work as well. before we had no android:name under <application/> because according to the migration guide (https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects) it is not needed and should be removed.
but it seems it needs to be there for headless background fetch to work with flutter 1.22.x

@christocracy
Copy link
Member

Generate a fresh new flutter create foo. Observe the AndroidManifest.xml.

This is what your application element must look like when no override is provided:

<application
        android:name="io.flutter.app.FlutterApplication"

@KorbinianMossandl
Copy link
Author

@christocracy
this is directly from the flutter upgrade guide:
image

Since you link to it in your own setup guide and say

If you've upgraded your Flutter SDK to 1.12 (or higher) AND Upgraded Your Android Project, there are no additional steps required — everything is now automatic.

you might want to add the info above.

@ghost
Copy link

ghost commented Nov 19, 2020

@christocracy ok, after some more tinkering i found the solution:
for what ever reasons with flutter 1.22.x you need an Application.kt/java for headless to work. it can just be a basic file with

package com.package.name

import io.flutter.app.FlutterApplication

class Application : FlutterApplication() {
    override fun onCreate() {
        super.onCreate()
    }
}

and the corresponding line
android:name=".Application"
in the manifest.

without that it will not work even though it should not be needed and was not needed when running flutter 1.20.x

This is solve my problem.
Actually its not only in background_fetch its also happen in flutter_alarm_manager, the same issue is happening.

@KorbinianMossandl
Copy link
Author

ok, so it seems to be a framework issue.
@christocracy i think you really should update the setup guide for Android and mention this workaround. if one follows your setup guide 1:1 he is bound to run into this issue.

@apaolino
Copy link

apaolino commented Nov 26, 2020

Had same problem and the provided solution resolved (Flutter 1.22.4).
I'm using embedding v2

@darek-dots
Copy link

darek-dots commented Dec 28, 2020

In HeadlessTask.java I have changed:

       String appBundlePath = FlutterMain.findAppBundlePath(); 

to

      FlutterInjector.instance().flutterLoader().startInitialization(mContext);
      String appBundlePath = FlutterInjector.instance().flutterLoader().findAppBundlePath();

and it solved the problem of
java.lang.NullPointerException: Attempt to read from field 'java.lang.String io.flutter.embedding.engine.loader.FlutterApplicationInfo.flutterAssetsDir' on a null object reference at io.flutter.embedding.engine.loader.FlutterLoader.findAppBundlePath(FlutterLoader.java:324) at io.flutter.view.FlutterMain.findAppBundlePath(FlutterMain.java:98)

My AndroidManifest.xml does not have 'android:name' , as described in flutter upgrade guide.

...
According to https://api.flutter.dev/javadoc/io/flutter/embedding/engine/loader/FlutterLoader.html#startInitialization-android.content.Context-

Calling this method multiple times has no effect.

so it should be safe.

@vishnukvmd
Copy link

@christocracy
this is directly from the flutter upgrade guide:
image

Not following this step during the migration fixed the issue with headless tasks.

@saeid-pm
Copy link

@christocracy ok, after some more tinkering i found the solution:
for what ever reasons with flutter 1.22.x you need an Application.kt/java for headless to work. it can just be a basic file with

package com.package.name

import io.flutter.app.FlutterApplication

class Application : FlutterApplication() {
    override fun onCreate() {
        super.onCreate()
    }
}

and the corresponding line
android:name=".Application"
in the manifest.

without that it will not work even though it should not be needed and was not needed when running flutter 1.20.x

I had the same problem with flutter_alarm_manager when app was killed and flutter_alarm_manager wanted to fire an alarm, this solved the problem.

@rifaia
Copy link

rifaia commented Jan 21, 2021

Generate a fresh new flutter create foo. Observe the AndroidManifest.xml.

This is what your application element must look like when no override is provided:

<application
        android:name="io.flutter.app.FlutterApplication"

This works on "Flutter 1.26.0-1.0.pre" for me.
Just included android:name="io.flutter.app.FlutterApplication" as @christocracy mentioned. Don't need any other changes

@voivoed
Copy link

voivoed commented Jan 25, 2021

This works on "Flutter 1.26.0-1.0.pre" for me.
Just included android:name="io.flutter.app.FlutterApplication" as @christocracy mentioned. Don't need any other changes

Can confirm that this worked for me also, using Flutter 1.25.0-8.3.pre.

@vanlooverenkoen
Copy link

I am still having this issue with flutter 3.3.0 Anybody an idea why the flutterapplication is still needed. while the old migration page said to remove it?

@christocracy
Copy link
Member

Where in the Setup or API docs are you directed to use FlutterApplication?

@khanhtruongq
Copy link

please help, we got the same issue on 3.3.0

@christocracy
Copy link
Member

christocracy commented Sep 8, 2022

I just booted the /example app in this repo. I have see no issues.

I suggest you implement the plugin in a fresh new hello_world app and familiarize yourself with the plugin Setup in a simple test-case, isolated from your application code.

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.0, on macOS 12.5.1 21G83 darwin-x64, locale en-CA)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.2)
[✓] HTTP Host Availability
09-08 09:23:29.526 10718 10718 D TSBackgroundFetch: - configure
09-08 09:23:29.526 10718 10718 D TSBackgroundFetch: - start
09-08 09:23:29.527 10718 10718 D TSBackgroundFetch: - registerTask: flutter_background_fetch
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch: {
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch:   "taskId": "flutter_background_fetch",
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch:   "isFetchTask": true,
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch:   "minimumFetchInterval": 15,
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch:   "stopOnTerminate": false,
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch:   "requiredNetworkType": 0,
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch:   "requiresBatteryNotLow": false,
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch:   "requiresCharging": false,
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch:   "requiresDeviceIdle": false,
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch:   "requiresStorageNotLow": false,
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch:   "startOnBoot": true,
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch:   "jobService": "com.transistorsoft.flutter.backgroundfetch.HeadlessTask",
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch:   "forceAlarmManager": false,
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch:   "periodic": true,
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch:   "delay": -1
09-08 09:23:29.529 10718 10718 D TSBackgroundFetch: }
.
09-08 09:23:29.539 10718 10754 I flutter : [BackgroundFetch] configure success: 2
.
09-08 09:23:29.547 10718 10718 D TSBackgroundFetch: - registerTask: com.transistorsoft.customtask
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch: {
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch:   "taskId": "com.transistorsoft.customtask",
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch:   "isFetchTask": false,
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch:   "minimumFetchInterval": 15,
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch:   "stopOnTerminate": false,
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch:   "requiredNetworkType": 0,
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch:   "requiresBatteryNotLow": false,
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch:   "requiresCharging": false,
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch:   "requiresDeviceIdle": false,
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch:   "requiresStorageNotLow": false,
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch:   "startOnBoot": false,
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch:   "jobService": "com.transistorsoft.flutter.backgroundfetch.HeadlessTask",
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch:   "forceAlarmManager": true,
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch:   "periodic": false,
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch:   "delay": 10000
09-08 09:23:29.548 10718 10718 D TSBackgroundFetch: }
09-08 09:23:37.647 10718 10718 D TSBackgroundFetch: - Background Fetch event received: flutter_background_fetch
.
09-08 09:23:37.659 10718 10754 I flutter : [BackgroundFetch] Event received: flutter_background_fetch
09-08 09:23:39.338 10718 10754 I flutter : Number of books about http: 3062.
.
09-08 09:23:39.339 10718 10718 D TSBackgroundFetch: - finish: flutter_background_fetch
09-08 09:23:39.339 10718 10718 D TSBackgroundFetch: - jobFinished
09-08 09:23:39.567 10718 10718 D TSBackgroundFetch: - Background Fetch event received: com.transistorsoft.customtask
.
09-08 09:23:39.572 10718 10754 I flutter : [BackgroundFetch] Event received: com.transistorsoft.customtask
.
09-08 09:23:39.574 10718 10718 D TSBackgroundFetch: - finish: com.transistorsoft.customtask
09-08 09:23:39.575 10718 10718 D TSBackgroundFetch: - FetchAlarmReceiver finish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests