Skip to content

Commit 6a3891e

Browse files
committed
updating gradle build files and importAar plugin to gradle 8
1 parent f057b2e commit 6a3891e

File tree

8 files changed

+385
-340
lines changed

8 files changed

+385
-340
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
* Build Gradle plgin needed to use aar files as dependencies in a pure java library project.
28+
* Adapted from the following plugin by nekocode
29+
* https://github.com/nekocode/Gradle-Import-Aar
30+
* Ported to Groovy, and made specific to the needs of the Android mode build process (i.e.: this plugin
31+
* is not meant to be used with other projects).
32+
* Ported to Gradle 8 replacing the deprecated ArtifactTransform with the new TransformAction API.
33+
*/
34+
class ImportAar implements Plugin<Project> {
35+
final String CONFIG_NAME_POSTFIX = "Aar"
36+
37+
@Override
38+
void apply(Project project) {
39+
def aar = AndroidArtifacts.TYPE_AAR
40+
def jar = AndroidArtifacts.TYPE_JAR
41+
42+
println "ImportAar: WORKS!"
43+
44+
// Create AAR configurations
45+
Collection<Configuration> allConfigs = project.getConfigurations().toList()
46+
for (Configuration config: allConfigs) {
47+
println config
48+
Configuration aarConfig = project.configurations.maybeCreate(config.name + CONFIG_NAME_POSTFIX)
49+
println aarConfig
50+
51+
// Add extracted jars to original configuration after project evaluating
52+
aarConfig.attributes {
53+
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, LibraryElements.JAR))
54+
attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage, Usage.JAVA_RUNTIME))
55+
attribute(Category.CATEGORY_ATTRIBUTE, project.objects.named(Category, Category.LIBRARY))
56+
}
57+
58+
project.afterEvaluate {
59+
aarConfig.resolvedConfiguration.resolvedArtifacts.each { artifact ->
60+
File jarFile = artifact.file
61+
print "================================================> FILE "
62+
println jarFile
63+
println jarFile.getName()
64+
65+
// Add jar file to classpath
66+
project.sourceSets.main.compileClasspath += project.files(jarFile)
67+
68+
File libraryFolder = new File(project.buildDir, "libs")
69+
libraryFolder.mkdirs()
70+
71+
// Strip version number when copying
72+
String name = jarFile.name
73+
int p = name.lastIndexOf("-")
74+
String libName = name.substring(0, p) + ".jar"
75+
File libraryJar = new File(libraryFolder, libName)
76+
Files.copy(jarFile.toPath(), libraryJar.toPath(), REPLACE_EXISTING)
77+
}
78+
}
79+
}
80+
81+
// Register aar transform
82+
project.dependencies {
83+
registerTransform(AarToJarTransform) {
84+
from.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, aar))
85+
to.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, LibraryElements.JAR))
86+
}
87+
}
88+
}
89+
90+
abstract static class AarToJarTransform implements TransformAction<TransformParameters.None> {
91+
AarToJarTransform() {
92+
println "AarToJarTransform instantiated"
93+
}
94+
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}"
117+
}
118+
}
119+
}
120+
}
121+

processing/buildSrc/src/main/groovy/processing/android/ImportAar.groovy

-125
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=ImportAar

processing/buildSrc/src/main/resources/META-INF/gradle-plugins/aar.properties

-1
This file was deleted.

processing/core/build.gradle

+53-52
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,34 @@ import java.nio.file.Files
33
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
44

55
plugins {
6-
id 'aar'
6+
id 'ImportAar'
7+
id 'java-library'
8+
id 'maven-publish'
79
}
810

911
dependencies {
1012
implementation name: "android"
11-
implementationAar "androidx.legacy:legacy-support-v4:${v4legacyVersion}"
12-
implementationAar "com.google.android.support:wearable:${wearVersion}"
13+
implementation "androidx.legacy:legacy-support-v4:${v4legacyVersion}"
14+
implementation "com.google.android.support:wearable:${wearVersion}"
1315
}
1416

