29

I am developing a crash reporter plugin for phonegap android apps. For the testing purpose, I have to make my application crash & "Unfortunately, application has stopped" window has to be invoked. When I make an unhandled exception in the javascript, the app is not crashing. Instead its showing the same screen. I made the app screen stop respond to user input by executing some infinite loop in javascript & waited around 1 hour, still the app was not crashing. Is the phonegap library by default handling the exceptions ? how I can make my app crash by making exception in javascript level ?

I tried the below code,added a java method to generate crash in the 'CordovaActivity' class.

public static void generateCrash()
   {
     int a = 0;
     int b = 10;
     int c = b/a ;
    }

The app is crashing when I call this method from java (from the 'onCreate' in the activity class) . But when I invoke the same method from the javascript using plugin, the app is not crashing. I want my app to crash by calling/invoking some function from the javascript.

Sinu Varghese
  • 800
  • 1
  • 14
  • 39
  • 2
    Great question. You could try an infinitely recursive function: `void function turtle() { turtle() } ()` (pure speculation). – joews Feb 05 '15 at 10:03
  • 2
    Is this is any use? http://stackoverflow.com/q/28129601/334274 – Jivings Feb 05 '15 at 10:05
  • 1
    @joews, the recursive function generates exception ("Uncaught RangeError: Maximum call stack size exceeded"). But the app is not crashing, instead its showing the same screen. – Sinu Varghese Feb 05 '15 at 10:17
  • 1
    I don not know how to make exception through javascript but once my application got crashed when i used a style class which is actually written twice in .css. – Prabhu Vignesh Rajagopal Feb 05 '15 at 10:24
  • 1
    @Jivings, the link given by you is not helpful. – Sinu Varghese Feb 05 '15 at 10:32
  • @PrabhuVigneshRajagopal, writing a style class twice in a .css file is not making the application crash. – Sinu Varghese Feb 05 '15 at 10:52
  • @SinuVarghese As i said i doesn't know about crashing it. but i fixed my application from crashing by avoid using such class and removing such class. – Prabhu Vignesh Rajagopal Feb 05 '15 at 10:57
  • create a null object and use it (on java code) – jcesarmobile Feb 05 '15 at 11:07
  • @jcesarmobile, creating a null object in java & using it makes the application crash. But I want to make the app crash from javascript as the phonegap app is more based on javascript than java. – Sinu Varghese Feb 05 '15 at 11:12
  • ok, I though you just wanted the app to crash. Then try using phonegap plugins wrong, passing arrays where it expects strings and something like that. – jcesarmobile Feb 05 '15 at 11:22
  • @jcesarmobile, making the plugin call from javascript with wrong arguments also not making the app crash. I guess javascript level exceptions are handled by phonegap library so that the app doesn't crash. – Sinu Varghese Feb 05 '15 at 11:47
  • 1
    Surely if 'stack overflow' and 'out of memory' doesn't trigger your crash reporter then you have a problem... while(true){} should be enough to not respond to user input. – Daniel Feb 16 '15 at 01:46
  • @Daniel, while(true){} is enough to make the app to stop respond to user input, but it will make a phonegap app to crash. 'out of memory' will not trigger the crash reporter as the crash reporter is also part of the app & no more memory is available for the app for its execution. – Sinu Varghese Apr 21 '15 at 08:13
  • I think you might need to redefine crashing here. Unless you find some security holes or bugs in the WebView class you aren't going to crash the app from Javascript. – hobberwickey Apr 28 '15 at 05:24
  • @SinuVarghese as mentioned in my post we can make the app crash from javascript but for that you have to remove 2 catch statements in cordova classes. From this you can test your crash reporter. Let me know if this didnt work in your case. – kumar Apr 29 '15 at 06:54
  • @kumar this would work. but I am searching an option without editing the codova library. Aaron D posted to override onmessage function & throw the exception from the main thread. editing of cordova library might not be required in that case. – Sinu Varghese Apr 29 '15 at 07:27

9 Answers9

10

Crash by pressing menu button:

