Giter Club home page Giter Club logo

native-crash-handler's Introduction

THIS PROJECT IS DISCONTINUED

While this has been an experiment, it turns out that Android native stack is too:

  • Stripped out of all linux debuging goodies
  • dependant of processor & driver implementations.

Moreover, the use of this library usually makes matter worse when the app crashes inside a thread : The Android system tries to restart the faulting activity making the app having an infinite crash loop.

This project remains here for reference, but I advise against using it until some better way to debug native code is found.











Native-Crash-Handler

A simple set of code to enable JNI Native crashes to be caught by java and throw a meaningfull exception

Why it exists

While java exceptions are a tremendous tool for Android JAVA developers to investigate crashes, JNI C / C++ crashes terminate the process and are not catchable by the system.

Can it work with crash reporters like ACRA ?

Yes ! I'm glad you ask : That's the reason why it was developped in the first place ! As this throws an exception, a crash handler can intercept it and handle it the way it needs.

How it works

On the JNI side, it registers sigaction callbacks to be called when the program receives an error signal (such as SIGSEGV: segmentation fault). It then generates an exception to capture the stack trace and then launches a new activity in a new process (because the current is to be killed by the signal) which will throw an exception.

This tool has been largely inspired by Chris Boyle's great SO answer.

How to use it

  1. Download the archive and extract it

  2. Put NativeCrashHandler-vX.Y.jar in the libs/ directory of your project

  3. Put NativeCrashHandler.h and NativeCrashHandler.cpp in the jni/ directory of your project

  4. Add NativeCrashHandler.cpp to the LOCAL_SRC_FILES variable in your jni/Android.mk file

  5. In your application class, in onCreate, add:

    new NativeCrashHandler().registerForNativeCrash(this);
  1. In The JNI_OnLoad JNI C function, add:
    #include "NativeCrashHandler.h"
    /*...*/
    nativeCrashHandler_onLoad(jvm);
  1. In your AndroidManifest.xml, add:
    <activity
        android:name="com.github.nativehandler.NativeCrashActivity"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:exported="false"
        android:process=":CrashHandler"
        android:stateNotNeeded="true"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />
  1. You're done

Advanced usage

Activity Context

Sometimes, you need to register the NativeCrashACRAHandler with an Activity Context instead of an Application Context. Please remember that this should not be done if you can use the Application Context.

When you use NativeCrashACRAHandler.registerForNativeCrash with an Activity Context, the activity is retained by the NativeCrashACRAHandler JNI code. Therefore, you need to call NativeCrashACRAHandler.unregisterForNativeCrash when the activity is paused, otherwise the application will never be relased by the system. Remember that only ONE context needs to be registered at a time, so you need to unregister an activity before register a new one.

native-crash-handler's People

Contributors

funnything avatar salomonbrys avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.