简体   繁体   中英

Intercepting third party functions in android using AspectJ

I have been trying to intercept all the third party functions which are part of my application using aspectJ, but somehow am only able to intercept the functions declared by me and not the ones declared by third party libraries.

i am using this aspectJ gradle configuration referenced from this tutorial .

Here's what my aspect looks like :

private static final String POINTCUT_METHOD = "execution(* *(..))";

@Pointcut(POINTCUT_METHOD) 
public void methodAnnotatedWithDebugTrace() {}

@Around("methodAnnotatedWithDebugTrace()") 
public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) 
            throws Throwable { 
    // ...
}

Is there any way by which we can start intercepting third party functions as well ??

To paraphrase another answer that has been given multiple times :

You can only weave your own code

Basically Aspects with android only works at compile time and will usually weave your own code. If you're using existing code for which you don't have source (like the Android framework for example), the compiler won't have access to modify those. In your case, you can only catch when your code is accessing the third party library.

Meaning that if you want to intercept third party libraries you need to use "call(* *(..))" instead of "execution(* *(..))"

You can weave third party libraries' code with my gradle-aspectj plugin . It's possible because of Transform API, which handles all project sources, not only src/* packages, but jars/aars, sub-modules too. But beware using weaving of third party code, it may lead to unexpected behaviours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM