FlyJingFish/AndroidAOP

如何拦截三方库的方法使用自己的实现

svran opened this issue · 7 comments

svran commented

三方库:

public class LocaleTransform {
    public static Locale getLanguage(int language) {
    }
}

如果我要修改三方库的 " java.util.Locale getLanguage(int) " 方法, 需要怎么操作? 修改后自己的逻辑代码写在哪儿?
我没看懂文档的描述.

说实话文档已经写的很清楚了,非得喂到嘴里吗

@AndroidAopMatchClassMethod(
        targetClassName = "LocaleTransform的包名.LocaleTransform",
        methodName = ["getLanguage"],
        type = MatchType.SELF
)
class MatchLocaleTransform : MatchClassMethod {
  override fun invoke(joinPoint: ProceedJoinPoint, methodName: String): Any? {
    Log.e("MatchLocaleTransform","======"+methodName);
    return joinPoint.proceed()
  }
}
plugins {
    ...
}
androidAopConfig {
    // enabled 为 false 切面不再起作用,默认不写为 true
    enabled true 
    // include 不设置默认全部扫描,设置后只扫描设置的包名的代码
    include '你项目的包名','自定义module的包名','自定义module的包名'
    // exclude 是扫描时排除的包
    // 可排除 kotlin 相关,提高速度
    exclude 'kotlin.jvm', 'kotlin.internal'
    exclude 'kotlinx.coroutines.internal', 'kotlinx.coroutines.android'
}
android {
    ...
}

如果你设置了 include 记得把你这个 MatchLocaleTransform这个类的包名也算进去,否则没效果

svran commented
                              Process: com.xxx, PID: 5895
                              java.lang.VerifyError: Verifier rejected class com.luck.picture.lib.language.LocaleTransform: java.util.Locale com.luck.picture.lib.language.LocaleTransform.getLanguage(int) failed to verify: java.util.Locale com.luck.picture.lib.language.LocaleTransform.getLanguage(int): [0x2B] returning 'Reference: java.lang.Object', but expected from declaration 'Precise Reference: java.util.Locale' (declaration of 'com.luck.picture.lib.language.LocaleTransform' appears in /data/app/com.xxx-1/base.apk:classes2.dex)
                              	at com.luck.picture.lib.language.LocaleTransform.getLanguage(LocaleTransform.java)
                              	at com.luck.picture.lib.language.PictureLanguageUtils.setAppLanguage(PictureLanguageUtils.java:39)
                              	at com.luck.picture.lib.basic.PictureContextWrapper.wrap(PictureContextWrapper.java:22)
                              	at com.luck.picture.lib.basic.PictureSelectorSupporterActivity.attachBaseContext(PictureSelectorSupporterActivity.java:82)
                              	at android.app.Activity.attach(Activity.java:6637)
                              	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2632)
                              	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2759)
                              	at android.app.ActivityThread.-wrap12(ActivityThread.java)
                              	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
                              	at android.os.Handler.dispatchMessage(Handler.java:102)
                              	at android.os.Looper.loop(Looper.java:154)
                              	at android.app.ActivityThread.main(ActivityThread.java:6205)
                              	at java.lang.reflect.Method.invoke(Native Method)
                              	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
                              	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)

存在闪退情况.

@AndroidAopMatchClassMethod(
    targetClassName = "com.luck.picture.lib.language.LocaleTransform",
    methodName = ["getLanguage"],
    type = MatchType.SELF
)
class MatchLocaleTransform : MatchClassMethod {
    override fun invoke(joinPoint: ProceedJoinPoint, methodName: String): Any? {
        Log.e("MatchLocaleTransform", "======" + methodName);
        return joinPoint.proceed()
    }
}

看来应该不是文档我没看懂. 我以为非基本数据类型返回值的拦截文档没给全.

报错就是报错,跟看没看懂有啥关系,你是问拦截后逻辑代码写在哪

svran commented

非常抱歉, 我把自定义AOP和拦截三方库的搞混了。 我刚才试了,wiki列出的数据类型能拦截,其他类型存在问题。

目前wiki给出的肯定没问题,其他引用类型,我测的时候多数是自定义数据,系统的引用类型还没试过,你这个方法中的返回类型接下来我会再次做下适配

最新版本1.1.4已经适配,你可以试一下