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 20, 2024
1 parent 7af89f9 commit 89f595d
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 11 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 Down Expand Up @@ -52,6 +53,7 @@ class DesugarClassVisitor(
interfaces: Array<out String>?
) {
super.visit(version, access, name, signature, superName, interfaces)
if (!name.isNullOrBlank()) className = normalize(name)
}

override fun visitMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package com.growingio.android.plugin.util
* @author cpacm 2023/8/18
*/
interface ClassContextCompat {
val className: String
var className: String

fun isAssignable(subClazz: String, superClazz: String): Boolean

Expand Down
4 changes: 2 additions & 2 deletions autotracker-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ buildscript {
set("kotlin_version", "1.8.20")
set("agp_version", "8.1.0")
set("low_agp_version", "4.2.2")
set("releaseVersion", "4.3.0")
set("releaseVersionCode", 40300)
set("releaseVersion", "4.3.1-SNAPSHOT")
set("releaseVersionCode", 40301)
}
}

Expand Down
4 changes: 2 additions & 2 deletions autotracker-gradle-plugin/saas-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
extra.apply {
set("saasVersion", "2.10.2")
set("saasVersionCode", "21002")
set("saasVersion", "2.10.3-SNAPSHOT")
set("saasVersionCode", "21003")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal abstract class SaasAutoTrackerFactory :

override fun createClassVisitor(classContext: ClassContext, nextClassVisitor: ClassVisitor): ClassVisitor {
val classContextCompat = object : ClassContextCompat {
override val className = classContext.currentClassData.className
override var className = classContext.currentClassData.className

override fun isAssignable(subClazz: String, superClazz: String): Boolean {
return classContext.loadClassData(normalize(subClazz))?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal class SaasAutoTrackerTransform(
val apiVersion = autoTrackerWriter.getApi()

val classContextCompat = object : ClassContextCompat {
override val className = classReader.className
override var className = classReader.className
override fun isAssignable(subClazz: String, superClazz: String): Boolean {
return context.klassPool.get(superClazz).isAssignableFrom(subClazz)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ internal class GioKitCodeVisitor(
interfaces: Array<out String>?
) {
super.visit(version, access, name, signature, superName, interfaces)
if (!name.isNullOrBlank()) context.className = normalize(name)
}

override fun visitMethod(
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,18 @@ internal class GioKitInjectVisitor(
private val giokitParams: GioKitParams
) : ClassVisitor(api, ncv), ClassContextCompat by classContext {

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)
if (!name.isNullOrBlank()) classContext.className = normalize(name)
}

override fun visitMethod(
access: Int,
methodName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ internal abstract class AutoTrackerFactory :

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

override fun isAssignable(subClazz: String, superClazz: String): Boolean {
return classContext.loadClassData(normalize(subClazz))?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ internal class AutoTrackerTransform(
val apiVersion = autoTrackerWriter.getApi()

val classContextCompat = object : ClassContextCompat {
override val className = classReader.className
override var className = classReader.className
override fun isAssignable(subClazz: String, superClazz: String): Boolean {
return context.klassPool.get(superClazz).isAssignableFrom(subClazz)
}
Expand Down
5 changes: 3 additions & 2 deletions autotracker-gradle-plugin/src/test/kotlin/GradleTestRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class GradleTestRunner(val tempFolder: TemporaryFolder) {
tempFolder.newFolder("src", "main", "java", "growingio")
tempFolder.newFolder("src", "test", "java", "growingio")
tempFolder.newFolder("src", "main", "res")
addDependencies("implementation 'com.growingio.android:autotracker:4.0.0-SNAPSHOT'")
addDependencies("implementation 'com.growingio.android:autotracker:4.3.0'")
}

// Adds project dependencies, e.g. "implementation <group>:<id>:<version>"
Expand Down Expand Up @@ -223,7 +223,8 @@ class GradleTestRunner(val tempFolder: TemporaryFolder) {
.withArguments(listOf("--stacktrace", "assembleDebug") + additionalTasks)
.withPluginClasspath()
.withDebug(true) // Add this line to enable attaching a debugger to the gradle test invocation
.forwardOutput()
.forwardStdError(System.err.writer())
.forwardStdOutput(System.out.writer())

// Data class representing a Gradle Test run result.
data class Result(
Expand Down

0 comments on commit 89f595d

Please sign in to comment.