53

I want to test out crash report using acra but the first step is I need to simulate a fatal crash in Android using code.

Any idea?

Ahmad
  • 69,608
  • 17
  • 111
  • 137
Howard
  • 19,215
  • 35
  • 112
  • 184

17 Answers17

108

Just execute this code: divide by zero

Update: Also can try this Create a method,

public void stackOverflow() {
    this.stackOverflow();
}

And call this somewhere/buttonClick

OR simply throw an uncaught exception

throw new RuntimeException("This is a crash");

Bingo!

emen
  • 6,050
  • 11
  • 57
  • 94
Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
  • "OR simply throw an uncaught exception" maybe you could add a third solution throw a caught exception? or i'll do it myself :) – ceph3us May 19 '19 at 10:19
31
  1. Access a view that is not defined.
  2. Access the first element of an empty list without checking.
  3. Divide by Zero.
  4. Throw the device out the window.
  5. Submerge the device in water.
Makyen
  • 31,849
  • 12
  • 86
  • 121
JoxTraex
  • 13,423
  • 6
  • 32
  • 45
13

Don't declare activity in the Android Manifest .

Ahmad
  • 69,608
  • 17
  • 111
  • 137
Chirag
  • 56,621
  • 29
  • 151
  • 198
10

You could try a Null Pointer exception.

Integer i = null;

Then invoke any method on the object.

i.byteValue();

Invoking a method on an object that hasn't been initialized will crash the app.

Luke Taylor
  • 9,481
  • 13
  • 41
  • 73
7

A very simple approach... and is very important to understand that why it happened.

Try initiating a variable in onCreate() before the setContentView() method, then use it to call a method or variable or try registering it to some listener..

Eg:

Button b;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        b = (Button)findViewById(R.id.butt);
        b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {


        }
    });
        setContentView(R.layout.main);


    }

This crashed, because before setContentView() none of the components/view in the main.xml layout got their ids.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
6

Simply create in your main.xml a button like this:

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="crash"
    android:text="Crash me" />

then run your app and click for crash

FaBioInsolia
  • 96
  • 1
  • 2
3

Most simple I know : throw null.

You can't throw null, so a NullPointerException is raised.

throw null;
sexyslippers69
  • 334
  • 2
  • 9
3

in addition to @vinnet-shukla answer:

"OR simply throw an uncaught exception"

throwing uncaught exception to do a crash is bad idea as the exception could by caught somwhere higher in the stack - especially when whe don't know where we are right now :)

more ellegant way is to use ERROR

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

so we could make our own Error subclass and use it:

/*
 * usage:
 * 
 *   CrashError.doCrash(); 
 *
 */
public class CrashError extends Error {

    public CrashError() { 
        this("simulated crash");
    }

    public CrashError(String msg) {
        super(msg);
    }

    public static void doCrash() {
        throw new CrashError();
    }

}

but if we talk about other possibilities we could also a throw checked exception :)

this will also be a lesson how we could RETHROW CHECKED EXCEPTION :) with other way than use of sun.misc.Unsafe especially when the class is not available in VM implementation

@SuppressWarnings("unchecked")
public static <E extends Throwable> void throwAnyT(Throwable e) throws E {
    throw (E) e;
}

public static void throwUnchecked(Throwable e) {
    throwAny(e);
    // should never get there.
    throw new InternalError();
}

public static void crash() {
    throwUnchecked(new java.io.IOException("simulated crash"));
}

in addition to @audric answer:

"You can't throw null"

yes you can :) it's exactly what you are doing in your example and yes it could get undetectable if the catch block will not use Throwable - the NPX will never be thrown and simply there will be normal flow when code will still execute and yes i'll have seen this and experienced by myself :) on ANDROID DALVIK

and what about..? maybe it could fulfill your needs?

java specific (also in android):

- Runtime.getRuntime().exit(int);
- System.exit(int);
- Runtime.getRuntime().halt(int);

android specific:

- android.os.Process.killProcess(int);
- android.os.Process.killProcessQuiet(int);
- android.os.Process.sendSignal(int,int);
- android.system.Os.kill(int);
ceph3us
  • 7,326
  • 3
  • 36
  • 43
2

You can use activity manager to crash the app with no code changes

adb shell am crash <packagename>
Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126
1

you can crash with a simple null point exception.

 throw new NullPointerException();
1

Force crash after some delay like this,

new android.os.Handler().postDelayed(
              new Runnable() {
                  public void run() {
                      Log.i("tag", "This'll run 10 seconds later");
                      throw new RuntimeException("This is a crash");
                  }
              },
              10000);

Ref-1 & Ref-2

Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
1

If you are using firebase crashlytics, then there is a very easy way to do this. Mentioned in their document also.

val crashlytics = FirebaseCrashlytics.getInstance()
crashlytics.log("my message")
JensV
  • 3,997
  • 2
  • 19
  • 43
Prateek
  • 189
  • 1
  • 6
1

For kotlin:

val test = ("0")
println(test[1])
Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
Crebain
  • 180
  • 1
  • 8
0

You can crash the application simply with this line of code.

throw new RuntimeException("Test Crash"); // Force a crash

You can pass the message, in this case "Test Crash", which can be useful later to refer.

One step ahead, Make sure none of your application's class files implement UncaughtExceptionHandler interface as below,

public class MyApplication extends Application implements Thread.UncaughtExceptionHandler{

Otherwise the above exception or any exception for that matter, would be caught/consumed by that class, resulting in not crashing the app. However, you can get hold of that exception in uncaughtException(Thread t, Throwable e) method as below and take the required action. In your case, you'd be reporting it to the Acra using their supported SDK methods.

@Override
public void uncaughtException(Thread t, Throwable e) {
    Log.e("MyApplication", "Error", e);
}
Rohit Jagtap
  • 1,660
  • 1
  • 14
  • 12
0

I couldn't throw an exception because the compiler wouldn't let me unless I was catching it somewhere!

Do this instead:

    if (1 == 1 ) {
        throw new RuntimeException("This is a crash");
    }
Alan Nelson
  • 1,129
  • 2
  • 11
  • 27
0

This code will add a button on your view and when clicked it will crash application.

val crashButton = Button(this)
crashButton.text = "Test Crash"
crashButton.setOnClickListener {
   throw RuntimeException("This is a Test Crash")
}
addContentView(crashButton, ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT))
Rabindra Khadka
  • 1,344
  • 1
  • 13
  • 23
-3

Use the following code:

String xyz=null;
system.out.println(xyz);