Search Unity

How to force crash on Android to test crash reporting systems

Discussion in 'Unity Cloud Diagnostics' started by batrand, Apr 1, 2019.

  1. batrand

    batrand

    Joined:
    Mar 20, 2014
    Posts:
    1
    I have been looking for how to force a crash on Android. Techniques like Application.ForceCrash(), infinite loops and throwing exceptions do not crash the app (they just freeze the app). I was looking into replicating crashes that makes Android show "App has stopped working" dialog, and the technique here on StackOverflow does it, as it invokes the exception handler on the Android level instead of Unity.

    Here is a MonoBehaviour version from the link above. Hope this helps others who need it. Call the CrashOnAndroid() from somewhere e.g. button click.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Crasher : MonoBehaviour
    4. {
    5.     public void CrashOnAndroid()
    6.     {
    7.         // https://stackoverflow.com/questions/17511070/android-force-crash-with-uncaught-exception-in-thread
    8.         var message = new AndroidJavaObject("java.lang.String", "This is a test crash, ignore.");
    9.         var exception = new AndroidJavaObject("java.lang.Exception", message);
    10.        
    11.         var looperClass = new AndroidJavaClass("android.os.Looper");
    12.         var mainLooper = looperClass.CallStatic<AndroidJavaObject>("getMainLooper");
    13.         var mainThread = mainLooper.Call<AndroidJavaObject>("getThread");
    14.         var exceptionHandler = mainThread.Call<AndroidJavaObject>("getUncaughtExceptionHandler");
    15.         exceptionHandler.Call("uncaughtException", mainThread, exception);
    16.     }
    17. }
     
  2. xavi_GSN

    xavi_GSN

    Joined:
    Dec 7, 2017
    Posts:
    4
    This worked for me ! Thanks a lot batrand ! This reported a crash while the Utils.ForceCrash method included in Unity didn't work.
     
  3. DanielSteegmueller

    DanielSteegmueller

    Unity Technologies

    Joined:
    May 3, 2022
    Posts:
    2
    Johannski likes this.