简体   繁体   English

使用AspectJ拦截android中的第三方功能

[英]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. 我一直在尝试使用AspectJ拦截属于我的应用程序的所有第三方功能,但是以某种方式只能拦截由我声明的功能,而不能拦截由第三方库声明的功能。

i am using this aspectJ gradle configuration referenced from this tutorial . 我正在使用本教程中引用的aspectJ gradle配置。

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. 基本上,使用Android的Aspects仅在编译时有效,并且通常会编织您自己的代码。 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. 如果您使用的是没有源代码的现有代码(例如Android框架),则编译器将无权修改这些代码。 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(* *(..))" 这意味着如果要拦截第三方库,则需要使用"call(* *(..))"而不是"execution(* *(..))"

You can weave third party libraries' code with my gradle-aspectj plugin . 您可以使用我的gradle-aspectj插件来编织第三方库的代码。 It's possible because of Transform API, which handles all project sources, not only src/* packages, but jars/aars, sub-modules too. 可能是因为Transform API可以处理所有项目源,不仅处理src/*包,还处理jar / aars和子模块。 But beware using weaving of third party code, it may lead to unexpected behaviours. 但是提防使用第三方代码的编织,这可能会导致意外的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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