Skip to content

CGLib-for-Android (Dex code Generation Library for Android) is high level API to dynamicall generate sub class, its implementation based on DexMaker. This library use for AOP, method intercept, data access authorization authentication on Android.

bluesky466/CGLib-for-Android

 
 

Repository files navigation

CGLib-for-Android

fork from https://github.com/leo-ouyang/CGLib-for-Android and fix some bugs

CGLib-for-Android (Dex code Generation Library for Android) is high level API to dynamicall generate sub class, its implementation based on DexMaker. This library use for AOP, method intercept, data access authorization authentication on Android.

Usage

The usage of CGLib-for-Android is similar with CGLib, but CGLib only supports java byte code, CGLib-for-Android designed for dex code which runs on Android.


1 Download cglib-for-android.jar to your project * [cglib-for-android.aar](https://github.com/bluesky466/CGLib-for-Android/blob/master/output/cglib-for-android.aar) (Right-click and Save as)
2 implementation cglib-for-android and dexmaker:
implementation files("libs/cglib-for-android.aar")
implementation 'com.linkedin.dexmaker:dexmaker:2.28.3'

3 Define the bussiness class which will be proxied in future ```Java public class Printer {
public void print() {
    Logger.d("Hello, world!");
}

}


<br>
4 Define proxy class, need to implement MethodInterceptor
```Java
public class MyProxy implements MethodInterceptor {
    
    private Context context;
    
    public MyProxy(Context context) {
        this.context = context;
    }
    
    public Object getProxy(Class cls) {
        Enhancer e = new Enhancer(context);
        e.setSuperclass(cls);
        e.setInterceptor(this);
        return e.create();
    }

    @Override
    public Object intercept(Object object, Object[] args, MethodProxy methodProxy) throws Exception {
        Logger.d("begin print");
        Object result = methodProxy.invokeSuper(object, args);
        Logger.d("end print");
        return result;
    }

}

5 Get printer and run ```Java Printer printer = (Printer) new MyProxy(this).getProxy(Printer.class); printer.print(); ```

Logcat output
[main:MyProxy.java:26 intercept]begin print
[main:Printer.java:6 print]Hello, world!
[main:MyProxy.java:28 intercept]end print

About

CGLib-for-Android (Dex code Generation Library for Android) is high level API to dynamicall generate sub class, its implementation based on DexMaker. This library use for AOP, method intercept, data access authorization authentication on Android.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%