2

I need to force the crash of an Android App, from inside a Cordova plug-in class.

The problem is that the plug-in class runs in a separated Thread, so even if I force a RuntimeException(), it crashes just the separated thread.

Idan Adar
  • 44,156
  • 13
  • 50
  • 89

2 Answers2

0

What your plugin code should do is to define handler on your for unhandled exception as defined on Rahits answer. Idea is to attach to event like this (code borrowed from the answer linked)

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
  public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
    Log.e("Error"+Thread.currentThread().getStackTrace()[2],paramThrowable.getLocalizedMessage());
  }
});

and then when the callback is called, throw the real exception which causes the desired effect.

Community
  • 1
  • 1
Roope Hakulinen
  • 7,326
  • 4
  • 43
  • 66
  • So, I put the snippet that you suggested above just after the onCreate() of my main Activity. Afterwards, I call the cordova plugin where I forced a RuntimeException(): but the exception thrown isn't caught by the uncaughtException() handler of my main activity. What ahppens is that the the 'communication' between JAVA and JS breaks with an error message like this: – Giuseppe Roberto Rossi Jan 25 '15 at 00:13
  • "processMessage failed: Message: S01 WLCustomDialog452008395 n2", source: file:///android_asset/www/default/wlclient/js/cordova.js (976) "processMessage failed: Error: Error: Error calling method on NPObject.", source: file:///android_asset/www/default/wlclient/js/cordova.js (977) "processMessage failed: Stack: Error: Error calling method on NPObject. at Object.androidExec [as exec] (file:///android_asset/www/default/wlclient/js/cordova.js:860:48) etc... – Giuseppe Roberto Rossi Jan 25 '15 at 00:13
  • @GiuseppeRobertoRossi: So in handler you should crash that thread too to make the whole app crash? – Roope Hakulinen Jan 25 '15 at 09:09
  • Yes, I need to crash the app in a cordova plugin because I can't in javascript – Giuseppe Roberto Rossi Jan 25 '15 at 09:12
  • Yes, so why can't you crash it on that exception handler which runs in main thread AFAIK? – Roope Hakulinen Jan 25 '15 at 20:32
  • Because if I force a runtime exception in the plugin, it isn't caught by the exception handler. That's what I wrote above. How could I force an exception an get caught by the exception handler in the hybrid app? – Giuseppe Roberto Rossi Jan 25 '15 at 21:30
  • @GiuseppeRobertoRossi refer http://stackoverflow.com/questions/28340920/how-to-make-my-phonegap-android-app-crash/29875804#29875804 – kumar May 05 '15 at 02:43
0

If you want to crash the app from cordova plugin/or pressing menu button in android app you have to edit few classes in cordova project as mentioned in how to make my phonegap android app crash?.

Crash by calling some native function from Javascript:

Write an Android native plugin for Phonegap. Refer http://docs.phonegap.com/en/3.0.0/guide_platforms_android_plugin.md.html#Android%20Plugins for plugin creation. Throw exception inside execute method. This will be handled in the parent layer(Thats why you can see logs about crash in the console), So please do following changes to make the app crash.(Both the classes belong to org.apache.cordova package)

  • Remove catch (Exception e){} block in execHelper method of pluginManager classs.
  • Remove catch (Throwable e) {} block in exec method of ExposedJsApi class.

With this change I am able to crash the application from javascript call.

Community
  • 1
  • 1
kumar
  • 1,814
  • 1
  • 24
  • 35
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Gautam May 04 '15 at 10:01
  • @Gautam Edited the answer with required snippets. Thanks for the advice :). – kumar May 04 '15 at 10:32