15-
sourceSets {
16-
main {
17-
java {
18-
srcDirs = ["../../libs/processing-core/src/main/java/"]
19-
}
20-
resources {
21-
srcDirs = ["../../libs/processing-core/src/main/"]
22-
exclude "AndroidManifest.xml"
23-
exclude '**/java/**'
24-
}
25-
}
17+
sourceSets.main {
18+
java.srcDir("../../libs/processing-core/src/main/java/")
19+
resources.srcDir("../../libs/processing-core/src/main/")
20+
resources.exclude("AndroidManifest.xml", "**/java/**")
2621
}
2722

28-
task sourceJar(type: Jar, dependsOn: classes) {
23+
tasks.register('sourceJar', Jar) {
24+
dependsOn classes
2925
duplicatesStrategy = DuplicatesStrategy.INCLUDE
30-
classifier = "sources"
31-
from sourceSets.main.allSource
26+
archiveClassifier.set("sources")
27+
from sourceSets.main.allSource
3228
}
3329

34-
3530
// Does not work because of Processing-specific tags in source code, such as @webref
36-
task javadocJar(type: Jar, dependsOn: javadoc) {
37-
classifier = "javadoc"
31+
tasks.register('javadocJar', Jar) {
32+
dependsOn javadoc
33+
archiveClassifier.set("javadoc")
3834
from javadoc.destinationDir
3935
}
4036

@@ -44,45 +40,49 @@ artifacts {
4440
}
4541

4642
jar.doLast { task ->
47-
ant.checksum file: task.archivePath
43+
ant.checksum file: task.archiveFile.get().asFile
4844
}
4945

50-
clean.doFirst {
51-
delete "dist"
52-
delete "${coreZipPath}"
46+
tasks.named('clean').configure {
47+
doFirst {
48+
delete "dist"
49+
delete "${coreZipPath}"
50+
}
5351
}
5452

55-
compileJava.doFirst {
56-
String[] deps = ["wearable.jar"]
57-
for (String fn : deps) {
58-
Files.copy(file("${rootDir}/build/libs/" + fn).toPath(),
59-
file("${rootDir}/mode/mode/" + fn).toPath(), REPLACE_EXISTING)
53+
tasks.named('compileJava').configure {
54+
doFirst {
55+
String[] deps = ["wearable.jar"]
56+
deps.each { fn ->
57+
Files.copy(file("${rootDir}/build/libs/${fn}").toPath(),
58+
file("${rootDir}/mode/mode/${fn}").toPath(), REPLACE_EXISTING)
59+
}
6060
}
6161
}
6262

63-
build.doLast {
64-
65-
// Need to check the existance of the files before using as the files
66-
// will get generated only if Task :core:jar is not being skipped
67-
// Task :core:jar will be skipped if source files are unchanged or jar task is UP-TO-DATE
68-
69-
if (file("${buildDir}/libs/core.jar").exists()) {
70-
// Copying core jar as zip inside the mode folder
71-
Files.copy(file("${buildDir}/libs/core.jar").toPath(),
72-
file("${coreZipPath}").toPath(), REPLACE_EXISTING)
73-
}
74-
// Renaming artifacts for maven publishing
75-
if (file("${buildDir}/libs/core.jar").exists()) {
76-
Files.move(file("${buildDir}/libs/core.jar").toPath(),
77-
file("$buildDir/libs/processing-core-${modeVersion}.jar").toPath(), REPLACE_EXISTING);
78-
}
79-
if (file("${buildDir}/libs/core-sources.jar").exists()) {
80-
Files.move(file("${buildDir}/libs/core-sources.jar").toPath(),
81-
file("$buildDir/libs/processing-core-${modeVersion}-sources.jar").toPath(), REPLACE_EXISTING);
82-
}
83-
if (file("${buildDir}/libs/core.jar.MD5").exists()) {
84-
Files.move(file("${buildDir}/libs/core.jar.MD5").toPath(),
85-
file("$buildDir/libs/processing-core-${modeVersion}.jar.md5").toPath(), REPLACE_EXISTING);
63+
tasks.named('build').configure {
64+
doLast {
65+
// Need to check the existance of the files before using as the files
66+
// will get generated only if Task :core:jar is not being skipped
67+
// Task :core:jar will be skipped if source files are unchanged or jar task is UP-TO-DATE
68+
if (file("${buildDir}/libs/core.jar").exists()) {
69+
// Copying core jar as zip inside the mode folder
70+
Files.copy(file("${buildDir}/libs/core.jar").toPath(),
71+
file("${coreZipPath}").toPath(), REPLACE_EXISTING)
72+
}
73+
// Renaming artifacts for maven publishing
74+
if (file("${buildDir}/libs/core.jar").exists()) {
75+
Files.move(file("${buildDir}/libs/core.jar").toPath(),
76+
file("$buildDir/libs/processing-core-${modeVersion}.jar").toPath(), REPLACE_EXISTING)
77+
}
78+
if (file("${buildDir}/libs/core-sources.jar").exists()) {
79+
Files.move(file("${buildDir}/libs/core-sources.jar").toPath(),
80+
file("$buildDir/libs/processing-core-${modeVersion}-sources.jar").toPath(), REPLACE_EXISTING)
81+
}
82+
if (file("${buildDir}/libs/core.jar.MD5").exists()) {
83+
Files.move(file("${buildDir}/libs/core.jar.MD5").toPath(),
84+
file("$buildDir/libs/processing-core-${modeVersion}.jar.md5").toPath(), REPLACE_EXISTING)
85+
}
8686
}
8787
}
8888

@@ -96,4 +96,5 @@ ext {
9696
[name: 'wearable', group: 'com.google.android.support', version: wearVersion],
9797
[name: 'android']]
9898
}
99+
99100
apply from: "${rootProject.projectDir}/scripts/publish-module.gradle"

0 commit comments

Comments
 (0)