Enabling a dummy HttpClientModule interceptor causes crash (Error: Trying to link invalid 'this' to a Java object)


#1

Using NativeScriptHttpClientModule/HttpClientModule works fine, but enabling a dummy HTTP interceptor would crash:

Refreshing application...
JS: Angular is running in the development mode. Call enableProdMode() to enable the production mode.
System.err: java.lang.RuntimeException: Unable to resume activity {org.nativescript.httpinterceptor/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: 
System.err: Calling js method onActivityResumed failed
System.err: 
System.err: Error: Trying to link invalid 'this' to a Java object
System.err: File: "file:///data/data/org.nativescript.httpinterceptor/files/app/tns_modules/tns-core-modules/ui/frame/fragment.transitions.js, line: 237, column: 35
System.err: 
System.err: StackTrace: 
System.err:     Frame: function:'AnimationListnerImpl', file:'file:///data/data/org.nativescript.httpinterceptor/files/app/tns_modules/tns-core-modules/ui/frame/fragment.transitions.js', line: 237, column: 36
System.err:     Frame: function:'getAnimationListener', file:'file:///data/data/org.nativescript.httpinterceptor/files/app/tns_modules/tns-core-modules/ui/frame/fragment.transitions.js', line: 268, column: 29
System.err:     Frame: function:'setupExitAndPopEnterAnimation', file:'file:///data/data/org.nativescript.httpinterceptor/files/app/tns_modules/tns-core-modules/ui/frame/fragment.transitions.js', line: 474, column: 20
System.err:     Frame: function:'setupAllAnimation', file:'file:///data/data/org.nativescript.httpinterceptor/files/app/tns_modules/tns-core-modules/ui/frame/fragment.transitions.js', line: 489, column: 5
System.err:     Frame: function:'_setAndroidFragmentTransitions', file:'file:///data/data/org.nativescript.httpinterceptor/files/app/tns_modules/tns-core-modules/ui/frame/fragment.transitions.js', line: 103, column: 9
System.err:     Frame: function:'Frame._navigateCore', file:'file:///data/data/org.nativescript.httpinterceptor/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 128, column: 32
System.err:     Frame: function:'FrameBase.performNavigation', file:'file:///data/data/org.nativescript.httpinterceptor/files/app/tns_modules/tns-core-modules/ui/frame/frame-common.js', line: 275, column: 14
System.err:     Frame: function:'FrameBase._processNavigationContext', file:'file:///data/data/org.nativescript.httpinterceptor/files/app/tns_modules/tns-core-modules/ui/frame/frame-common.js', line: 261, column: 18
System.err:     Frame: function:'resume_1', file:'file:///data/data/org.nativescript.httpinterceptor/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 238, column: 68
System.err:     Frame: function:'Observable.notify', file:'file:///data/data/org.nativescript.httpinterceptor/files/app/tns_modules/tns-core-modules/data/observable/observable.js', line: 103, column: 23
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.httpinterceptor/files/app/tns_modules/tns-core-modules/application/application.js', line: 197, column: 24
System.err: 
System.err:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3400)
System.err:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440)
System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2713)
System.err:     at android.app.ActivityThread.-wrap12(ActivityThread.java)
System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
System.err:     at android.os.Looper.loop(Looper.java:154)
System.err:     at android.app.ActivityThread.main(ActivityThread.java:6077)
System.err:     at java.lang.reflect.Method.invoke(Native Method)
System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
System.err: Caused by: com.tns.NativeScriptException: 
System.err: Calling js method onActivityResumed failed

To recreate:

tns --version
3.3.1
tns create http-interceptor --template ng

Add the following to app.module.ts:

import { Injectable } from '@angular/core';
import { HttpClientModule, HTTP_INTERCEPTORS, HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { NativeScriptHttpClientModule } from "nativescript-angular/http-client";

@Injectable()
export class HttpChecker implements HttpInterceptor {
    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(req);
    }
}

    providers: [
        ItemService,
        {
            provide: HTTP_INTERCEPTORS,
            useClass: HttpChecker,
            multi: true,
        }
    ],

Appreciate any suggestions. Thanks.


#2

https://github.com/NativeScript/nativescript-angular/issues/1085#issuecomment-354571436 suggested moving up HTTP related imports. After experimenting, I believe it’s that HTTP_INTERCEPTORS must be imported after NativeScriptHttpClientModule.

Not sure the exact reason, but this is good enough and I will close the topic.