AndroidAOP 鏄笓灞炰簬 Android 绔?Aop 妗嗘灦锛屽彧闇€涓€涓敞瑙e氨鍙互璇锋眰鏉冮檺銆佸垏鎹㈢嚎绋嬨€佺姝㈠鐐广€佺洃娴嬬敓鍛藉懆鏈熺瓑绛夛紝鏈簱涓嶆槸鍩轰簬 AspectJ 瀹炵幇鐨?Aop锛屽綋鐒朵綘涔熷彲浠ュ畾鍒跺嚭灞炰簬浣犵殑 Aop 浠g爜
AndroidAOP - Github閾炬帴
鐗硅壊鍔熻兘
1銆佹湰搴撳唴缃簡寮€鍙戜腑甯哥敤鐨勪竴浜涘垏闈㈡敞瑙d緵浣犱娇鐢?/p>
2銆佹湰搴撴敮鎸佽浣犺嚜宸卞仛鍒囬潰锛岃娉曠畝鍗曟槗涓婃墜
3銆佹湰搴撳悓姝ユ敮鎸?Java 鍜?Kotlin 浠g爜
4銆佹湰搴撴敮鎸佸垏鍏ヤ笁鏂瑰簱
5銆佹湰搴撴敮鎸佸垏鐐规柟娉曚负 Lambda 琛ㄨ揪寮忕殑鎯呭喌
6銆佹湰搴撴敮鎸佺敓鎴愭墍鏈夊垏鐐逛俊鎭疛son鏂囦欢锛屾柟渚夸竴瑙堟墍鏈夊垏鐐逛綅缃湪姝ら厤缃?/p>
7銆佹湰搴撲笉鏄熀浜?AspectJ 瀹炵幇鐨勶紝缁囧叆浠g爜閲忔瀬灏戯紝渚靛叆鎬ф瀬浣?/strong>
鐐规涓嬭浇apk
鐗堟湰闄愬埗
鏈€浣嶨radle鐗堟湰锛?.0
鏈€浣嶴DK鐗堟湰锛歮inSdkVersion >= 21
浣跨敤姝ラ
鍦ㄥ紑濮嬩箣鍓嶅彲浠ョ粰椤圭洰涓€涓猄tar鍚楋紵闈炲父鎰熻阿锛屼綘鐨勬敮鎸佹槸鎴戝敮涓€鐨勫姩鍔涖€傛杩嶴tar鍜孖ssues!
涓€銆佸湪椤圭洰鏍圭洰褰曚笅鐨刡uild.gradle娣诲姞锛堝繀椤伙級
buildscript {
dependencies {
//蹇呴』椤?馃憞
classpath 'io.github.FlyJingFish.AndroidAop:android-aop-plugin:1.2.0'
}
}
plugins {
//闈炲繀椤婚」 馃憞锛屽鏋滈渶瑕佽嚜瀹氫箟鍒囬潰锛屽苟涓斾娇鐢?android-aop-ksp 杩欎釜搴撶殑璇濋渶瑕侀厤缃?锛屼笅杈圭増鏈彿鏍规嵁浣犻」鐩殑 Kotlin 鐗堟湰鍐冲畾
id 'com.google.devtools.ksp' version '1.8.0-1.0.9' apply false
}
Kotlin 鍜?KSP Github 鐨勫尮閰嶇増鏈彿鍒楄〃
浜屻€佸湪 app 鐨刡uild.gradle娣诲姞锛堟姝ヤ负蹇呴』椤癸級
鈿狅笍娉ㄦ剰锛氿煈嗘姝ヤ负蹇呴』椤桂煈?/h4>
//蹇呴』椤?馃憞
plugins {
...
id 'android.aop'//鏈€濂芥斁鍦ㄦ渶鍚庝竴琛?
}
涓夈€佸紩鍏ヤ緷璧栧簱
plugins {
//闈炲繀椤婚」 馃憞锛屽鏋滈渶瑕佽嚜瀹氫箟鍒囬潰锛屽苟涓斾娇鐢?android-aop-ksp 杩欎釜搴撶殑璇濋渶瑕侀厤缃?
id 'com.google.devtools.ksp'
}
dependencies {
//蹇呴』椤?馃憞
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-core:1.2.0'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-annotation:1.2.0'
//闈炲繀椤婚」 馃憞锛屽鏋滀綘鎯宠嚜瀹氫箟鍒囬潰闇€瑕佺敤鍒帮紝鈿狅笍鏀寔Java鍜孠otlin浠g爜鍐欑殑鍒囬潰
ksp 'io.github.FlyJingFish.AndroidAop:android-aop-ksp:1.2.0'
//闈炲繀椤婚」 馃憞锛屽鏋滀綘鎯宠嚜瀹氫箟鍒囬潰闇€瑕佺敤鍒帮紝鈿狅笍鍙€傜敤浜嶫ava浠g爜鍐欑殑鍒囬潰
annotationProcessor 'io.github.FlyJingFish.AndroidAop:android-aop-processor:1.2.0'
//鈿狅笍涓婅竟鐨?android-aop-ksp 鍜?android-aop-processor 浜岄€変竴
}
//蹇呴』椤?馃憞
plugins {
...
id 'android.aop'//鏈€濂芥斁鍦ㄦ渶鍚庝竴琛?
}
plugins {
//闈炲繀椤婚」 馃憞锛屽鏋滈渶瑕佽嚜瀹氫箟鍒囬潰锛屽苟涓斾娇鐢?android-aop-ksp 杩欎釜搴撶殑璇濋渶瑕侀厤缃?
id 'com.google.devtools.ksp'
}
dependencies {
//蹇呴』椤?馃憞
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-core:1.2.0'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-annotation:1.2.0'
//闈炲繀椤婚」 馃憞锛屽鏋滀綘鎯宠嚜瀹氫箟鍒囬潰闇€瑕佺敤鍒帮紝鈿狅笍鏀寔Java鍜孠otlin浠g爜鍐欑殑鍒囬潰
ksp 'io.github.FlyJingFish.AndroidAop:android-aop-ksp:1.2.0'
//闈炲繀椤婚」 馃憞锛屽鏋滀綘鎯宠嚜瀹氫箟鍒囬潰闇€瑕佺敤鍒帮紝鈿狅笍鍙€傜敤浜嶫ava浠g爜鍐欑殑鍒囬潰
annotationProcessor 'io.github.FlyJingFish.AndroidAop:android-aop-processor:1.2.0'
//鈿狅笍涓婅竟鐨?android-aop-ksp 鍜?android-aop-processor 浜岄€変竴
}
鎻愮ず锛歬sp 鎴?annotationProcessor鍙槸鍦ㄥ綋鍓?module 璧蜂綔鐢紝鍦ㄥ摢涓?module 涓湁鑷畾涔夊垏闈唬鐮佸氨鍔犲湪鍝釜 module锛屽繀椤讳緷璧栭」鍙互閫氳繃 api 鏂瑰紡鍙姞鍒板叕鍏?module 涓?/strong>
鍥涖€佸湪 app 鐨刡uild.gradle娣诲姞 androidAopConfig 閰嶇疆椤癸紙姝ゆ涓哄彲閫夐厤缃」锛?/h4>
plugins {
...
}
androidAopConfig {
// enabled 涓?false 鍒囬潰涓嶅啀璧蜂綔鐢紝榛樿涓嶅啓涓?true
enabled true
// include 涓嶈缃粯璁ゅ叏閮ㄦ壂鎻忥紝璁剧疆鍚庡彧鎵弿璁剧疆鐨勫寘鍚嶇殑浠g爜
include '浣犻」鐩殑鍖呭悕','鑷畾涔塵odule鐨勫寘鍚?,'鑷畾涔塵odule鐨勫寘鍚?
// exclude 鏄壂鎻忔椂鎺掗櫎鐨勫寘
// 鍙帓闄?kotlin 鐩稿叧锛屾彁楂橀€熷害
exclude 'kotlin.jvm', 'kotlin.internal','kotlinx.coroutines.internal', 'kotlinx.coroutines.android'
// verifyLeafExtends 鏄惁寮€鍚獙璇佸彾瀛愮户鎵匡紝榛樿鎵撳紑锛屽鏋滄病鏈夎缃?@AndroidAopMatchClassMethod 鐨?type = MatchType.LEAF_EXTENDS锛屽彲浠ュ叧闂?
verifyLeafExtends true
//榛樿鍏抽棴锛屽紑鍚湪 Build 鎴?鎵撳寘鍚?灏嗕細鐢熸垚鍒囩偣淇℃伅json鏂囦欢鍦?app/build/tmp/cutInfo.json
cutInfoJson false
}
android {
...
}
plugins {
...
}
androidAopConfig {
// enabled 涓?false 鍒囬潰涓嶅啀璧蜂綔鐢紝榛樿涓嶅啓涓?true
enabled true
// include 涓嶈缃粯璁ゅ叏閮ㄦ壂鎻忥紝璁剧疆鍚庡彧鎵弿璁剧疆鐨勫寘鍚嶇殑浠g爜
include '浣犻」鐩殑鍖呭悕','鑷畾涔塵odule鐨勫寘鍚?,'鑷畾涔塵odule鐨勫寘鍚?
// exclude 鏄壂鎻忔椂鎺掗櫎鐨勫寘
// 鍙帓闄?kotlin 鐩稿叧锛屾彁楂橀€熷害
exclude 'kotlin.jvm', 'kotlin.internal','kotlinx.coroutines.internal', 'kotlinx.coroutines.android'
// verifyLeafExtends 鏄惁寮€鍚獙璇佸彾瀛愮户鎵匡紝榛樿鎵撳紑锛屽鏋滄病鏈夎缃?@AndroidAopMatchClassMethod 鐨?type = MatchType.LEAF_EXTENDS锛屽彲浠ュ叧闂?
verifyLeafExtends true
//榛樿鍏抽棴锛屽紑鍚湪 Build 鎴?鎵撳寘鍚?灏嗕細鐢熸垚鍒囩偣淇℃伅json鏂囦欢鍦?app/build/tmp/cutInfo.json
cutInfoJson false
}
android {
...
}
鎻愮ず锛氬悎鐞嗕娇鐢?include 鍜?exclude 鍙彁楂樼紪璇戦€熷害锛屽缓璁洿鎺ヤ娇鐢?include 璁剧疆浣犻」鐩殑鐩稿叧鍖呭悕锛堝寘鎷?app 鍜岃嚜瀹氫箟 module 鐨勶級
鍙﹀璁剧疆姝ゅ涔嬪悗鐢变簬 Android Studio 鍙兘鏈夌紦瀛橈紝寤鸿閲嶅惎 AS 骞?clean 涓嬮」鐩啀缁х画寮€鍙?/strong>
鏈簱鍐呯疆浜嗕竴浜涘姛鑳芥敞瑙e彲渚涗綘鐩存帴浣跨敤
娉ㄨВ鍚嶇О | 鍙傛暟璇存槑 | 鍔熻兘璇存槑 |
---|---|---|
@SingleClick | value = 蹇€熺偣鍑荤殑闂撮殧锛岄粯璁?000ms | 鍗曞嚮娉ㄨВ锛屽姞鍏ユ娉ㄨВ锛屽彲浣夸綘鐨勬柟娉曞彧鏈夊崟鍑绘椂鎵嶅彲杩涘叆 |
@DoubleClick | value = 涓ゆ鐐瑰嚮鐨勬渶澶х敤鏃讹紝榛樿300ms | 鍙屽嚮娉ㄨВ锛屽姞鍏ユ娉ㄨВ锛屽彲浣夸綘鐨勬柟娉曞弻鍑绘椂鎵嶅彲杩涘叆 |
@IOThread | ThreadType = 绾跨▼绫诲瀷 | 鍒囨崲鍒板瓙绾跨▼鐨勬搷浣滐紝鍔犲叆姝ゆ敞瑙e彲浣夸綘鐨勬柟娉曞唴鐨勪唬鐮佸垏鎹㈠埌瀛愮嚎绋嬫墽琛?/td> |
@MainThread | 鏃犲弬鏁?/td> | 鍒囨崲鍒颁富绾跨▼鐨勬搷浣滐紝鍔犲叆姝ゆ敞瑙e彲浣夸綘鐨勬柟娉曞唴鐨勪唬鐮佸垏鎹㈠埌涓荤嚎绋嬫墽琛?/td> |
@OnLifecycle | value = Lifecycle.Event | 鐩戝惉鐢熷懡鍛ㄦ湡鐨勬搷浣滐紝鍔犲叆姝ゆ敞瑙e彲浣夸綘鐨勬柟娉曞唴鐨勪唬鐮佸湪瀵瑰簲鐢熷懡鍛ㄦ湡鍐呮墠鍘绘墽琛?/td> |
@TryCatch | value = 浣犺嚜瀹氫箟鍔犵殑涓€涓猣lag | 鍔犲叆姝ゆ敞瑙e彲涓烘偍鐨勬柟娉曞寘瑁逛竴灞?try catch 浠g爜 |
@Permission | value = 鏉冮檺鐨勫瓧绗︿覆鏁扮粍 | 鐢宠鏉冮檺鐨勬搷浣滐紝鍔犲叆姝ゆ敞瑙e彲浣挎偍鐨勪唬鐮佸湪鑾峰彇鏉冮檺鍚庢墠鎵ц |
@Scheduled | initialDelay = 寤惰繜寮€濮嬫椂闂?br>interval = 闂撮殧 repeatCount = 閲嶅娆℃暟 isOnMainThread = 鏄惁涓荤嚎绋?br>id = 鍞竴鏍囪瘑 |
瀹氭椂浠诲姟锛屽姞鍏ユ娉ㄨВ锛屽彲浣夸綘鐨勬柟娉曟瘡闅斾竴娈垫椂闂存墽琛屼竴娆★紝璋冪敤AndroidAop.shutdownNow(id)鎴朅ndroidAop.shutdown(id)鍙仠姝?/td> |
@Delay | delay = 寤惰繜鏃堕棿 isOnMainThread = 鏄惁涓荤嚎绋?br>id = 鍞竴鏍囪瘑 |
寤惰繜浠诲姟锛屽姞鍏ユ娉ㄨВ锛屽彲浣夸綘鐨勬柟娉曞欢杩熶竴娈垫椂闂存墽琛岋紝璋冪敤AndroidAop.shutdownNow(id)鎴朅ndroidAop.shutdown(id)鍙彇娑?/td> |
@CustomIntercept | value = 浣犺嚜瀹氫箟鍔犵殑涓€涓瓧绗︿覆鏁扮粍鐨刦lag | 鑷畾涔夋嫤鎴紝閰嶅悎 AndroidAop.setOnCustomInterceptListener 浣跨敤锛屽睘浜庝竾閲戞补 |
涓婅堪娉ㄨВ浣跨敤绀轰緥閮藉湪杩?杩樻湁杩?/p>
杩欏潡寮鸿皟涓€涓?@OnLifecycle
- 1銆丂OnLifecycle 鍔犲埌鐨勬柟娉曟墍灞炲璞″繀椤绘槸灞炰簬鐩存帴鎴栭棿鎺ョ户鎵胯嚜 FragmentActivity 鎴?Fragment鐨勬柟娉曟墠鏈夌敤锛屾垨鑰呮敞瑙f柟娉曠殑瀵硅薄瀹炵幇 LifecycleOwner 涔熷彲浠?/strong>
- 2銆佸鏋滅1鐐逛笉绗﹀悎鐨勬儏鍐典笅锛屽彲浠ョ粰鍒囬潰鏂规硶绗竴涓弬鏁拌缃负绗?鐐圭殑绫诲瀷锛屽湪璋冪敤鍒囬潰鏂规硶浼犲叆涔熸槸鍙互鐨勶紝渚嬪锛?/li>
public class StaticClass {
@SingleClick(5000)
@OnLifecycle(Lifecycle.Event.ON_RESUME)
public static void onStaticPermission(MainActivity activity, int maxSelect , ThirdActivity.OnPhotoSelectListener back){
back.onBack();
}
}
涓嬮潰鍐嶇潃閲嶄粙缁嶄笅 @TryCatch @Permission @CustomIntercept
- @TryCatch 浣跨敤姝ゆ敞瑙d綘鍙互璁剧疆浠ヤ笅璁剧疆锛堥潪蹇呴』锛?/li>
AndroidAop.INSTANCE.setOnThrowableListener(new OnThrowableListener() {
@Nullable
@Override
public Object handleThrowable(@NonNull String flag, @Nullable Throwable throwable,TryCatch tryCatch) {
// TODO: 2023/11/11 鍙戠敓寮傚父鍙牴鎹綘褰撴椂浼犲叆鐨刦lag浣滃嚭鐩稿簲澶勭悊锛屽鏋滈渶瑕佹敼鍐欒繑鍥炲€硷紝鍒欏湪 return 澶勮繑鍥炲嵆鍙?
return 3;
}
});
- @Permission 浣跨敤姝ゆ敞瑙e繀椤婚厤鍚堜互涓嬭缃紙鈿狅笍姝ゆ涓哄繀椤昏缃殑锛屽惁鍒欐槸娌℃晥鏋滅殑锛?/li>
AndroidAop.INSTANCE.setOnPermissionsInterceptListener(new OnPermissionsInterceptListener() {
@SuppressLint("CheckResult")
@Override
public void requestPermission(@NonNull ProceedJoinPoint joinPoint, @NonNull Permission permission, @NonNull OnRequestPermissionListener call) {
Object target = joinPoint.getTarget();
if (target instanceof FragmentActivity){
RxPermissions rxPermissions = new RxPermissions((FragmentActivity) target);
rxPermissions.request(permission.value()).subscribe(call::onCall);
}else if (target instanceof Fragment){
RxPermissions rxPermissions = new RxPermissions((Fragment) target);
rxPermissions.request(permission.value()).subscribe(call::onCall);
}else{
// TODO: target 涓嶆槸 FragmentActivity 鎴?Fragment 锛岃鏄庢敞瑙f墍鍦ㄦ柟娉曚笉鍦ㄥ叾涓紝璇疯嚜琛屽鐞嗚繖绉嶆儏鍐?
// 寤鸿锛氬垏鐐规柟娉曠涓€涓弬鏁板彲浠ヨ缃负 FragmentActivity 鎴?Fragment 锛岀劧鍚?joinPoint.args[0] 灏卞彲浠ユ嬁鍒?
}
}
});
- @CustomIntercept 浣跨敤姝ゆ敞瑙d綘蹇呴』閰嶅悎浠ヤ笅璁剧疆锛堚殸锔忔姝ヤ负蹇呴』璁剧疆鐨勶紝鍚﹀垯杩樻湁浠€涔堟剰涔夊憿锛燂級
AndroidAop.INSTANCE.setOnCustomInterceptListener(new OnCustomInterceptListener() {
@Nullable
@Override
public Object invoke(@NonNull ProceedJoinPoint joinPoint, @NonNull CustomIntercept customIntercept) {
// TODO: 2023/11/11 鍦ㄦ鍐欎綘鐨勯€昏緫 鍦ㄥ悎閫傜殑鍦版柟璋冪敤 joinPoint.proceed()锛?
// joinPoint.proceed(args)鍙互淇敼鏂规硶浼犲叆鐨勫弬鏁帮紝濡傛灉闇€瑕佹敼鍐欒繑鍥炲€硷紝鍒欏湪 return 澶勮繑鍥炲嵆鍙?
return null;
}
});
馃憜涓婅竟涓変釜鐩戝惉锛屾渶濂芥斁鍒颁綘鐨?application 涓?/p>
姝ゅ鏈簱涔熷悓鏍锋敮鎸佽浣犺嚜宸卞仛鍒囬潰锛屽疄鐜拌捣鏉ラ潪甯哥畝鍗曪紒
鏈簱閫氳繃 @AndroidAopPointCut 鍜?@AndroidAopMatchClassMethod 涓ょ娉ㄨВ锛屽疄鐜拌嚜瀹氫箟鍒囬潰
涓€銆?strong>@AndroidAopPointCut 鏄湪鏂规硶涓婇€氳繃娉ㄨВ鐨勫舰寮忓仛鍒囬潰鐨勶紝涓婅堪涓敞瑙i兘鏄€氳繃杩欎釜鍋氱殑锛岃缁嗕娇鐢ㄨ鐪媤iki鏂囨。
鈿狅笍娉ㄦ剰锛氳嚜瀹氫箟鐨勬敞瑙o紙涔熷氨鏄 @AndroidAopPointCut 娉ㄨВ鐨勬敞瑙g被锛夊鏋滄槸 Kotlin 浠g爜璇风敤 android-aop-ksp 閭d釜搴?/p>
涓嬮潰浠?@CustomIntercept 涓轰緥浠嬬粛涓嬭濡備綍浣跨敤
- 鍒涘缓娉ㄨВ
@AndroidAopPointCut(CustomInterceptCut.class)
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomIntercept {
String[] value() default {};
}
- 鍒涘缓娉ㄨВ澶勭悊鍒囬潰鐨勭被锛堥渶瑕佸疄鐜?BasePointCut 鎺ュ彛锛屽畠鐨勬硾鍨嬪~涓婅竟鐨勬敞瑙o級
class CustomInterceptCut : BasePointCut<CustomIntercept> {
override fun invoke(
joinPoint: ProceedJoinPoint,
annotation: CustomIntercept //annotation灏辨槸浣犲姞鍒版柟娉曚笂鐨勬敞瑙?
): Any{
// 鍦ㄦ鍐欎綘鐨勯€昏緫
// joinPoint.proceed() 琛ㄧず缁х画鎵ц鍒囩偣鏂规硶鐨勯€昏緫锛屼笉璋冪敤姝ゆ柟娉曚笉浼氭墽琛屽垏鐐规柟娉曢噷杈圭殑浠g爜
// 鍏充簬 ProceedJoinPoint 鍙互鐪媤iki 鏂囨。锛岃缁嗙偣鍑讳笅鏂归摼鎺?
return joinPoint.proceed()
}
}
鍏充簬 ProceedJoinPoint 浣跨敤璇存槑锛屼笅鏂囩殑 ProceedJoinPoint 鍚岀悊
- 浣跨敤
鐩存帴灏嗕綘鍐欑殑娉ㄨВ鍔犲埌浠绘剰涓€涓柟娉曚笂锛屼緥濡傚姞鍒颁簡 onCustomIntercept() 褰?onCustomIntercept() 琚皟鐢ㄦ椂棣栧厛浼氳繘鍏ュ埌涓婃枃鎻愬埌鐨?CustomInterceptCut 鐨?invoke 鏂规硶涓?/p>
@CustomIntercept("鎴戞槸鑷畾涔夋暟鎹?)
fun onCustomIntercept(){
}
浜屻€?strong>@AndroidAopMatchClassMethod 鏄仛鍖归厤鏌愮被鍙婂叾瀵瑰簲鏂规硶鐨勫垏闈㈢殑
鍖归厤鏂规硶鏀寔绮惧噯鍖归厤锛岀偣姝ょ湅wiki璇︾粏浣跨敤鏂囨。
鈿狅笍娉ㄦ剰锛氳嚜瀹氫箟鐨勫尮閰嶇被鏂规硶鍒囬潰锛堜篃灏辨槸琚?@AndroidAopMatchClassMethod 娉ㄨВ鐨勪唬鐮侊級濡傛灉鏄?Kotlin 浠g爜璇风敤 android-aop-ksp 閭d釜搴?/p>
- 渚嬪瓙涓€
package com.flyjingfish.test_lib;
public class TestMatch {
public void test1(int value1,String value2){
}
public String test2(int value1,String value2){
return value1+value2;
}
}
鍋囧 TestMatch 鏄鍖归厤鐨勭被锛岃€屼綘鎯宠鍖归厤鍒?test2 杩欎釜鏂规硶锛屼笅杈规槸鍖归厤鍐欐硶锛?/p>
package com.flyjingfish.test_lib.mycut;
@AndroidAopMatchClassMethod(
targetClassName = "com.flyjingfish.test_lib.TestMatch",
methodName = ["test2"],
type = MatchType.SELF
)
class MatchTestMatchMethod : MatchClassMethod {
override fun invoke(joinPoint: ProceedJoinPoint, methodName: String): Any{
Log.e("MatchTestMatchMethod","======"+methodName+",getParameterTypes="+joinPoint.getTargetMethod().getParameterTypes().length);
// 鍦ㄦ鍐欎綘鐨勯€昏緫
//涓嶆兂鎵ц鍘熸潵鏂规硶閫昏緫锛岎煈囧氨涓嶈皟鐢ㄤ笅杈硅繖鍙?
return joinPoint.proceed()
}
}
鍙互鐪嬪埌涓婃柟 AndroidAopMatchClassMethod 璁剧疆鐨?type 鏄?MatchType.SELF 琛ㄧず鍙尮閰?TestMatch 杩欎釜绫昏嚜韬紝涓嶈€冭檻鍏跺瓙绫?/p>
- 渚嬪瓙浜?/li>
鍋囧鎯?Hook 鎵€鏈夌殑 android.view.View.OnClickListener 鐨?onClick锛岃鐧戒簡灏辨槸鎯冲叏灞€鐩戞祴鎵€鏈夌殑璁剧疆 OnClickListener 鐨勭偣鍑讳簨浠讹紝浠g爜濡備笅锛?/p>
@AndroidAopMatchClassMethod(
targetClassName = "android.view.View.OnClickListener",
methodName = ["onClick"],
type = MatchType.EXTENDS //type 涓€瀹氭槸 EXTENDS 鍥犱负浣犳兂 hook 鎵€鏈夌户鎵夸簡 OnClickListener 鐨勭被
)
class MatchOnClick : MatchClassMethod {
// @SingleClick(5000) //鑱斿悎 @SingleClick 锛岀粰鎵€鏈夌偣鍑诲鍔犻槻澶氱偣锛?涓?
override fun invoke(joinPoint: ProceedJoinPoint, methodName: String): Any{
Log.e("MatchOnClick", "=====invoke=====$methodName")
return joinPoint.proceed()
}
}
鍙互鐪嬪埌涓婃柟 AndroidAopMatchClassMethod 璁剧疆鐨?type 鏄?MatchType.EXTENDS 琛ㄧず鍖归厤鎵€鏈夌户鎵胯嚜 OnClickListener 鐨勫瓙绫伙紝鍙﹀鏇村缁ф壙鏂瑰紡锛岃鍙傝€僕iki鏂囨。
鈿狅笍娉ㄦ剰锛氬鏋滃瓙绫绘病鏈夎鏂规硶锛屽垯鍒囬潰鏃犳晥锛屽彟澶栧鍚屼竴涓被鐨勫悓涓€涓柟娉曚笉瑕佸仛澶氭鍖归厤锛屽惁鍒欏彧鏈変竴涓細鐢熸晥
鍖归厤鍒囬潰瀹炵敤鍦烘櫙锛?/h4>
渚嬪浣犳兂鍋氶€€鍑虹櫥闄嗛€昏緫鏃跺彲浠ヤ娇鐢ㄤ笂杈硅繖涓紝鍙鍦ㄩ〉闈㈠唴璺宠浆灏卞彲浠ユ娴嬫槸鍚﹂渶瑕侀€€鍑虹櫥闄?/p>
鍙堟垨鑰呬綘鎯冲湪涓夋柟搴撴煇涓柟娉曚笂璁剧疆鍒囬潰锛屽彲浠ョ洿鎺ヨ缃搴旂被鍚嶏紝瀵瑰簲鏂规硶锛岀劧鍚?type = MatchType.SELF锛岃繖鏍峰彲浠ヤ镜鍏ヤ笁鏂瑰簱鐨勪唬鐮侊紝褰撶劧杩欎箞鍋氳寰椾慨鏀逛笂鏂囨彁鍒扮殑 androidAopConfig 鐨勯厤缃?/p>
璇︾粏浣跨敤璇风湅wiki鏂囨。
甯歌闂
渚嬪浣犳兂鍋氶€€鍑虹櫥闄嗛€昏緫鏃跺彲浠ヤ娇鐢ㄤ笂杈硅繖涓紝鍙鍦ㄩ〉闈㈠唴璺宠浆灏卞彲浠ユ娴嬫槸鍚﹂渶瑕侀€€鍑虹櫥闄?/p>
鍙堟垨鑰呬綘鎯冲湪涓夋柟搴撴煇涓柟娉曚笂璁剧疆鍒囬潰锛屽彲浠ョ洿鎺ヨ缃搴旂被鍚嶏紝瀵瑰簲鏂规硶锛岀劧鍚?type = MatchType.SELF锛岃繖鏍峰彲浠ヤ镜鍏ヤ笁鏂瑰簱鐨勪唬鐮侊紝褰撶劧杩欎箞鍋氳寰椾慨鏀逛笂鏂囨彁鍒扮殑 androidAopConfig 鐨勯厤缃?/p>
1銆丅uild鏃舵姤閿?"ZipFile invalid LOC header (bad signature)"
- 璇烽噸鍚疉ndroid Studio锛岀劧鍚?clean 椤圭洰
2銆?鍚屼竴涓柟娉曞瓨鍦ㄥ涓敞瑙f垨鍖归厤鍒囬潰鏃讹紝鎬庝箞澶勭悊鐨?/p>
- 澶氫釜鍒囬潰鍙犲姞鍒颁竴涓柟娉曚笂鏃舵敞瑙d紭鍏堜簬鍖归厤鍒囬潰锛堜笂鏂囩殑鍖归厤鍒囬潰锛夛紝娉ㄨВ鍒囬潰涔嬮棿浠庝笂鍒颁笅渚濇鎵ц
- 璋冪敤 proceed 鎵嶄細鎵ц涓嬩竴涓垏闈紝澶氫釜鍒囬潰涓渶鍚庝竴涓垏闈㈡墽琛?proceed 鎵嶄細璋冪敤鍒囧叆鏂规硶鍐呯殑浠g爜
- 鍦ㄥ墠杈瑰垏闈腑璋冪敤 proceed(args) 鍙洿鏂版柟娉曚紶鍏ュ弬鏁帮紝骞跺湪涓嬩竴涓垏闈腑涔熶細鎷垮埌涓婁竴灞傛洿鏂扮殑鍙傛暟
- 瀛樺湪寮傛璋冪敤proceed鏃讹紝绗竴涓紓姝ヨ皟鐢?proceed 鍒囬潰鐨勮繑鍥炲€硷紙灏辨槸 invoke 鐨勮繑鍥炲€硷級灏辨槸鍒囧叆鏂规硶鐨勮繑鍥炲€硷紱
娣锋穯瑙勫垯
涓嬭竟鏄秹鍙婂埌鏈簱鐨勪竴浜涘繀椤绘贩娣嗚鍒?/p>
# AndroidAop蹇呭娣锋穯瑙勫垯 -----start-----
-keep class * {
@androidx.annotation.Keep <fields>;
}
-keepnames class * implements com.flyjingfish.android_aop_annotation.base.BasePointCut
-keepnames class * implements com.flyjingfish.android_aop_annotation.base.MatchClassMethod
-keep class * implements com.flyjingfish.android_aop_annotation.base.BasePointCut{
public <init>();
}
-keep class * implements com.flyjingfish.android_aop_annotation.base.MatchClassMethod{
public <init>();
}
# AndroidAop蹇呭娣锋穯瑙勫垯 -----end-----