美文网首页开源库程序员安卓
Android最傻瓜式的AOP框架

Android最傻瓜式的AOP框架

作者: 冬日毛毛雨 | 来源:发表于2020-12-22 16:41 被阅读0次

    前景

    我们对Android的AOP有了初步的了解,但是其高门槛和学习成本还是让很多人望而却步。今天这里就给大家一个傻瓜式的AOP框架,这可能是Android最傻瓜式的AOP框架了。

    butcherknife 介绍

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

    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表达式,以及强制重写父类方法然后织入代码。

    大家如果还想了解更多Android 相关的更多知识点,可以点进我的GitHub项目中自行查看,里面记录了许多的Android 知识点。

    相关文章

      网友评论

        本文标题:Android最傻瓜式的AOP框架

        本文链接:https://www.haomeiwen.com/subject/xomsnktx.html