question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[expo-background-fetch] App has stopped when is killed and background task try to execute

See original GitHub issue

🐛 Bug Report

Summary of Issue

I’m having the AppName has stopped alert when use expo-background-fetch AND I have Proguard enabled. The issue is similar but not is the same that was reported. All works great if Proguard is disabled, but when I enable Proguard the app crash in background.

This happen in Bare workflow, I’m not tested in Managed workflow. Only Android.

Related Issues

Another related issues to background-location, but I have this with expo-background-fetch and only when Proguard is enabled.

Logcat

2020-09-30 07:53:43.632 5371-5371/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: xxx, PID: 5371
    java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke interface method 'void l.b.a.b.a(android.content.Context, l.b.a.b$b, java.lang.Runnable, l.b.b.l.e)' on a null object reference
        at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:112)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void l.b.a.b.a(android.content.Context, l.b.a.b$b, java.lang.Runnable, l.b.b.l.e)' on a null object reference
        at expo.modules.taskManager.h.a(Unknown Source:125)
        at expo.modules.taskManager.c.a(Unknown Source:2)
        at expo.modules.backgroundfetch.BackgroundFetchTaskConsumer.didExecuteJob(Unknown Source:8)
        at expo.modules.taskManager.h.b(Unknown Source:64)
        at expo.modules.taskManager.TaskJobService.onStartJob(Unknown Source:9)
        at android.app.job.JobService$1.onStartJob(JobService.java:71)
        at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:108)

Environment - output of expo diagnostics & the platform(s) you’re targeting

  Expo CLI 3.27.12 environment info:
    System:
      OS: macOS 10.15.5
      Shell: 5.7.1 - /bin/zsh
    Binaries:
      Node: 14.4.0 - /usr/local/bin/node
      Yarn: 1.19.1 - /usr/local/bin/yarn
      npm: 6.14.5 - /usr/local/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    Managers:
      CocoaPods: 1.9.3 - /usr/local/bin/pod
    SDKs:
      iOS SDK:
        Platforms: iOS 14.0, DriverKit 19.0, macOS 10.15, tvOS 14.0, watchOS 7.0
      Android SDK:
        Android NDK: 21.3.6528147
    IDEs:
      Android Studio: 4.0 AI-193.6911.18.40.6514223
      Xcode: 12.0/12A7209 - /usr/bin/xcodebuild
    npmPackages:
      expo: ~39.0.3 => 39.0.3
      react: 16.13.1 => 16.13.1
      react-native: ~0.63.3 => 0.63.3
    npmGlobalPackages:
      expo-cli: 3.27.12
    Expo Workflow: bare
expo-background-fetch: 8.5.0
expo-task-manager: 8.5.0

Reproducible Demo

Steps to Reproduce

  1. Create a Bare project
  2. Install expo-background-fetch
  3. Create a task to run in background
  4. Open Android Studio and enable Proguard (def enableProguardInReleaseBuilds = true)
  5. Create a release sdk
  6. Install in your device, open the app and kill the app.
  7. Wait a few minutes to see the AppName has stopped

Expected Behavior vs Actual Behavior

Expected Behavior: background tasks works great Actual Behavior: Error AppName has stopped

I tested with the following proguard rule but doesn’t work.

-keep class expo.modules.taskManager { *; }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
RodolfoGScommented, Oct 2, 2020

Hi @byCedric, I created a reproducible demo: https://github.com/RodolfoGS/expo-background-fetch-isseu10475

How to reproduce.

  1. Create a release APK from demo
  2. Install APK on your device
  3. KILL THE APP (too many important) and wait for 1 minute
  4. You’ll see the TestBackgroundFetch has stopped alert

How I created this demo:

  1. expo init TestBackgroundFetch
  2. Select Bare workflow -> minimal
  3. cd TestBackgroundFetch
  4. expo install expo-background-fetch
  5. expo install expo-task-manager
  6. Add following code to App.js:
import * as TaskManager from 'expo-task-manager';
import * as BackgroundFetch from 'expo-background-fetch';

TaskManager.defineTask('demo-task', async () => {
  console.log('demo-task is running');
  return BackgroundFetch.Result.NewData;
});

BackgroundFetch.registerTaskAsync('demo-task', {
  minimumInterval: 60, // 1 min
  stopOnTerminate: false,
  startOnBoot: true,
})
  .then(() => alert('BackgroundFetch.registerTaskAsync success'))
  .catch(error => alert(`Error registerTaskAsync: ${error.message}`));
  1. Enable proguard editing android/app/build.gradle: def enableProguardInReleaseBuilds = true
  2. Add the following rule to proguard-rules.pro file to fix another bug that are described below
-keep class expo.modules.backgroundfetch.** { *; }
  1. Create release apk (Android Studio: Build -> Generate Signed Bundle / APK…)
  2. Install in your device
  3. Open the app
  4. KILL THE APP (too many important)
  5. Wait 1 minute and you will see the TestBackgroundFetch has stopped alert

Additional to this, I found another bug when you run BackgroundFetch.registerTaskAsync.

Initializing task consumer 'e.a.a.c' failed. Inherited error: <init> [class android.content.Context, interface i.b.c.f.f]"

I fixed this bug adding this rule to proguard rules file https://github.com/RodolfoGS/expo-background-fetch-isseu10475/blob/master/android/app/proguard-rules.pro#L12

-keep class expo.modules.backgroundfetch.** { *; }

Tested on Android Nexus 6P - Android 8.1.0

1reaction
lukmccallcommented, Oct 2, 2020

@byCedric, I don’t think that my PR fixed this bug. However, we now can add DoNotStrip annotation to BackgrondFetchTaskConsumer… and this should fix the problem with BackgroundFetch.registerTaskAsync.

Unfortunately, it won’t fix the problem with the AppName has stopped alert.

Frankly speaking, I really don’t understand why the BackgrondFetchTaskConsumer is dropped here 🤷🏼‍♂️ I’m not familiar with the task manager code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expo : Background and Closed Application - Stack Overflow
To run a function in background in Expo is TaskManager , but work only if app is in FOREGROUND. How to run a...
Read more >
Expo TaskManager crash when killing app. : r/reactnative
Expo TaskManager crash when killing app. I am running a background task to track locations with Location.startLocationUpdatesAsync().
Read more >
react-native-background-fetch - npm
Background Fetch is a very simple plugin which attempts to awaken an app in the background about every 15 minutes, providing a short...
Read more >
How to handle Background App Refresh with HealthKit in ...
Triggering background fetch event while app is running is not enough. In practice our apps should be wake up from terminated state due...
Read more >
iOS background processing - Background App Refresh Task
Performance: ensure background execution does not have any negative effect on active usage. Privacy: Users should be aware of background tasks ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found