You cannot make the app crash from a plugin or javascript call as the exceptions are handled internally. If you want to make the app crash in android you can edit CordovaActivity.java in Android platform. Change onCreateOptionsMenu as shown:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    this.postMessage("onCreateOptionsMenu", menu);
    throw new RuntimeException();
} 

Press the menu button and app will crash & "Unfortunately, application has stopped" window will be displayed.

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.

kumar
  • 1,814
  • 1
  • 24
  • 35
  • You are right. We cannot make the app crash from a plugin or javascript call as the cordova library by default handles the exception in the web view. So it might not be possible to generate the crash from Javascript without editing the web view implementation classes in the cordova library. So I am accepting this answer. – Sinu Varghese Apr 29 '15 at 07:40
6

If an app would run like a webpage, a infinite loop might take some time to cause a crash, but should work. Just remember to remove it after testing. Because it causes an exception, and times out the program, the crash should be properly invoked if something possible harmful is used in it. Do something like this:

while(true){ eval("9.99999e+5000 / Infinity"); }

Because we aren't editing a variable, we will prevent the out of memory error.

If this doesn't work, refer to @Aaron D's answer, and if that doesn't work, refer to @kumar's answer.

Travis
  • 1,274
  • 1
  • 16
  • 33
  • I tried the above code. Executed the infinite while loop, the screen stops responding, but the app is not crashing. Executed the infinite loop & waited for long time(around 45 mins), still the app is not crashing. I guess this is not applicable for hybrid apps. In case of a native application if the screen stops respond for 2-3 minutes, the app will crash. But it is not happening for a phonegap/hybrid app. – Sinu Varghese Apr 21 '15 at 08:06
  • 1
    @SinuVarghese Try what Aaron said, it worked for a hybrid app for me. – Travis Apr 26 '15 at 18:43
  • @Wyatt I tried with throwing runtimeexception from within execute method. I can see the error in console but app didnt get crashed! Am using cordova 3.5 version. So I tried with throwing exception in onCreateOptionsMenu(aswritten below) which worked from me. – kumar Apr 27 '15 at 05:31
  • @kumar Well at least you found a solution. – Travis Apr 27 '15 at 16:53
  • @kumar you are right, exception in execute method is displayed in console but the app is not getting crashed. Throwing exception in onCreateOptionsMenu is generating crash whenever user presses the menu button, but I want the app to crash by invoking/calling some native function from Javascript. – Sinu Varghese Apr 28 '15 at 06:23
  • @SinuVarghese I have edited my answer with the changes required. I tested it in my environment which works fine. Please let me know if you need anything else :). – kumar Apr 28 '15 at 08:54
3

Unfortunately, JS exceptions are handled by the WebView and even native plugins run in their own separate thread, and will not crash the main Application thread if you raise an exception there (this may not be true for hybrid applications). So you need to hook into the main activity and cause it to throw an Exception on the main thread.

Subclass the CordovaActivity class and override the plugin messaging method:

@Override
public Object onMessage(String id, Object data) {
    if ("crashApp".equals(id)) {
        throw new Exception();
    }
    super(id, data);
}

Write an Android native plugin for Phonegap as per this answer. What it does doesn't matter, but when you message it from the Javascript you should use the id 'crashApp' which should be picked up by your overridden method in the CordovaActivity subclass, and raise the Exception on the main thread.

. Then you can message your plugin using this id whenever you have a Javascript exception by overriding the window.onerror function:

window.onerror = function myFunction(errorMsg, url, lineNumber) { 
/* call Android native function to throw Exception */ ; 
return false; }

This will throw all Javascript errors down into your Java code where they can raise the ANR dialog (and you can pass debugging information with it too).

Community
  • 1
  • 1
Aaron D
  • 7,540
  • 3
  • 44
  • 48
  • I tried this, I overridden the window.onerror function. Now all the Java-script exceptions are landing in this function. But from there when I call the android native function to throw the exception, I can see the error in console but app didn't get crashed. It is automatically got handled somewhere in the web view's implementation. – Sinu Varghese Apr 28 '15 at 05:30
  • You're right, in some cases the plugins are running in a separate thread and an exception there will not crash the main application thread. I've updated my answer to provide a technique whereby you can subclass the application's main `Activity` and override Cordova's `onMessage` public method to crash if you send it the right message. – Aaron D Apr 28 '15 at 07:18
  • your overriding onMessage code is wrong. The method is supposed to return an Object but you are not returning anything. Also call to super must be the first line in the method. Your solution will not work if you make those changes also as the throw clause has to be enclosed in a try-catch block. so the exception will be handled in the catch block & the app won't crash. – Sinu Varghese Apr 29 '15 at 07:52
