Skip to content

Commit

Permalink
fix: agp is unable to handle the classes under the default package co…
Browse files Browse the repository at this point in the history
…rrectly
  • Loading branch information
styluo committed Aug 19, 2024
1 parent 7af89f9 commit 911a8c3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/CustomDialog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import android.view.View

class CustomDialog {
private var view: View? = null

public fun setListener() {
view?.setOnClickListener {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.growingio.android.plugin.hook.TargetMethod
import com.growingio.android.plugin.util.ClassContextCompat
import com.growingio.android.plugin.util.info
import com.growingio.android.plugin.util.unNormalize
import com.growingio.android.plugin.util.normalize
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.Handle
import org.objectweb.asm.MethodVisitor
Expand All @@ -42,6 +43,7 @@ class DesugarClassVisitor(
private val generateMethodBlocks = hashMapOf<String, GenerateMethodBlock>()
private val needInjectTargetMethods = hashSetOf<TargetMethod>()
private var generateMethodIndex = 0
private lateinit var realClassName: String

override fun visit(
version: Int,
Expand All @@ -52,6 +54,7 @@ class DesugarClassVisitor(
interfaces: Array<out String>?
) {
super.visit(version, access, name, signature, superName, interfaces)
realClassName = if (name != null) normalize(name) else className
}

override fun visitMethod(
Expand Down Expand Up @@ -208,7 +211,7 @@ class DesugarClassVisitor(
}
}

if (handle.owner == className.unNormalize()) {
if (handle.owner == realClassName.unNormalize()) {

// 实现方法在当前类中
// 示例参考1: 在类中生成相应的方法如 LambdaSample#print0 ==> 生成 lambda$print0$0,将其加入 injectMethods 中
Expand Down Expand Up @@ -255,7 +258,7 @@ class DesugarClassVisitor(
}
newArgs[1] = Handle(
H_INVOKESTATIC,
className.unNormalize(),
realClassName.unNormalize(),
methodBlock.methodName,
methodBlock.methodDesc,
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ internal class GioKitCodeVisitor(

private val generatedMethods = hashSetOf<String>()

private lateinit var realClassName: String

init {
val configMethods = hashSetOf<String>()
trackerCalledMethods.forEach {
Expand All @@ -59,6 +61,7 @@ internal class GioKitCodeVisitor(
interfaces: Array<out String>?
) {
super.visit(version, access, name, signature, superName, interfaces)
realClassName = if (name != null) normalize(name) else context.className
}

override fun visitMethod(
Expand Down Expand Up @@ -124,7 +127,7 @@ internal class GioKitCodeVisitor(
}
}
codeSet.removeIf {
it.startsWith(context.className + "::")
it.startsWith("$realClassName::")
}
generatedMethods.forEach {
codeSet.add(it)
Expand Down Expand Up @@ -165,9 +168,9 @@ internal class GioKitCodeVisitor(
else it
}
calledMethods.filter { owner2 == it.first.unNormalize() && name2 == it.second }.forEach { _ ->
g("find ${context.className} :: ${name}")
generatedMethods.add("${context.className}::${gName}")
g("[generate index]:$index:${context.className} :: ${name}")
g("find $realClassName :: $name")
generatedMethods.add("$realClassName::$gName")
g("[generate index]:$index:$realClassName :: $name")
index += 1
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.growingio.android.plugin.giokit

import com.growingio.android.plugin.util.ClassContextCompat
import com.growingio.android.plugin.util.normalize
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.MethodVisitor

Expand All @@ -16,6 +17,20 @@ internal class GioKitInjectVisitor(
private val giokitParams: GioKitParams
) : ClassVisitor(api, ncv), ClassContextCompat by classContext {

private lateinit var realClassName: String

override fun visit(
version: Int,
access: Int,
name: String?,
signature: String?,
superName: String?,
interfaces: Array<out String>?
) {
super.visit(version, access, name, signature, superName, interfaces)
realClassName = if (name != null) normalize(name) else classContext.className
}

override fun visitMethod(
access: Int,
methodName: String,
Expand All @@ -24,7 +39,7 @@ internal class GioKitInjectVisitor(
exceptions: Array<out String>?
): MethodVisitor {
val mv = super.visitMethod(access, methodName, descriptor, signature, exceptions)
val data = GioKitInjectData.matchGioKitData(classContext.className, methodName, descriptor) ?: return mv
val data = GioKitInjectData.matchGioKitData(realClassName, methodName, descriptor) ?: return mv
return when (data) {
GioKitInjectData.GioKitInjectInit -> {
val config = hashMapOf<String, Any>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ internal abstract class AutoTrackerFactory :

override fun createClassVisitor(classContext: ClassContext, nextClassVisitor: ClassVisitor): ClassVisitor {
val classContextCompat = object : ClassContextCompat {
// AsmInstrumentationManager#instrumentClassesFromDirectoryToDirectory 移除文件名方法无包名(相对路径为当前路径)时不生效
// 非日志输出时,尽可能使用visit方法参数中返回的name
override val className = classContext.currentClassData.className

override fun isAssignable(subClazz: String, superClazz: String): Boolean {
Expand Down

0 comments on commit 911a8c3

Please sign in to comment.