CrepeCake

General

Category
Free
Tag
AOP
License
Apache License, Version 2.0
Min SDK
9 (Android 2.3–2.3.2 Gingerbread)
Registered
Jan 25, 2018
Favorites
0
Link
https://github.com/MEiDIK/CrepeCake
See also
AspectJ
Legend
Android Aspects

Additional

Language
Java
Version
N/A
Created
Dec 28, 2017
Updated
Jan 24, 2018
Owner
认真的帅斌 (MEiDIK)
Contributor
认真的帅斌 (MEiDIK)
1
Activity
Badge
Generate
Download
Source code
APK file

Promotion

CrepeCake

A compile-time aop engine like AspectJ but easier to use in android application development.

Wiki


That's all,these code will be executed during the onCreate method in MainActivity with printing the running time of it:

I/System.out: ⇢ onCreate
I/System.out: ⇠ onCreate [33ms]

Installation

  1. Add the buildscript dependencies in the root project:
buildscript {

  repositories {
      google()
      jcenter()
  }
  dependencies {
      classpath 'com.android.tools.build:gradle:3.0.1'
      classpath 'net.idik.crepecake:plugin:0.0.4' // Add Here
  }

}
  1. Add crepecake plugin in the target module

The crepecake plugin MUST AFTER(VERY IMPORTANT) the application plugin

// Apply application or library plugin here...
apply plugin: 'net.idik.crepecake'

Usage

  1. Programme the injection processor on the basis of target class, with the annotation @Aspect
@Aspect(MainActivity.class)
public class MainActivityAspect {
  // injection methods...
}
  1. Programme the injection method

    Step 1. Declare the injection method with the target method infos, including the access level, static or not, name of method, parameters.

    Step 2. Insert InvocationHandler invocationHandler at the 1st position of the parameter list

    Done.

    @Aspect(MainActivity.class)
    public class MainActivityAspect {
    
        protected void onCreate(InvocationHandler invocationHandler, Bundle savedInstanceState) {
          System.out.println("⇢ onCreate");
          long startTime = System.currentTimeMillis();
          invocationHandler.invoke(savedInstanceState);
          System.out.println(String.format("⇠ onCreate [%dms]", System.currentTimeMillis() - startTime)); 
        }
    
        public android.support.v7.app.ActionBar getSupportActionBar(InvocationHandler invocationHandler) {
            ActionBar bar = (ActionBar) invocationHandler.invoke();
            //do stuff...
            return bar;
        }
    
        //other injection methods...
    }

So far, we have completed the job that injects the onCreate(Bundle savedInstanceState)getSupportActionBar() and other methods in the MainActivity.

InvocationHandler is a flag indicating this is an injection method. Beside, we could invoke the original method through it and obtain the result(if return type is not void).

AspectConfig

We could customize the injection points by inheriting the AspectConfig class

public class OnClickAspectConfig extends AspectConfig {
    @Override
    protected boolean isEnable() {
        return super.isEnable();
    }

    @Override
    public boolean isHook(Class clazz) {
        return View.OnLongClickListener.class.isAssignableFrom(clazz) || View.OnClickListener.class.isAssignableFrom(clazz);
    }
}

The above codes means that we will inject ALL the subClass of OnLongClickListener and OnClickListener.

⚠️ Attention: The AspectConfig class will be executed during the compile-time, so do not make any running-time logic within it.

Then, use it to the injection processor with the annotation @Aspect

@Aspect(OnClickAspectConfig.class)
public class OnClickListenerAspect {
  public void onClick(InvocationHandler invocationHandler, View view) {
      System.out.println("OnClick: " + view);
      invocationHandler.invoke(view);
  }

  public boolean onLongClick(InvocationHandler invocationHandler, View view) {
      boolean isConsume = (boolean) invocationHandler.invoke(view);
      if (isConsume) {
          System.out.println("OnLongClick: " + view);
      }
      return isConsume;
  }
}

See more...

Great Thanks to...