2
  1. try download any file more that 100MB(my app got stuck in low end androids). You can even do that in loops.

  2. Use canvas and drop some random 3d figures.

  3. app an alert message in a loop of 1000.

Vishnuraj
  • 71
  • 10
  • I already tried similar code. This makes the app crashes due to 'Out Of Memory Error'. But in this case, crash reporter plugin is not working as the plugin is part of the app & no more memory is available for the app for its execution. – Sinu Varghese Apr 21 '15 at 08:18
2

You can make your cordova/ phonegap application crash by removing the permissions from the AndroindManifest.xml file but still using them inside the app, these kind of exceptions mostly crash your application.

please note that (depending on your project structure) modifying the AndroindManifest.xml file itself, it may be regenerated and change back to its correct state so you're gonna need to remove permission lines and then change the Androind.json file in the plugins folder befor build, appropriately to make your changes take effect.

I tested this process and it definitely works! :D

hope it helps, mim ;)

mim
  • 1,301
  • 14
  • 24
  • 1
    In the process of developing a Phonegap app right now, and I can verify after some banging my head against the wall that not having the correct permissions will indeed crash your app. – hobberwickey Apr 28 '15 at 05:20
1

I strongly believe infinite loops won't work in this case. When you use it with PhoneGap, javascript is only a (safe) script language that cannot generate system-driven exceptions.

The best method is to run some native codes (problematic codes) under the help of another PhoneGap plugin. For example, you can do division by zero in objective-c and java native codes.

Nick Song
  • 1,988
  • 3
  • 29
  • 35
  • you are right. infinite loops won't work in this case & javascript cannot generate system-driven exceptions. Exception in native code can generate crash, but the issue is that when I invoke the problematic native code from javascript with the help of PhoneGap plugin, it is not generating the crash. – Sinu Varghese Apr 28 '15 at 05:15
0

Usually a halt in execution, intentional or not, can cause android to think the app has crashed. Try wrapping your code in something like setTimeout(function() { your code }, 60000) to delay execution by a minute.

Or, try making an ajax request that will time out.

mduleone
  • 73
  • 10
  • 2
    Delaying the execution will not make a phonegap app to crash. I made the app screen stops responding by executing some infinite loop & waited for around 1 hour, still the app was not crashing. – Sinu Varghese Apr 21 '15 at 08:26
  • Sorry about that, I expected it would cause the main thread of the app to crash, as something like that in a native app would work. – mduleone May 28 '15 at 02:37
0

how I can make my app crash by making exception in javascript level ?

You cannot handle js errors that causes the "Unfortunately, application has stopped" message, because they don't exist and are not javascript-related but Webview/plugin-related.

If yous see this kind of message after a js call, then it comes from the web view's javascript implementation, AKA Chrome's one or the plugin's native part. Javascript is sandboxed and should not produce such behaviours.

n00dl3
  • 21,213
  • 7
  • 66
  • 76
-1

You can try to do a infinity loop incrementally appending a string as the app starts:

onDeviceReady: function() {
    var test = "test";
    while (true) {
        test += "--------------------";
        console.log(test);
    }
},

This will do the trick.

Cesar
  • 117
  • 1
  • 7
  • 1
    I already tried this. This makes the app crashes due to 'Out Of Memory Error'. But in this case, crash collection plugin is not working as its part of the app & no more memory is available for the app for its execution. Moreover 'Out Of Memory' is an error & not an exception. I want my app to crash due to an exception( in the java script level) so that I can test my crash report collection code. – Sinu Varghese Feb 10 '15 at 05:56
  • This will end up in a memory issue what is not wanted. – Sithys Apr 21 '15 at 11:01