how to make my phonegap android app crash?

前端 未结 9 1330
萌比男神i
萌比男神i 2021-02-05 03:27

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\"

相关标签:
9条回答
  • 2021-02-05 04:00

    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).

    0 讨论(0)
  • 2021-02-05 04:00

    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.

    0 讨论(0)
  • 2021-02-05 04:00

    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.

    0 讨论(0)
提交回复
热议问题