Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):
Describe the bug
When extending android.app.Service and returning START_STICKY from onStartCommand application crashes when process is killed.
To Reproduce
Create app with a custom service, then kill the app. When android tries to restart service, nativescript throws com.tns.NativeScriptException: Failed to create JavaScript extend wrapper for class 'tk/ozymandias/ServiceTest/Service'.
Expected behavior
Service is successfully restarted and keeps running in the background.
Sample project
~https://github.com/OzymandiasTheGreat/nativescript-servicetest~
https://github.com/OzymandiasTheGreat/Nativescript-ServiceExample
Additional context
As you can see from the sample project, service is started successfully on app start and keeps logging PINGs. I have no clue why it fails when the app is killed.
Here's the stack trace.
An uncaught Exception occurred on "main" thread.
Unable to create service tk.ozymandias.ServiceTest.Service: com.tns.NativeScriptException: Failed to create JavaScript extend wrapper for class 'tk/ozymandias/ServiceTest/Service'
StackTrace:
java.lang.RuntimeException: Unable to create service tk.ozymandias.ServiceTest.Service: com.tns.NativeScriptException: Failed to create JavaScript extend wrapper for class 'tk/ozymandias/ServiceTest/Service'
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3349)
at android.app.ActivityThread.-wrap4(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1677)
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:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:108)
Caused by: com.tns.NativeScriptException: Failed to create JavaScript extend wrapper for class 'tk/ozymandias/ServiceTest/Service'
at com.tns.Runtime.createJSInstanceNative(Native Method)
at com.tns.Runtime.createJSInstance(Runtime.java:778)
at com.tns.Runtime.initInstance(Runtime.java:751)
at tk.ozymandias.ServiceTest.Service.onCreate(Service.java:29)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3339)
... 9 more
@OzymandiasTheGreat check this thread for a possible reason and solution to this issue
Thank you, that pretty much explains it. I'll have to try that.
I did find a workaround: by implementing a broadcast receiver to restart the service when it's killed and calling startForeground from onCreate I managed to avoid this problem and create a truly persistent service.
~I'm gonna write up a little tutorial for this when I get the time.~
Here's the tutorial for anyone who stumbles on this issue.
Most helpful comment
Thank you, that pretty much explains it. I'll have to try that.
I did find a workaround: by implementing a broadcast receiver to restart the service when it's killed and calling
startForegroundfromonCreateI managed to avoid this problem and create a truly persistent service.~I'm gonna write up a little tutorial for this when I get the time.~
Here's the tutorial for anyone who stumbles on this issue.