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.
using UnityEngine;
public class Crasher : MonoBehaviour
{
public void CrashOnAndroid()
{
// https://stackoverflow.com/questions/17511070/android-force-crash-with-uncaught-exception-in-thread
var message = new AndroidJavaObject("java.lang.String", "This is a test crash, ignore.");
var exception = new AndroidJavaObject("java.lang.Exception", message);
var looperClass = new AndroidJavaClass("android.os.Looper");
var mainLooper = looperClass.CallStatic<AndroidJavaObject>("getMainLooper");
var mainThread = mainLooper.Call<AndroidJavaObject>("getThread");
var exceptionHandler = mainThread.Call<AndroidJavaObject>("getUncaughtExceptionHandler");
exceptionHandler.Call("uncaughtException", mainThread, exception);
}
}