阅读 2453

这可能是Android最傻瓜式的AOP框架

背景

经过之前两篇文章
Android AOP方案(一)——AspectJ
Android AOP方案(二)——ASM
我们对Android的AOP有了初步的了解,但是其高门槛和学习成本还是让很多人望而却步。今天这里就给大家一个傻瓜式的AOP框架,这可能是Android最傻瓜式的AOP框架了。

butcherknife 介绍

乍看上去是不是有点眼熟,对我就是为了蹭JakeWharton大神的butterknife框架的热度,哈哈。这里 butcherknife我翻译成“屠刀”,意思是希望用最简单的方式能达到屠刀式的代码织入,能够完美处理Lambda表达式,下面是github的地址
github.com/LitterSun/b…

butcherknife 使用

butcherknife是通过注解的形式定义切点,然后进行代码织入的,类似Aspectj,但是只有5个简单的注解,如下:

@Aspect

表明一个类是Aspect Class,且class必须是public,如

@Aspect
public class FragmentInjector {
}
复制代码

@BeforeCall

方法调用前织入代码,如

@Aspect
public class FragmentInjector {
private static final String TAG = "FragmentInjector";

@BeforeCall(clazz = FragmentTransaction.class, method = "replace")
public static void beforeCallFragmentReplace(FragmentTransaction transaction, int containerViewId, Fragment fragment) {
    Log.e(TAG, "beforeCallFragmentReplace: transaction = " + transaction + ", containerViewId = " + containerViewId + " ,fragment = " + fragment);
    }
}
复制代码

织入前的代码

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getFragmentManager().beginTransaction().replace(R.id.fragment, new BlankFragment()).commit();
    }
}
复制代码

织入后的代码

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
        BlankFragment fragment = new BlankFragment();
        int containerViewId = R.id.fragment;
        FragmentInjector.beforeCallFragmentReplace(transaction, containerViewId, fragment);
        transaction.replace(containerViewId, fragment).commit();
    }
}
复制代码

@AfterCall

方法调用后织入代码,同上@BeforeCall这里不在累述

@BeforeSuperExecute

父类方法内部执行前织入代码,如果是子类没有重写父类方法的话,将强制实现该方法,且该方法只有只会在直接子类中只会织入一次,子类的子类不在织入,防止多次调用。

@Aspect
public class FragmentInjector {
    private static final String TAG = "FragmentInjector";

    @BeforeSuperExecute(clazz = Fragment.class, method = "onCreate")
    public static void beforeFragmentCreate(Fragment fragment, Bundle savedInstanceState) {
        Log.e(TAG, "beforeFragmentCreate: fragment = " + fragment + ", savedInstanceState = " + savedInstanceState);
    }

    @AfterSuperExecute(clazz = Fragment.class,       method = "onResume")
    @AfterSuperExecute(clazz = DialogFragment.class, method = "onResume")
    @AfterSuperExecute(clazz = ListFragment.class,   method = "onResume")
    public static void afterFragmentResume(Fragment fragment) {
        Log.e(TAG, "afterFragmentResume: fragment = " + fragment);
    }
}
复制代码

织入前的代码

public class BlankFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}
复制代码

织入后的代码

public class BlankFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        FragmentInjector.beforeFragmentCreate(this, savedInstanceState);
        super.onCreate(savedInstanceState);
    }
    
    @Override
    public void onResume() {
        super.onResume();
        FragmentInjector.afterFragmentResume(this);
    }
}
复制代码

@BeforeSuperExecute同样适用于interface,如下,且能适配Lambda表达式

@Aspect
public class ClickListenerInjector {
    private static final String TAG = "ClickListenerInjector";

    @BeforeSuperExecute(clazz = View.OnClickListener.class, method = "onClick")
    public static void beforeViewOnClick(View.OnClickListener listener, View view) {
        Log.e(TAG, "beforeViewOnClick: listener = " + listener + ", view = " + view);
    }
}
复制代码

@AfterSuperExecute

父类方法内部执行后织入代码,同上@BeforeSuperExecute这里不在累述

注解方法定义

这里有朋友可能观察到了注解下面方法定义的规则

  1. 方法必须是 public static
  2. 第一个参数是切点的this对象,后面的参数分别的切点方法的参数
  3. 除this参数外后面的参数类型是严格匹配,顺序和类型必须和切点方法保持一致

butcherknife 集成

在project根目录的build.gradle添加插件

buildscript {
    repositories {
        mavenLocal()
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:x.x.x'
        classpath "com.littersun.butcherknife:butcherknife-gradle-plugin:1.0.0"
    }
}
复制代码

然后在APP module的build.gradle应用插件

apply plugin: 'com.littersun.butcherknife'
复制代码

在需要的module中添加注解的依赖

dependencies {
    implementation "com.littersun.butcherknife:butcherknife-annotations:1.0.0"
}
复制代码

交流

butcherknife 的定位是一个轻量级的AOP框架,可能没有AspectJ那么强大,可以修改切点代码执行过程。但是已经满足了绝大部分的使用场景,对比AspectJ,butcherknife可以完美处理Lambda表达式,以及强制重写父类方法然后织入代码。 butcherknife还是在一个初级阶段,大家有什么建议可以一起交流和开发。