nativescript-background-fetch icon indicating copy to clipboard operation
nativescript-background-fetch copied to clipboard

Background Fetch Headless Class Not Found Exception

Open ssulei7 opened this issue 7 years ago • 8 comments

When I try to enable headless mode on Android 8.1 / Nexus 6P, I get an error java.lang.ClassNotFoundException. We are running the latest version of NativeScript and the latest available version of your plugin. Not sure if relevant, but we're also using your background geolocation plugin.

BackgroundFetch.registerHeadlessTask(async () => {
	console.log('[My BackgroundFetch HeadlessTask] onFetch');    
	// Do some asynchronous work (eg: HTTP Request)
	
	// Politely signal to the OS that our processing is complete.
	BackgroundFetch.finish();
});

// Configure Background Fetch
BackgroundFetch.configure({
	stopOnTerminate: false,
	startOnBoot: true,
	enableHeadless: true
}, () => {
	console.log("[js] BackgroundFetch event received");
	// do stuff
	console.log("[js] BackgroundFetch event completed");
}, (status) => {
	console.log('BackgroundFetch not supported by your OS', status);
});

This is the log of the error:

System.err: java.lang.ClassNotFoundException: com.transistorsoft.backgroundfetch.HeadlessJobService
System.err:     at java.lang.Class.classForName(Native Method)
System.err:     at java.lang.Class.forName(Class.java:453)
System.err:     at java.lang.Class.forName(Class.java:378)
System.err:     at com.transistorsoft.tsbackgroundfetch.BackgroundFetch.onFetch(BackgroundFetch.java:176)
System.err:     at com.transistorsoft.tsbackgroundfetch.BackgroundFetch.onFetch(BackgroundFetch.java:151)
System.err:     at com.transistorsoft.tsbackgroundfetch.FetchJobService.onStartJob(FetchJobService.java:22)
System.err:     at android.app.job.JobService$1.onStartJob(JobService.java:71)
System.err:     at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:108)
System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
System.err:     at android.os.Looper.loop(Looper.java:164)
System.err:     at android.app.ActivityThread.main(ActivityThread.java:6494)
System.err:     at java.lang.reflect.Method.invoke(Native Method)
System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
System.err: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.transistorsoft.backgroundfetch.HeadlessJobService" on path: DexPathList[[zip file "/data/app/com.test.app-hFj8-9wq6p-ke3BOsoW15Q==/base.apk"],nativeLibraryDirectories=[/data/app/com.test.app-hFj8-9wq6p-ke3BOsoW15Q==/lib/arm, /data/app/com.test.app-hFj8-9wq6p-ke3BOsoW15Q==/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
System.err:     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
System.err:     ... 14 more

Any ideas? A full re-build didn't help.

ssulei7 avatar Sep 20 '18 19:09 ssulei7

Ensure you’re using latest background-fetch

christocracy avatar Sep 20 '18 19:09 christocracy

I'm using version 1.20 of the plugin, and I'm still receiving the Class Not Found exception.

Not sure if relevant, but our background geolocation config is below - in case there's some sort of conflict with this plugin?

        await BackgroundGeolocation.clearDatabase();
        await BackgroundGeolocation.ready({
            ...baseCfg,

            reset: true,

            // enable background tracking
            stopOnTerminate: false,

            // HTTP
            url: `${this.baseCloudFunc}/updateUserLocation`,
            httpRootProperty: 'data',
            locationTemplate: '{ "lat": <%= latitude %>, "lon": <%= longitude %> }',
            geofenceTemplate: '{ "lat": <%= latitude %>, "lon": <%= longitude %>, "geofenceId": "<%= geofence.identifier %>", "geofenceAction": "<%= geofence.action %>" }',
            autoSync: true,

            // battery opts
            disableMotionActivityUpdates: true // disable for all but select group
        });

ssulei7 avatar Sep 21 '18 15:09 ssulei7

christocracy avatar Sep 21 '18 20:09 christocracy

I'm getting this same issue, even after installing the latest version and doing the above. It seems using Background Geolocation plugin and this plugin at the same time (since the former also uses background fetch?) causes issues?

neil-119 avatar Oct 20 '18 15:10 neil-119

I'll come up with a workaround for this no problem

neil-119 avatar Oct 20 '18 15:10 neil-119

@neil-119 They're designed to work together. Background Geolocation uses background-fetch internally for its own purposes.

christocracy avatar Oct 20 '18 17:10 christocracy

This issue appears on SDK version 19 for me. Any updates on solution?

System.err: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.transistorsoft.backgroundfetch.HeadlessJobService" on path: DexPathList[[zip file "/data/app/app.loco-1.apk", zip file "/data/data/app.loco/code_cache/secondary-dexes/app.loco-1.apk.classes2.zip", zip file "/data/data/app.loco/code_cache/secondary-dexes/app.loco-1.apk.classes3.zip"],nativeLibraryDirectories=[/data/app-lib/app.loco-1, /vendor/lib, /system/lib]]

FranciZ avatar Feb 02 '19 17:02 FranciZ

@FranciZ I'm getting the same crash reports on google play for some phones (eg Android 4.3). Probably because of the targetApi(21) https://github.com/transistorsoft/transistor-background-fetch/blob/master/android/tsbackgroundfetch/src/main/java/com/transistorsoft/tsbackgroundfetch/FetchJobService.java#L12

For me the background service is not a mandatory feature, but a desired one. So I'll try disabling the feature on <21 sdk versions using device.sdkVersion

Also might be useful looking it this answer: https://stackoverflow.com/a/58427864

svilenkov avatar Mar 24 '20 14:03 svilenkov