Skip to content

Commit db830cb

Browse files
committed
1、修复多个app模块织入配置混乱的问题
1 parent bb8f304 commit db830cb

File tree

7 files changed

+78
-50
lines changed

7 files changed

+78
-50
lines changed

android-aop-annotation/src/main/java/com/flyjingfish/android_aop_annotation/AndroidAopJoinPoint.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,11 @@ private void getTargetMethod(){
299299
try {
300300
targetMethod = tClass.getDeclaredMethod(targetMethodName, classes);
301301
} catch (NoSuchMethodException exc) {
302-
Method oriMethod = Utils.INSTANCE.findMethodWithKeepName(tClass,targetMethodName,classes);
303-
if (oriMethod == null){
304-
String realMethodName = getRealMethodName(targetMethodName);
305-
if (realMethodName == null){
306-
throw new RuntimeException(exc);
307-
}
308-
oriMethod = tClass.getDeclaredMethod(realMethodName, classes);
302+
Method tarMethod = Utils.INSTANCE.findMethodWithKeepName(tClass,targetMethodName,classes);
303+
if (tarMethod == null){
304+
throw new RuntimeException(exc);
309305
}
310-
targetMethod = oriMethod;
306+
targetMethod = tarMethod;
311307
}
312308
try {
313309
originalMethod = tClass.getDeclaredMethod(originalMethodName, classes);

android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/plugin/CompilePlugin.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.flyjingfish.android_aop_plugin.scanner_visitor.SuspendReturnScanner
1313
import com.flyjingfish.android_aop_plugin.scanner_visitor.WovenIntoCode
1414
import com.flyjingfish.android_aop_plugin.tasks.CompileAndroidAopTask
1515
import com.flyjingfish.android_aop_plugin.tasks.DebugModeFileTask
16+
import com.flyjingfish.android_aop_plugin.utils.AppClasses
1617
import com.flyjingfish.android_aop_plugin.utils.ClassFileUtils
1718
import com.flyjingfish.android_aop_plugin.utils.ClassPoolUtils
1819
import com.flyjingfish.android_aop_plugin.utils.FileHashUtils
@@ -171,10 +172,17 @@ class CompilePlugin(private val fromRootSet:Boolean): BasePlugin() {
171172
project.rootProject.gradle.taskGraph.addTaskExecutionGraphListener(object :
172173
TaskExecutionGraphListener {
173174
override fun graphPopulated(it: TaskExecutionGraph) {
175+
val appTask = it.allTasks.firstOrNull {
176+
val project = it.project
177+
val isApp = project.plugins.hasPlugin(AppPlugin::class.java)
178+
isApp
179+
}
174180
startTask = true
175-
AndroidAopConfig.syncConfig(project)
176-
if (AndroidAopConfig.cutInfoJson){
177-
InitConfig.initCutInfo(runtimeProject,true)
181+
appTask?.let {
182+
AndroidAopConfig.syncConfig(it.project)
183+
if (AndroidAopConfig.cutInfoJson){
184+
InitConfig.initCutInfo(runtimeProject,true)
185+
}
178186
}
179187
project.rootProject.gradle.taskGraph.removeTaskExecutionGraphListener(this)
180188
}

android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/plugin/TransformPlugin.kt

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.android.build.gradle.internal.tasks.DexArchiveBuilderTask
77
import com.flyjingfish.android_aop_plugin.config.AndroidAopConfig
88
import com.flyjingfish.android_aop_plugin.tasks.AssembleAndroidAopTask
99
import com.flyjingfish.android_aop_plugin.utils.AppClasses
10+
import com.flyjingfish.android_aop_plugin.utils.InitConfig
1011
import com.flyjingfish.android_aop_plugin.utils.RuntimeProject
1112
import io.github.flyjingfish.fast_transform.tasks.DefaultTransformTask
1213
import io.github.flyjingfish.fast_transform.toTransformAll
@@ -30,9 +31,28 @@ object TransformPlugin : BasePlugin() {
3031
}
3132
return
3233
}
33-
if (isApp){
34-
project.rootProject.gradle.taskGraph.addTaskExecutionGraphListener {
35-
AppClasses.clearModuleNames()
34+
val androidAopConfig = project.extensions.getByType(AndroidAopConfig::class.java)
35+
36+
project.rootProject.gradle.taskGraph.addTaskExecutionGraphListener {
37+
for (task in it.allTasks) {
38+
val project = task.project
39+
val isDynamic = project.plugins.hasPlugin(DynamicFeaturePlugin::class.java)
40+
if (isDynamic && task is AssembleAndroidAopTask){
41+
task.doFirst {
42+
AndroidAopConfig.syncConfig(project)
43+
}
44+
}
45+
}
46+
val appTask = it.allTasks.firstOrNull {
47+
val project = it.project
48+
val isApp = project.plugins.hasPlugin(AppPlugin::class.java)
49+
isApp
50+
}
51+
appTask?.let { app ->
52+
AndroidAopConfig.syncConfig(app.project)
53+
}
54+
AppClasses.clearModuleNames()
55+
if (isApp){
3656
for (task in it.allTasks) {
3757
val project = task.project
3858
val isApp = project.plugins.hasPlugin(AppPlugin::class.java)
@@ -42,44 +62,40 @@ object TransformPlugin : BasePlugin() {
4262
}
4363
}
4464
try {
45-
var lastCanModifyTask : Task? =null
46-
var dexTask : DexArchiveBuilderTask? =null
47-
var aopTask : AssembleAndroidAopTask? =null
48-
for (task in it.allTasks) {
49-
if (task is AssembleAndroidAopTask){
50-
aopTask = task
51-
}
52-
if (task is DexArchiveBuilderTask){
53-
dexTask = task
54-
break
55-
}
56-
lastCanModifyTask = task
65+
val assembleAndroidAopTasks = it.allTasks.filter {
66+
it is AssembleAndroidAopTask
5767
}
58-
if (lastCanModifyTask != null && dexTask != null && aopTask != null){
59-
if (lastCanModifyTask !is AssembleAndroidAopTask && lastCanModifyTask !is DefaultTransformTask){
60-
if (aopTask.isFastDex){
61-
val hintText = "When fastDex is enabled, you should put [id 'android.aop'] at the end to make ${aopTask.name} execute after ${lastCanModifyTask.name}"
68+
69+
for ((index,aopTask) in assembleAndroidAopTasks.withIndex()) {
70+
val nextTask = it.allTasks[index+1]
71+
var lastCanModifyTask : Task? =null
72+
for (i in index+1 until it.allTasks.size) { // 0..list.size-1
73+
val task = it.allTasks[i]
74+
if (task is DexArchiveBuilderTask){
75+
break
76+
}
77+
lastCanModifyTask = task
78+
}
79+
if (aopTask is AssembleAndroidAopTask && aopTask.isFastDex && nextTask !is DexArchiveBuilderTask && lastCanModifyTask != null && lastCanModifyTask !is AssembleAndroidAopTask){
80+
val hintText = "When fastDex is enabled, you should put [id 'android.aop'] at the end to make ${aopTask.name} execute after ${lastCanModifyTask.name}"
81+
project.logger.error(hintText)
82+
aopTask.doLast {
83+
project.logger.error(hintText)
84+
}
85+
it.allTasks[it.allTasks.size - 1].doLast {
6286
project.logger.error(hintText)
63-
aopTask.doLast {
64-
project.logger.error(hintText)
65-
}
66-
it.allTasks[it.allTasks.size - 1].doLast {
67-
project.logger.error(hintText)
68-
}
6987
}
7088
}
7189
}
90+
7291
} catch (_: Exception) {
7392
}
7493
}
7594
}
7695
val androidComponents = project.extensions.getByType(AndroidComponentsExtension::class.java)
7796
androidComponents.onVariants { variant ->
7897
val runtimeProject = RuntimeProject.get(project)
79-
val androidAopConfig = project.extensions.getByType(AndroidAopConfig::class.java)
80-
if (isApp){
81-
androidAopConfig.initConfig()
82-
}
98+
8399
val buildTypeName = variant.buildType
84100
if (androidAopConfig.enabled && !isDebugMode(buildTypeName,variant.name)){
85101
val fastDex = isFastDex(buildTypeName,variant.name)

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ include ':android-aop-plugin'
3232
include ':android-aop-core'
3333
include ':android-aop-apt'
3434
include ':android-aop-extra'
35-
//include ':test-app'
35+
include ':test-app'
3636
include ':test-java-lib'
3737
//include ':sub-modules'
3838
//include ':sub-modules:sub1'

test-app/build.gradle

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ androidAopConfig {
1313
// debug 为true打开日志方便调试
1414
debug true
1515
// 测试
16-
include 'com.flyjingfish.test'
16+
include 'com.flyjingfish.test_app'
1717
//默认关闭,开启后将会生成切点信息json文件在 /build/tmp/cutInfo.json
1818
cutInfoJson true
1919
increment true
2020
// 移除kotlin相关,编译错误和提升速度
2121
// exclude 'kotlin.jvm', 'kotlin.internal','kotlinx.coroutines.internal', 'kotlinx.coroutines.android'
22+
excludePackaging 'META-INF/DEPENDENCIES', 'META-INF/NOTICE' , 'META-INF/LICENSE' , 'META-INF/LICENSE.txt' , 'META-INF/NOTICE.txt' , 'license/README.dom.txt' , 'license/LICENSE.dom-documentation.txt' , 'license/NOTICE' , 'license/LICENSE.dom-software.txt' , 'license/LICENSE'
2223
}
2324
android {
2425
namespace 'com.flyjingfish.test_app'
@@ -82,16 +83,23 @@ android {
8283

8384
packagingOptions {
8485
exclude 'META-INF/kotlinx_coroutines_core.version'
86+
exclude 'META-INF/DEPENDENCIES'
87+
exclude 'META-INF/NOTICE'
88+
exclude 'META-INF/LICENSE'
89+
exclude 'META-INF/LICENSE.txt'
90+
exclude 'META-INF/NOTICE.txt'
91+
exclude 'license/README.dom.txt'
92+
exclude 'license/LICENSE.dom-documentation.txt'
93+
exclude 'license/NOTICE'
94+
exclude 'license/LICENSE.dom-software.txt'
95+
exclude 'license/LICENSE'
8596
}
8697

8798
}
8899

89100
dependencies {
90-
implementation libs.core.ktx
91-
implementation libs.appcompat
92-
implementation libs.material
93101
implementation project(path: ":test-lib")
94-
if (TestType == "0"){
102+
if (TestType == "-1"){
95103
//本地
96104
ksp project(':android-aop-apt')
97105
// kapt project(':android-aop-apt')

test-app/src/main/java/com/flyjingfish/test_app/MainActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import com.flyjingfish.android_aop_core.annotations.Permission
1515
import com.flyjingfish.android_aop_core.annotations.SingleClick
1616
import com.flyjingfish.android_aop_core.annotations.TryCatch
1717
import com.flyjingfish.android_aop_core.enums.ThreadType
18-
import com.flyjingfish.androidaop.databinding.ActivityMainBinding
18+
import com.flyjingfish.test_app.databinding.ActivityMainBinding
1919
import com.flyjingfish.test_lib.LocaleTransform
2020
import com.flyjingfish.test_lib.PermissionRejectListener
2121
import com.flyjingfish.test_lib.TestMatch
@@ -28,7 +28,7 @@ import java.lang.Thread.sleep
2828

2929
class MainActivity: AppCompatActivity(), PermissionRejectListener{
3030
// val haha = 1
31-
lateinit var binding:ActivityMainBinding
31+
lateinit var binding: ActivityMainBinding
3232
override fun onCreate(savedInstanceState: Bundle?) {
3333
super.onCreate(savedInstanceState)
3434
binding = ActivityMainBinding.inflate(layoutInflater)

test-app/src/main/java/com/flyjingfish/test_app/MyTestApp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void onCreate() {
3838
AndroidAop.INSTANCE.setOnPermissionsInterceptListener(new OnPermissionsInterceptListener() {
3939
@SuppressLint("CheckResult")
4040
@Override
41-
public void requestPermission(@NonNull ProceedJoinPoint joinPoint, @NonNull Permission permission, @NonNull OnRequestPermissionListener call) {
41+
public void requestPermission(@NonNull ProceedJoinPoint joinPoint, @NonNull Permission permission, @NonNull OnRequestPermissionListener call) throws Throwable {
4242
Object target = joinPoint.getTarget();
4343
String[] permissions = permission.value();
4444
permissions = check13ReadExternalStorage(permissions);
@@ -70,7 +70,7 @@ public void requestPermission(@NonNull ProceedJoinPoint joinPoint, @NonNull Perm
7070
AndroidAop.INSTANCE.setOnCustomInterceptListener(new OnCustomInterceptListener() {
7171
@Nullable
7272
@Override
73-
public Object invoke(@NonNull ProceedJoinPoint joinPoint, @NonNull CustomIntercept customIntercept) {
73+
public Object invoke(@NonNull ProceedJoinPoint joinPoint, @NonNull CustomIntercept customIntercept) throws Throwable {
7474
// TODO: 2023/11/11 在此写你的逻辑 在合适的地方调用 joinPoint.proceed(),
7575
// joinPoint.proceed(args)可以修改方法传入的参数,如果需要改写返回值,则在 return 处返回即可
7676
// 不调用 proceed 就不会执行拦截切面方法内的代码

0 commit comments

Comments
 (0)