1
+ package kotlinx.benchmark.gradle
2
+
3
+ import kotlinx.benchmark.gradle.internal.KotlinxBenchmarkPluginInternalApi
4
+ import org.gradle.api.*
5
+ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation
6
+ import org.jetbrains.org.objectweb.asm.*
7
+ import org.jetbrains.org.objectweb.asm.tree.*
8
+ import java.io.File
9
+
10
+ @KotlinxBenchmarkPluginInternalApi
11
+ fun Project.unpackAndProcessAar (target : KotlinJvmAndroidCompilation ) {
12
+ println (" Unpacking AAR file for ${target.name} " )
13
+ val aarFile = File (" ${project.projectDir} /build/outputs/aar/${project.name} -${target.name} .aar" )
14
+ if (aarFile.exists()) {
15
+ val unpackedDir = File (" ${project.projectDir} /build/outputs/unpacked-aar/${target.name} " )
16
+ val classesJar = File (unpackedDir, " classes.jar" )
17
+ val unzipDir = File (" ${project.projectDir} /build/outputs/unzipped-classes/${target.name} " )
18
+
19
+ // Unpack AAR file
20
+ project.copy {
21
+ it.from(project.zipTree(aarFile))
22
+ it.into(unpackedDir)
23
+ }
24
+
25
+ if (classesJar.exists()) {
26
+ project.copy {
27
+ it.from(project.zipTree(classesJar))
28
+ it.into(unzipDir)
29
+ }
30
+ println (" Unzipped classes.jar to: $unzipDir " )
31
+
32
+ // Process the .class files to retrieve annotation data
33
+ val annotationProcessor = AnnotationProcessor ()
34
+ unzipDir.walk().forEach { file ->
35
+ if (file.extension == " class" ) {
36
+ println (" Processing class file: $file " )
37
+ annotationProcessor.processClassFile(file)
38
+ }
39
+ }
40
+ } else {
41
+ println (" classes.jar not found in AAR file" )
42
+ }
43
+ } else {
44
+ println (" AAR file not found" )
45
+ }
46
+ }
47
+
48
+ class AnnotationProcessor {
49
+
50
+ fun processClassFile (classFile : File ) {
51
+ val classReader = ClassReader (classFile.readBytes())
52
+ val classNode = ClassNode ()
53
+ classReader.accept(classNode, 0 )
54
+
55
+ // Retrieve annotations from the class
56
+ classNode.visibleAnnotations?.forEach { annotationNode ->
57
+ println (" Class annotation: ${annotationNode.desc} " )
58
+ }
59
+
60
+ // Retrieve annotations from the methods
61
+ classNode.methods?.forEach { methodNode ->
62
+ methodNode.visibleAnnotations?.forEach { annotationNode ->
63
+ println (" Method annotation in ${methodNode.name} : ${annotationNode.desc} " )
64
+ }
65
+ }
66
+
67
+ // Retrieve annotations from the fields
68
+ classNode.fields?.forEach { fieldNode ->
69
+ fieldNode.visibleAnnotations?.forEach { annotationNode ->
70
+ println (" Field annotation in ${fieldNode.name} : ${annotationNode.desc} " )
71
+ }
72
+ }
73
+ }
74
+ }
0 commit comments