Skip to content

Commit 4bcd09e

Browse files
committed
Add gradle tasks to unpack aar file & retrieve annotation
1 parent 89dafee commit 4bcd09e

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
7+
8+
@KotlinxBenchmarkPluginInternalApi
9+
fun Project.processAndroidCompilation(target: KotlinJvmAndroidCompilation) {
10+
project.logger.info("Configuring benchmarks for '${target.name}' using Kotlin/Android")
11+
println("processAndroidCompilation: ${target.name}")
12+
val compilation = target.target.compilations.names.let(::println)
13+
14+
tasks.register("process${target.name.capitalize()}Compilation", DefaultTask::class.java) {
15+
it.group = "benchmark"
16+
it.description = "Processes the Android compilation '${target.name}' for benchmarks"
17+
it.dependsOn("bundle${target.name.capitalize()}Aar")
18+
it.doLast {
19+
unpackAndProcessAar(target)
20+
}
21+
}
22+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

plugin/main/src/kotlinx/benchmark/gradle/BenchmarksPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ constructor(
125125
config.target.compilations.all { compilation ->
126126
// This block is called for each compilation when they are materialized
127127
println("handling compilation: $compilation")
128+
processAndroidCompilation(compilation)
128129
}
129130
}
130131
}

0 commit comments

Comments
 (0)