About Hansel

How do you build instant native apps?

Why instrumentation ?

There are multiple tools available in the market which do a good job as crash reporting tools. But in a world which is increasingly going mobile, the need for a tool that lets you diagnose and fix these crashes in real time becomes a need rather than a want. A drop in service could impact your transactions, your ratings and the customer experience. 

At Hansel.io, we are a set of developers dedicated to making sure your apps never, ever crash. As app developers we have all encountered the painful process of fixing a bug but only being able to release the fix after a painfully long dev → QA → review cycle, and the painfully long wait for users to update. After presenting at DroidCon we realised that there are many developers out there who face the same issue and might not have used Hansel. This blog is part of a 3 blog series in which we help you create your own tool to

  1. Arrest crashes
  2. Add logs
  3. Change UI and configs – all at run time.

Aspect Oriented Programming – AspectJ

Before we begin building our tool, let’s take a look at Aspect Oriented Programming using AspectJ. AspectJ is an instrumentation tool, available in Eclipse foundation open source projects. It’s based on the principle of Aspect Oriented Programming.

Aspect-oriented programming (AOP) helps you in adding additional behaviour to existing code without modifying the actual functions. The code for the additional behaviour is maintained as a separate module. You can create an expression which specifies the function(s) which should be modified and then compile your code using AspectJ.

The additional behaviour you would like to add is called Advice and the expression which marks the functions on which the advice has to be run is called Pointcut.
Screen Shot 2016-11-25 at 5.58.00 PM

On the left is your code and on your right you have the cross cutting concerns, the secondary functionality, which should not pollute your code and still run when needed. Now we know enough to get started.

Screen Shot 2016-11-25 at 5.57.51 PM

Say you pointcut a function FOO on which an Advice should run. AspectJ will convert your function Foo into FooO (the original one) create a new function called Foo, which has the Advice for the Foo function.

There are 3 different kinds of advice definitions – before, after, and around advices, which are executed before, after, or instead of a target method. In our example we would be using the “around” advice.

You could potentially unlock –

  • Debugging issues in real time
  • Arresting crashes on live apps
  • Disabling functions

and much more if AOP is used right.

So now we have basic knowledge of what AOP can do for us, let’s combine it with configs from the server to build the tool which lets you arrest crashes on your live apps.

public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    String className = methodSignature.getDeclaringType().getName(); // The Class on which the Advice gets executed
    String methodName = methodSignature.getName(); // The method on which the Advice gets executed

    Action action = Action.CALL_SELF;
    JSONObject actionDataMap = null;
    JSONObject functionToActionMap = classMethodMap.optJSONObject(className);
    // Making sure the input Class received from configs is not null
    if (functionToActionMap != null) {
        actionDataMap = functionToActionMap.optJSONObject(methodName);
        // Making sure the input method received from configs is not null
        if (actionDataMap != null) {
            Action actionTemp;
            try {
            // Capturing the action from the JSON Object
                actionTemp = Action.valueOf(actionDataMap.optString(KEY_ACTION));
            }catch (IllegalArgumentException il){
                actionTemp = Action.CALL_SELF;
            if (actionTemp != null) {
                action = actionTemp;
                if (action == Action.LOG_ALL) {
                    logAll = true;

    if (action == Action.TRY_CATCH) {

    try {
        result = joinPoint.proceed();
    } catch (Exception ex) {
      //your logic to handle the exception

    } else (action == Action.CALL_SELF) {

        result = joinPoint.proceed();

    return result;
public void responseReceived(String response) {
    try {
        classMethodMap = new JSONObject(response);
    } catch (Exception exc) {
        classMethodMap = new JSONObject();
   your.package.YourClass: {
    "yourMethod" : {
      "action" : "TRY_CATCH"

And that’s it. All you have to do is make this live and you can now fix crashes on your live apps in real time!

This tool is just a POC and the potential of AOP is much, much more. We will explore this in greater detail in our next set of blogs. Happy coding!

You Might Also Like

    We would love to help you understand Hansel better