1
+ // import org.gradle.api.Plugin
2
+ // import org.gradle.api.Project
3
+ // import org.gradle.api.artifacts.Configuration
4
+ // import org.gradle.api.artifacts.transform.TransformAction
5
+ // import org.gradle.api.artifacts.transform.TransformParameters
6
+ // import org.gradle.api.artifacts.transform.InputArtifact
7
+ // import org.gradle.api.artifacts.transform.TransformOutputs
8
+ // import org.gradle.api.file.FileSystemLocation
9
+ // import org.gradle.api.provider.Provider
10
+ // import org.gradle.api.tasks.PathSensitive
11
+ // import org.gradle.api.tasks.PathSensitivity
12
+
13
+ // import org.gradle.api.attributes.LibraryElements
14
+ // import org.gradle.api.attributes.Usage
15
+ // import org.gradle.api.attributes.Category
16
+
17
+ // import com.android.build.gradle.internal.dependency.AarTransform
18
+ // import com.android.build.gradle.internal.dependency.ExtractAarTransform
19
+ // import com.android.build.gradle.internal.publishing.AndroidArtifacts
20
+ // import com.android.builder.aar.AarExtractor
21
+ // import com.google.common.collect.ImmutableList
22
+
23
+ // import java.nio.file.Files
24
+ // import static java.nio.file.StandardCopyOption.REPLACE_EXISTING
25
+
26
+
27
+ // import org.gradle.api.Plugin
28
+ // import org.gradle.api.Project
29
+ // import org.gradle.api.artifacts.Configuration
30
+ // import org.gradle.api.artifacts.ResolvedArtifact
31
+ // import java.util.zip.ZipFile
32
+
1
33
import org.gradle.api.Plugin
2
34
import org.gradle.api.Project
3
35
import org.gradle.api.artifacts.Configuration
4
- import org.gradle.api.artifacts.transform.TransformAction
5
- import org.gradle.api.artifacts.transform.TransformParameters
6
- import org.gradle.api.artifacts.transform.InputArtifact
7
- import org.gradle.api.artifacts.transform.TransformOutputs
8
- import org.gradle.api.file.FileSystemLocation
9
- import org.gradle.api.provider.Provider
10
- import org.gradle.api.tasks.PathSensitive
11
- import org.gradle.api.tasks.PathSensitivity
12
-
13
- import org.gradle.api.attributes.LibraryElements
14
- import org.gradle.api.attributes.Usage
15
- import org.gradle.api.attributes.Category
16
-
17
- import com.android.build.gradle.internal.dependency.AarTransform
18
- import com.android.build.gradle.internal.dependency.ExtractAarTransform
19
- import com.android.build.gradle.internal.publishing.AndroidArtifacts
20
- import com.android.builder.aar.AarExtractor
21
- import com.google.common.collect.ImmutableList
22
-
23
- import java.nio.file.Files
24
- import static java.nio.file.StandardCopyOption.REPLACE_EXISTING
36
+ import org.gradle.api.tasks.TaskAction
37
+ import java.util.zip.ZipFile
25
38
26
39
/**
27
40
* Build Gradle plgin needed to use aar files as dependencies in a pure java library project.
@@ -36,11 +49,42 @@ class ImportAar implements Plugin<Project> {
36
49
37
50
@Override
38
51
void apply (Project project ) {
39
- def aar = AndroidArtifacts . TYPE_AAR
40
- def jar = AndroidArtifacts . TYPE_JAR
52
+ // def aar = AndroidArtifacts.TYPE_AAR
53
+ // def jar = AndroidArtifacts.TYPE_JAR
54
+
55
+ println " >>> Calling ImportAar"
56
+
57
+ // Create a custom resolvable configuration
58
+ project. configurations. create(' aarExtractorResolvable' ) {
59
+ canBeResolved = true
60
+ canBeConsumed = false
61
+ extendsFrom project. configurations. implementation
62
+ }
63
+
64
+ project. tasks. register(' extractAarJars' , ExtractAarJarsTask ) {
65
+ group = ' build'
66
+ description = ' Extracts JAR files from AAR dependencies and places them in build/libs.'
67
+ }
68
+
69
+
70
+ // project.task('extractAarJars') {
71
+ // doLast {
72
+ // println "=======> Calling extractAarJars task"
41
73
42
- println " ImportAar: WORKS!"
74
+ // project.configurations.each { Configuration config ->
75
+ // config.resolvedConfiguration.resolvedArtifacts.each { ResolvedArtifact artifact ->
76
+ // println "Resolved artifact: ${artifact}"
77
+ // if (artifact.type == 'aar') {
78
+ // extractJarFromAar(artifact, project)
79
+ // }
80
+ // }
81
+ // }
82
+ // }
83
+ // }
84
+
43
85
86
+
87
+ /*
44
88
// Create AAR configurations
45
89
Collection<Configuration> allConfigs = project.getConfigurations().toList()
46
90
for (Configuration config: allConfigs) {
@@ -56,6 +100,7 @@ class ImportAar implements Plugin<Project> {
56
100
}
57
101
58
102
project.afterEvaluate {
103
+ println "-> In afterEvaluate"
59
104
aarConfig.resolvedConfiguration.resolvedArtifacts.each { artifact ->
60
105
File jarFile = artifact.file
61
106
print "================================================> FILE "
@@ -82,40 +127,109 @@ class ImportAar implements Plugin<Project> {
82
127
project.dependencies {
83
128
registerTransform(AarToJarTransform) {
84
129
from.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, aar))
85
- to. attribute(LibraryElements . LIBRARY_ELEMENTS_ATTRIBUTE , project. objects. named(LibraryElements , LibraryElements . JAR ))
130
+ to.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, jar ))
86
131
}
87
132
}
133
+ */
88
134
}
89
135
90
- abstract static class AarToJarTransform implements TransformAction<TransformParameters. None > {
91
- AarToJarTransform () {
92
- println " AarToJarTransform instantiated"
93
- }
136
+ // void extractJarFromAar(ResolvedArtifact artifact, Project project) {
137
+ // println "Input AAR: ${aarFile}"
138
+ // def aarFile = artifact.file
139
+ // def zipFile = new ZipFile(aarFile)
140
+ // def entry = zipFile.getEntry('classes.jar')
141
+ // if (entry) {
142
+ // project.copy {
143
+ // from project.zipTree(aarFile)
144
+ // include 'classes.jar'
145
+ // into "${project.buildDir}/libs"
146
+ // rename { "${artifact.name}-${artifact.moduleVersion.id.version}.jar" }
147
+ // }
148
+ // }
149
+ // zipFile.close()
150
+ // }
151
+
152
+
153
+ // abstract static class AarToJarTransform implements TransformAction<TransformParameters.None> {
154
+ // AarToJarTransform() {
155
+ // println "AarToJarTransform instantiated"
156
+ // }
157
+
158
+ // @InputArtifact
159
+ // @PathSensitive(PathSensitivity.NAME_ONLY)
160
+ // abstract Provider<FileSystemLocation> getInputArtifact()
161
+
162
+ // @Override
163
+ // void transform(TransformOutputs outputs) {
164
+ // File inputFile = inputArtifact.get().asFile
165
+ // println "Input AAR: ${inputFile}"
166
+ // File explodedDir = new File(outputs.getOutputDirectory(), "exploded")
167
+ // println "Exploded Directory: ${explodedDir}"
168
+
169
+ // AarExtractor aarExtractor = new AarExtractor()
170
+ // aarExtractor.extract(inputFile, explodedDir)
171
+ // File classesJar = new File(new File(explodedDir, "jars"), "classes.jar")
172
+ // if (classesJar.exists()) {
173
+ // println "Classes JAR found: ${classesJar}"
174
+ // String aarName = inputFile.name.replace(".aar", "")
175
+ // File renamedJar = outputs.file("${aarName}.jar")
176
+ // Files.copy(classesJar.toPath(), renamedJar.toPath(), REPLACE_EXISTING)
177
+ // println "Transformed JAR: ${renamedJar}"
178
+ // } else {
179
+ // println "Error: classes.jar not found in ${explodedDir}"
180
+ // }
181
+ // }
182
+ // }
183
+ }
184
+
94
185
95
- @InputArtifact
96
- @PathSensitive (PathSensitivity .NAME_ONLY )
97
- abstract Provider<FileSystemLocation > getInputArtifact ()
98
-
99
- @Override
100
- void transform (TransformOutputs outputs ) {
101
- File inputFile = inputArtifact. get(). asFile
102
- println " Input AAR: ${ inputFile} "
103
- File explodedDir = new File (outputs. getOutputDirectory(), " exploded" )
104
- println " Exploded Directory: ${ explodedDir} "
105
-
106
- AarExtractor aarExtractor = new AarExtractor ()
107
- aarExtractor. extract(inputFile, explodedDir)
108
- File classesJar = new File (new File (explodedDir, " jars" ), " classes.jar" )
109
- if (classesJar. exists()) {
110
- println " Classes JAR found: ${ classesJar} "
111
- String aarName = inputFile. name. replace(" .aar" , " " )
112
- File renamedJar = outputs. file(" ${ aarName} .jar" )
113
- Files . copy(classesJar. toPath(), renamedJar. toPath(), REPLACE_EXISTING )
114
- println " Transformed JAR: ${ renamedJar} "
115
- } else {
116
- println " Error: classes.jar not found in ${ explodedDir} "
186
+ class ExtractAarJarsTask extends org.gradle.api. DefaultTask {
187
+ @TaskAction
188
+ void extractJars () {
189
+ File outputDir = new File (project. buildDir, ' libs' )
190
+ outputDir. mkdirs()
191
+
192
+ // Configuration compileClasspath = project.configurations.getByName('implementation')
193
+ Configuration aarExtractorResolvable = project. configurations. getByName(' aarExtractorResolvable' )
194
+
195
+ // compileClasspath.resolvedConfiguration.resolvedArtifacts.each { artifact ->
196
+ aarExtractorResolvable. resolvedConfiguration. resolvedArtifacts. each { artifact ->
197
+ if (artifact. type == ' aar' ) {
198
+ File aarFile = artifact. file
199
+ println " Processing AAR: ${ aarFile.name} "
200
+
201
+ // Extract the AAR file
202
+ ZipFile zipFile = new ZipFile (aarFile)
203
+ zipFile. entries(). each { entry ->
204
+ if (entry. name. endsWith(' .jar' )) {
205
+ println " Classes JAR found: ${ entry} "
206
+ String aarName = aarFile. name. replace(" .aar" , " " )
207
+
208
+ File jarOutput = new File (outputDir, " ${ aarName} .jar" )
209
+ jarOutput. parentFile. mkdirs()
210
+
211
+ // Write the JAR file to the output directory
212
+ zipFile. getInputStream(entry). withCloseable { inputStream ->
213
+ jarOutput. withOutputStream { outputStream ->
214
+ copyStream(inputStream, outputStream)
215
+ }
216
+ }
217
+ println " Extracted JAR: ${ jarOutput.absolutePath} "
218
+ }
219
+ }
117
220
}
118
221
}
119
222
}
223
+
224
+ /**
225
+ * Copies data from an InputStream to an OutputStream.
226
+ */
227
+ void copyStream (InputStream input , OutputStream output ) {
228
+ byte [] buffer = new byte [1024 ]
229
+ int bytesRead
230
+ while ((bytesRead = input. read(buffer)) != -1 ) {
231
+ output. write(buffer, 0 , bytesRead)
232
+ }
233
+ }
120
234
}
121
235
0 commit comments