From ee8f49f0bd6cead41899287b40e00ef3fde194ff Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Wed, 27 Sep 2023 15:15:37 +0200 Subject: [PATCH 1/9] Splitting resources from classes for local projects --- .../gradle/android/ByteBuddyAndroidPlugin.java | 13 ++++++++++--- .../android/ByteBuddyLocalClassesEnhancerTask.java | 11 ++++++++++- .../LegacyByteBuddyLocalClassesEnhancerTask.java | 11 ++++++++++- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java index 81a3a21469..225465a629 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java @@ -44,6 +44,7 @@ import org.gradle.api.attributes.AttributeMatchingStrategy; import org.gradle.api.attributes.Category; import org.gradle.api.attributes.CompatibilityCheckDetails; +import org.gradle.api.attributes.LibraryElements; import org.gradle.api.attributes.Usage; import org.gradle.api.file.Directory; import org.gradle.api.file.DirectoryProperty; @@ -314,12 +315,19 @@ protected ByteBuddyTransformationConfiguration(Project project, * {@inheritDoc} */ public Unit invoke(ByteBuddyInstrumentationParameters parameters) { - parameters.getByteBuddyClasspath().from(configuration); + parameters.getByteBuddyClasspath().from(configuration).from(getByteBuddyClasspathResources()); parameters.getAndroidBootClasspath().from(project.getExtensions().getByType(BaseExtension.class).getBootClasspath()); parameters.getRuntimeClasspath().from(classPath); parameters.getByteBuddyService().set(byteBuddyAndroidServiceProvider); return Unit.INSTANCE; } + + private FileCollection getByteBuddyClasspathResources() { + return configuration.getIncoming().artifactView(viewConfiguration -> { + viewConfiguration.lenient(false); + viewConfiguration.getAttributes().attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.getObjects().named(LibraryElements.class, LibraryElements.RESOURCES)); + }).getFiles(); + } } /** @@ -396,11 +404,10 @@ protected AttributeContainerConfigurationAction(Project project, String buildTyp * {@inheritDoc} */ public void execute(AttributeContainer attributes) { - attributes.attribute(ARTIFACT_TYPE_ATTRIBUTE, BYTE_BUDDY_JAR_TYPE); attributes.attribute(Category.CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, Category.LIBRARY)); attributes.attribute(BuildTypeAttr.ATTRIBUTE, project.getObjects().named(BuildTypeAttr.class, buildType)); attributes.attribute(Usage.USAGE_ATTRIBUTE, project.getObjects().named(Usage.class, Usage.JAVA_RUNTIME)); - + attributes.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.getObjects().named(LibraryElements.class, LibraryElements.CLASSES)); } } diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java index 6ad20f616b..ef2092d2b8 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java @@ -28,7 +28,9 @@ import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; import org.gradle.api.JavaVersion; +import org.gradle.api.Task; import org.gradle.api.artifacts.Configuration; +import org.gradle.api.attributes.LibraryElements; import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.file.Directory; import org.gradle.api.file.FileCollection; @@ -243,10 +245,17 @@ public ConfigurationAction(Configuration byteBuddyConfiguration, BaseExtension a @Override public void execute(ByteBuddyLocalClassesEnhancerTask task) { - task.getByteBuddyClasspath().from(byteBuddyConfiguration); + task.getByteBuddyClasspath().from(byteBuddyConfiguration).from(getByteBuddyClasspathResources(task)); task.getAndroidBootClasspath().from(androidExtension.getBootClasspath()); task.getRuntimeClasspath().from(runtimeClasspath); task.getJavaTargetCompatibilityVersion().set(androidExtension.getCompileOptions().getTargetCompatibility()); } + + private FileCollection getByteBuddyClasspathResources(Task task) { + return byteBuddyConfiguration.getIncoming().artifactView(viewConfiguration -> { + viewConfiguration.lenient(false); + viewConfiguration.getAttributes().attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, task.getProject().getObjects().named(LibraryElements.class, LibraryElements.RESOURCES)); + }).getFiles(); + } } } diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/LegacyByteBuddyLocalClassesEnhancerTask.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/LegacyByteBuddyLocalClassesEnhancerTask.java index f9aff5c21d..c604a3e26e 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/LegacyByteBuddyLocalClassesEnhancerTask.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/LegacyByteBuddyLocalClassesEnhancerTask.java @@ -28,7 +28,9 @@ import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; import org.gradle.api.JavaVersion; +import org.gradle.api.Task; import org.gradle.api.artifacts.Configuration; +import org.gradle.api.attributes.LibraryElements; import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.file.Directory; import org.gradle.api.file.DirectoryProperty; @@ -231,10 +233,17 @@ public ConfigurationAction(Configuration byteBuddyConfiguration, BaseExtension a @Override public void execute(LegacyByteBuddyLocalClassesEnhancerTask task) { - task.getByteBuddyClasspath().from(byteBuddyConfiguration); + task.getByteBuddyClasspath().from(byteBuddyConfiguration).from(getByteBuddyClasspathResources(task)); task.getAndroidBootClasspath().from(androidExtension.getBootClasspath()); task.getRuntimeClasspath().from(runtimeClasspath); task.getJavaTargetCompatibilityVersion().set(androidExtension.getCompileOptions().getTargetCompatibility()); } + + private FileCollection getByteBuddyClasspathResources(Task task) { + return byteBuddyConfiguration.getIncoming().artifactView(viewConfiguration -> { + viewConfiguration.lenient(false); + viewConfiguration.getAttributes().attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, task.getProject().getObjects().named(LibraryElements.class, LibraryElements.RESOURCES)); + }).getFiles(); + } } } From 21dc6336330c08f50716a05d289dd1f0c2754615 Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Wed, 27 Sep 2023 22:29:06 +0200 Subject: [PATCH 2/9] Using custom dependency rule to merge class dirs and resources from local and remote deps --- .../android/AarGradleTransformAction.java | 91 ------------------- .../android/ByteBuddyAndroidPlugin.java | 54 +++++++---- .../ByteBuddyLocalClassesEnhancerTask.java | 16 +--- ...gacyByteBuddyLocalClassesEnhancerTask.java | 16 +--- 4 files changed, 42 insertions(+), 135 deletions(-) delete mode 100644 byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/AarGradleTransformAction.java diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/AarGradleTransformAction.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/AarGradleTransformAction.java deleted file mode 100644 index 488a7dba9d..0000000000 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/AarGradleTransformAction.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2014 - Present Rafael Winterhalter - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package net.bytebuddy.build.gradle.android; - -import org.gradle.api.Action; -import org.gradle.api.artifacts.transform.*; -import org.gradle.api.file.FileSystemLocation; -import org.gradle.api.provider.Provider; - -import java.io.*; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; - -/** - * This {@link TransformAction} takes care of providing JARs embedded in AARs dependencies for Byte Buddy's configuration. - */ -public abstract class AarGradleTransformAction implements TransformAction { - - /** - * The AAR file format extension. - */ - private static final String AAR_FILE_EXTENSION = "aar"; - - /** - * Returns the input artifact. - * - * @return The input artifact. - */ - @InputArtifact - public abstract Provider getInputArtifact(); - - /** - * {@inheritDoc} - */ - public void transform(TransformOutputs transformOutputs) { - File input = getInputArtifact().get().getAsFile(); - String outputName = input.getName().replaceAll("\\." + AAR_FILE_EXTENSION + "$", ".jar"); - try { - ZipFile zipFile = new ZipFile(input); - try { - ZipEntry entry = zipFile.getEntry("classes.jar"); - InputStream inputStream = zipFile.getInputStream(entry); - try { - OutputStream outputStream = new FileOutputStream(transformOutputs.file(outputName)); - try { - byte[] buffer = new byte[1024 * 8]; - int length; - while ((length = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, length); - } - } finally { - outputStream.close(); - } - } finally { - inputStream.close(); - } - } finally { - zipFile.close(); - } - } catch (IOException exception) { - throw new IllegalStateException("Failed to transform " + getInputArtifact(), exception); - } - } - - /** - * A configuration action for transforming an AAR file. - */ - protected static class ConfigurationAction implements Action> { - - /** - * {@inheritDoc} - */ - public void execute(TransformSpec spec) { - spec.getFrom().attribute(ByteBuddyAndroidPlugin.ARTIFACT_TYPE_ATTRIBUTE, AAR_FILE_EXTENSION); - spec.getTo().attribute(ByteBuddyAndroidPlugin.ARTIFACT_TYPE_ATTRIBUTE, "bytebuddy-jar"); - } - } -} diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java index 225465a629..81d04fc125 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java @@ -44,7 +44,6 @@ import org.gradle.api.attributes.AttributeMatchingStrategy; import org.gradle.api.attributes.Category; import org.gradle.api.attributes.CompatibilityCheckDetails; -import org.gradle.api.attributes.LibraryElements; import org.gradle.api.attributes.Usage; import org.gradle.api.file.Directory; import org.gradle.api.file.DirectoryProperty; @@ -73,7 +72,8 @@ public class ByteBuddyAndroidPlugin implements Plugin { /** * The name of the Byte Buddy jar type. */ - private static final String BYTE_BUDDY_JAR_TYPE = "bytebuddy-jar"; + public static final String BYTE_BUDDY_CLASSES_TYPE = "bytebuddy-classes"; + public static final String BYTE_BUDDY_RESOURCES_TYPE = "bytebuddy-resources"; /* * Resolves the dispatcher. @@ -111,7 +111,6 @@ public void apply(Project project) { if (currentAgpVersion.compareTo(new AndroidPluginVersion(7, 2)) < 0) { throw new IllegalStateException("Byte Buddy requires at least Gradle Plugin version 7.2+, but found " + currentAgpVersion); } - project.getDependencies().registerTransform(AarGradleTransformAction.class, new AarGradleTransformAction.ConfigurationAction()); project.getDependencies().getAttributesSchema().attribute(ARTIFACT_TYPE_ATTRIBUTE, new AttributeMatchingStrategyConfigurationAction()); extension.onVariants(extension.selector().all(), new VariantAction(project, project.getConfigurations().create("byteBuddy", new ConfigurationConfigurationAction()))); } @@ -315,19 +314,12 @@ protected ByteBuddyTransformationConfiguration(Project project, * {@inheritDoc} */ public Unit invoke(ByteBuddyInstrumentationParameters parameters) { - parameters.getByteBuddyClasspath().from(configuration).from(getByteBuddyClasspathResources()); + parameters.getByteBuddyClasspath().from(getByteBuddyProperClasspath(project, configuration)); parameters.getAndroidBootClasspath().from(project.getExtensions().getByType(BaseExtension.class).getBootClasspath()); parameters.getRuntimeClasspath().from(classPath); parameters.getByteBuddyService().set(byteBuddyAndroidServiceProvider); return Unit.INSTANCE; } - - private FileCollection getByteBuddyClasspathResources() { - return configuration.getIncoming().artifactView(viewConfiguration -> { - viewConfiguration.lenient(false); - viewConfiguration.getAttributes().attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.getObjects().named(LibraryElements.class, LibraryElements.RESOURCES)); - }).getFiles(); - } } /** @@ -407,7 +399,6 @@ public void execute(AttributeContainer attributes) { attributes.attribute(Category.CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, Category.LIBRARY)); attributes.attribute(BuildTypeAttr.ATTRIBUTE, project.getObjects().named(BuildTypeAttr.class, buildType)); attributes.attribute(Usage.USAGE_ATTRIBUTE, project.getObjects().named(Usage.class, Usage.JAVA_RUNTIME)); - attributes.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.getObjects().named(LibraryElements.class, LibraryElements.CLASSES)); } } @@ -420,7 +411,7 @@ protected static class AttributeMatchingStrategyConfigurationAction implements A * {@inheritDoc} */ public void execute(AttributeMatchingStrategy stringAttributeMatchingStrategy) { - stringAttributeMatchingStrategy.getCompatibilityRules().add(ByteBuddyJarRule.class); + stringAttributeMatchingStrategy.getCompatibilityRules().add(ByteBuddyDependencyRule.class); } } @@ -441,14 +432,22 @@ public void execute(Configuration configuration) { /** * A rule to check for jar compatibility. */ - public abstract static class ByteBuddyJarRule implements AttributeCompatibilityRule { + public abstract static class ByteBuddyDependencyRule implements AttributeCompatibilityRule { /** * {@inheritDoc} */ public void execute(CompatibilityCheckDetails details) { - if (BYTE_BUDDY_JAR_TYPE.equals(details.getConsumerValue()) && "jar".equals(details.getProducerValue())) { - details.compatible(); + if (BYTE_BUDDY_CLASSES_TYPE.equals(details.getConsumerValue())) { + String producerValue = details.getProducerValue(); + if ("java-classes-directory".equals(producerValue) || "android-classes-directory".equals(producerValue)) { + details.compatible(); + } + } else if (BYTE_BUDDY_RESOURCES_TYPE.equals(details.getConsumerValue())) { + String producerValue = details.getProducerValue(); + if ("java-resources-directory".equals(producerValue) || "android-java-res".equals(producerValue)) { + details.compatible(); + } } } } @@ -474,7 +473,7 @@ enum ForLegacyAndroid implements TransformationDispatcher { public void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath) { TaskProvider provider = project.getTasks().register(variant.getName() + "BytebuddyLocalTransform", LegacyByteBuddyLocalClassesEnhancerTask.class, - new LegacyByteBuddyLocalClassesEnhancerTask.ConfigurationAction(configuration, project.getExtensions().getByType(BaseExtension.class), classPath)); + new LegacyByteBuddyLocalClassesEnhancerTask.ConfigurationAction(getByteBuddyProperClasspath(project, configuration), project.getExtensions().getByType(BaseExtension.class), classPath)); variant.getArtifacts() .use(provider) .wiredWith(GetLocalClassesFunction.INSTANCE, GetOutputDirFunction.INSTANCE) @@ -571,7 +570,7 @@ protected ForApk74CompatibleAndroid(Method forScope, Method use, Method toTransf public void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath) { TaskProvider provider = project.getTasks().register(variant.getName() + "BytebuddyLocalTransform", ByteBuddyLocalClassesEnhancerTask.class, - new ByteBuddyLocalClassesEnhancerTask.ConfigurationAction(configuration, project.getExtensions().getByType(BaseExtension.class), classPath)); + new ByteBuddyLocalClassesEnhancerTask.ConfigurationAction(getByteBuddyProperClasspath(project, configuration), project.getExtensions().getByType(BaseExtension.class), classPath)); try { toTransform.invoke(use.invoke(forScope.invoke(variant.getArtifacts(), scope), provider), artifact, @@ -650,4 +649,23 @@ public RegularFileProperty invoke(ByteBuddyLocalClassesEnhancerTask task) { */ void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath); } + + private static FileCollection getByteBuddyClasspathResources(Configuration byteBuddyConfiguration) { + return byteBuddyConfiguration.getIncoming().artifactView(viewConfiguration -> { + viewConfiguration.lenient(false); + viewConfiguration.getAttributes().attribute(ARTIFACT_TYPE_ATTRIBUTE, BYTE_BUDDY_RESOURCES_TYPE); + }).getFiles(); + } + + private static FileCollection getByteBuddyClasspathClasses(Configuration byteBuddyConfiguration) { + return byteBuddyConfiguration.getIncoming().artifactView(viewConfiguration -> { + viewConfiguration.lenient(false); + viewConfiguration.getAttributes().attribute(ARTIFACT_TYPE_ATTRIBUTE, BYTE_BUDDY_CLASSES_TYPE); + }).getFiles(); + } + + private static FileCollection getByteBuddyProperClasspath(Project project, Configuration byteBuddyConfiguration) { + return project.files(getByteBuddyClasspathClasses(byteBuddyConfiguration), + getByteBuddyClasspathResources(byteBuddyConfiguration)); + } } diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java index ef2092d2b8..269eeedaf1 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java @@ -28,9 +28,6 @@ import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; import org.gradle.api.JavaVersion; -import org.gradle.api.Task; -import org.gradle.api.artifacts.Configuration; -import org.gradle.api.attributes.LibraryElements; import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.file.Directory; import org.gradle.api.file.FileCollection; @@ -220,7 +217,7 @@ public static class ConfigurationAction implements Action { - viewConfiguration.lenient(false); - viewConfiguration.getAttributes().attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, task.getProject().getObjects().named(LibraryElements.class, LibraryElements.RESOURCES)); - }).getFiles(); - } } } diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/LegacyByteBuddyLocalClassesEnhancerTask.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/LegacyByteBuddyLocalClassesEnhancerTask.java index c604a3e26e..f29db09ef1 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/LegacyByteBuddyLocalClassesEnhancerTask.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/LegacyByteBuddyLocalClassesEnhancerTask.java @@ -28,9 +28,6 @@ import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; import org.gradle.api.JavaVersion; -import org.gradle.api.Task; -import org.gradle.api.artifacts.Configuration; -import org.gradle.api.attributes.LibraryElements; import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.file.Directory; import org.gradle.api.file.DirectoryProperty; @@ -208,7 +205,7 @@ public static class ConfigurationAction implements Action { - viewConfiguration.lenient(false); - viewConfiguration.getAttributes().attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, task.getProject().getObjects().named(LibraryElements.class, LibraryElements.RESOURCES)); - }).getFiles(); - } } } From 8668a875081f3b5127a606dc79d2f0e36e391eb6 Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Thu, 28 Sep 2023 14:30:56 +0200 Subject: [PATCH 3/9] Using the last api's ALL scope --- .../android/ByteBuddyAndroidPlugin.java | 31 +++++-- .../ByteBuddyLocalClassesEnhancerTask.java | 22 +---- .../android/TargetForAndroidAppJarFile.java | 85 +++++++++++++++++++ 3 files changed, 110 insertions(+), 28 deletions(-) create mode 100644 byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/TargetForAndroidAppJarFile.java diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java index 81d04fc125..87d959206c 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java @@ -32,6 +32,7 @@ import java.util.concurrent.ConcurrentMap; import kotlin.Unit; import kotlin.jvm.functions.Function1; +import net.bytebuddy.utility.nullability.MaybeNull; import org.gradle.api.Action; import org.gradle.api.GradleException; import org.gradle.api.Plugin; @@ -85,7 +86,7 @@ public class ByteBuddyAndroidPlugin implements Plugin { Class scopedArtifacts = Class.forName("com.android.build.api.variant.ScopedArtifacts"); Class scopedArtifact = Class.forName("com.android.build.api.artifact.ScopedArtifact"); @SuppressWarnings("unchecked") - Object project = Enum.valueOf((Class) scope, "PROJECT"); + Object project = Enum.valueOf((Class) scope, "ALL"); dispatcher = new TransformationDispatcher.ForApk74CompatibleAndroid( Artifacts.class.getMethod("forScope", scope), scopedArtifacts.getMethod("use", TaskProvider.class), @@ -151,9 +152,26 @@ protected VariantAction(Project project, Configuration configuration) { * {@inheritDoc} */ public void execute(Variant variant) { + Configuration configuration = getByteBuddyConfiguration(variant); + + if (TRANSFORMATION_DISPATCHER instanceof TransformationDispatcher.ForApk74CompatibleAndroid) { + TRANSFORMATION_DISPATCHER.accept(project, variant, configuration, null); + return; + } + + // Legacy api usage. Provider byteBuddyAndroidServiceProvider = project.getGradle().getSharedServices().registerIfAbsent(variant.getName() + "ByteBuddyAndroidService", ByteBuddyAndroidService.class, new ByteBuddyAndroidService.ConfigurationAction(project.getExtensions().getByType(BaseExtension.class))); + FileCollection classPath = RuntimeClassPathResolver.INSTANCE.apply(variant); + variant.getInstrumentation().transformClassesWith(ByteBuddyAsmClassVisitorFactory.class, InstrumentationScope.ALL, new ByteBuddyTransformationConfiguration(project, + configuration, + byteBuddyAndroidServiceProvider, + classPath)); + TRANSFORMATION_DISPATCHER.accept(project, variant, configuration, classPath); + } + + private Configuration getByteBuddyConfiguration(Variant variant) { if (variant.getBuildType() == null) { throw new GradleException("Build type for " + variant + " was null"); } @@ -167,12 +185,7 @@ public void execute(Variant variant) { configuration = previous; } } - FileCollection classPath = RuntimeClassPathResolver.INSTANCE.apply(variant); - variant.getInstrumentation().transformClassesWith(ByteBuddyAsmClassVisitorFactory.class, InstrumentationScope.ALL, new ByteBuddyTransformationConfiguration(project, - configuration, - byteBuddyAndroidServiceProvider, - classPath)); - TRANSFORMATION_DISPATCHER.accept(project, variant, configuration, classPath); + return configuration; } } @@ -570,7 +583,7 @@ protected ForApk74CompatibleAndroid(Method forScope, Method use, Method toTransf public void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath) { TaskProvider provider = project.getTasks().register(variant.getName() + "BytebuddyLocalTransform", ByteBuddyLocalClassesEnhancerTask.class, - new ByteBuddyLocalClassesEnhancerTask.ConfigurationAction(getByteBuddyProperClasspath(project, configuration), project.getExtensions().getByType(BaseExtension.class), classPath)); + new ByteBuddyLocalClassesEnhancerTask.ConfigurationAction(getByteBuddyProperClasspath(project, configuration), project.getExtensions().getByType(BaseExtension.class))); try { toTransform.invoke(use.invoke(forScope.invoke(variant.getArtifacts(), scope), provider), artifact, @@ -647,7 +660,7 @@ public RegularFileProperty invoke(ByteBuddyLocalClassesEnhancerTask task) { * @param configuration The configuration to use. * @param classPath The class path to use. */ - void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath); + void accept(Project project, Variant variant, Configuration configuration, @MaybeNull FileCollection classPath); } private static FileCollection getByteBuddyClasspathResources(Configuration byteBuddyConfiguration) { diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java index 269eeedaf1..5dbf30c8ca 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java @@ -70,14 +70,6 @@ public abstract class ByteBuddyLocalClassesEnhancerTask extends DefaultTask { @InputFiles public abstract ConfigurableFileCollection getByteBuddyClasspath(); - /** - * Returns the runtime class path. - * - * @return The runtime class path. - */ - @InputFiles - public abstract ConfigurableFileCollection getRuntimeClasspath(); - /** * Returns the Java target compatibility version. * @@ -137,7 +129,7 @@ public void execute() { try { ClassFileVersion classFileVersion = ClassFileVersion.ofJavaVersionString(getJavaTargetCompatibilityVersion().get().toString()); List classFileLocators = new ArrayList(); - for (File file : getRuntimeClasspath().plus(getAndroidBootClasspath()).plus(getByteBuddyClasspath()).getFiles()) { + for (File file : getAndroidBootClasspath().plus(getByteBuddyClasspath()).getFiles()) { classFileLocators.add(file.isFile() ? ClassFileLocator.ForJarFile.of(file) : new ClassFileLocator.ForFolder(file)); @@ -185,7 +177,7 @@ public void execute() { classFileVersion, MethodNameTransformer.Suffixing.withRandomSuffix()) .with(classFileLocator) - .apply(new Plugin.Engine.Source.Compound(sources), new Plugin.Engine.Target.ForJarFile(getOutputFile().get().getAsFile()), factories); + .apply(new Plugin.Engine.Source.Compound(sources), new TargetForAndroidAppJarFile(getOutputFile().get().getAsFile()), factories); if (!summary.getFailed().isEmpty()) { throw new IllegalStateException(summary.getFailed() + " local type transformations have failed"); } else if (summary.getTransformed().isEmpty()) { @@ -223,28 +215,20 @@ public static class ConfigurationAction implements Action Date: Thu, 28 Sep 2023 16:18:00 +0200 Subject: [PATCH 4/9] Fixing android descriptor --- .../ByteBuddyLocalClassesEnhancerTask.java | 70 ++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java index 5dbf30c8ca..7d885031ec 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java @@ -22,8 +22,10 @@ import net.bytebuddy.build.BuildLogger; import net.bytebuddy.build.EntryPoint; import net.bytebuddy.build.Plugin; +import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.dynamic.ClassFileLocator; import net.bytebuddy.dynamic.scaffold.inline.MethodNameTransformer; +import net.bytebuddy.utility.QueueFactory; import org.gradle.api.Action; import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; @@ -138,9 +140,13 @@ public void execute() { ClassFileLocator classFileLocator = new ClassFileLocator.Compound(classFileLocators); try { Set sources = new LinkedHashSet(); + Set localClasspath = new HashSet<>(); for (Directory directory : getLocalClassesDirs().get()) { - sources.add(new Plugin.Engine.Source.ForFolder(directory.getAsFile())); + File file = directory.getAsFile(); + localClasspath.add(file); + sources.add(new Plugin.Engine.Source.ForFolder(file)); } + AndroidDescriptor androidDescriptor = DefaultAndroidDescriptor.ofClassPath(localClasspath); for (RegularFile jarFile : getLocalJars().get()) { sources.add(new Plugin.Engine.Source.ForJarFile(jarFile.getAsFile())); } @@ -165,7 +171,7 @@ public void execute() { throw new GradleException(type.getName() + " does not implement " + Plugin.class.getName()); } factories.add(new Plugin.Factory.UsingReflection(type) - .with(Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(AndroidDescriptor.class, AndroidDescriptor.Trivial.LOCAL)) + .with(Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(AndroidDescriptor.class, androidDescriptor)) .with(Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(Logger.class, getProject().getLogger())) .with(Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(org.slf4j.Logger.class, getProject().getLogger())) .with(Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(BuildLogger.class, buildLogger))); @@ -232,4 +238,64 @@ public void execute(ByteBuddyLocalClassesEnhancerTask task) { task.getJavaTargetCompatibilityVersion().set(androidExtension.getCompileOptions().getTargetCompatibility()); } } + + /** + * An implementation for an Android descriptor based on resolving class names against the class path. + */ + protected static class DefaultAndroidDescriptor implements AndroidDescriptor { + + /** + * The file name extension of a Java class file. + */ + private static final String CLASS_FILE_EXTENSION = ".class"; + + /** + * The files on the class path. + */ + private final Set names; + + /** + * Creates a default Android descriptor. + * + * @param names The names of all classes on the class path. + */ + protected DefaultAndroidDescriptor(Set names) { + this.names = names; + } + + /** + * Resolves class names of a set of class files from the class path. + * + * @param roots The class path roots to resolve. + * @return A suitable Android descriptor. + */ + protected static AndroidDescriptor ofClassPath(Set roots) { + Set names = new HashSet(); + for (File root : roots) { + Queue queue = QueueFactory.make(Collections.singleton(root)); + while (!queue.isEmpty()) { + File file = queue.remove(); + if (file.isDirectory()) { + File[] value = file.listFiles(); + if (value != null) { + queue.addAll(Arrays.asList(value)); + } + } else if (file.getName().endsWith(CLASS_FILE_EXTENSION)) { + String path = root.getAbsoluteFile().toURI().relativize(file.getAbsoluteFile().toURI()).getPath(); + names.add(path.substring(0, path.length() - CLASS_FILE_EXTENSION.length()).replace('/', '.')); + } + } + } + return new DefaultAndroidDescriptor(names); + } + + /** + * {@inheritDoc} + */ + public TypeScope getTypeScope(TypeDescription typeDescription) { + return names.contains(typeDescription.getName()) + ? TypeScope.LOCAL + : TypeScope.EXTERNAL; + } + } } From 08ff1f9a0819e183398ac36d4e9100def22f7132 Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Mon, 2 Oct 2023 10:09:42 +0200 Subject: [PATCH 5/9] Removed project references in ByteBuddyLocalClassesEnhancerTask due to https://docs.gradle.org/8.3/userguide/configuration_cache.html#config_cache:requirements:use_project_during_execution --- .../android/ByteBuddyLocalClassesEnhancerTask.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java index 7d885031ec..5d376f52a5 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java @@ -36,6 +36,7 @@ import org.gradle.api.file.RegularFile; import org.gradle.api.file.RegularFileProperty; import org.gradle.api.logging.Logger; +import org.gradle.api.logging.Logging; import org.gradle.api.provider.ListProperty; import org.gradle.api.provider.Property; import org.gradle.api.tasks.Input; @@ -104,6 +105,8 @@ public abstract class ByteBuddyLocalClassesEnhancerTask extends DefaultTask { @OutputFile public abstract RegularFileProperty getOutputFile(); + private static final Logger LOGGER = Logging.getLogger(ByteBuddyLocalClassesEnhancerTask.class); + /** * Translates a collection of files to {@link URL}s. * @@ -159,7 +162,7 @@ public void execute() { try { buildLogger = (BuildLogger) Class.forName("net.bytebuddy.build.gradle.GradleBuildLogger") .getConstructor(Logger.class) - .newInstance(getProject().getLogger()); + .newInstance(LOGGER); } catch (Exception exception) { throw new GradleException("Failed to resolve Gradle build logger", exception); } @@ -172,8 +175,8 @@ public void execute() { } factories.add(new Plugin.Factory.UsingReflection(type) .with(Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(AndroidDescriptor.class, androidDescriptor)) - .with(Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(Logger.class, getProject().getLogger())) - .with(Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(org.slf4j.Logger.class, getProject().getLogger())) + .with(Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(Logger.class, LOGGER)) + .with(Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(org.slf4j.Logger.class, LOGGER)) .with(Plugin.Factory.UsingReflection.ArgumentResolver.ForType.of(BuildLogger.class, buildLogger))); } catch (Throwable throwable) { throw new IllegalStateException("Cannot resolve plugin: " + name, throwable); @@ -187,9 +190,9 @@ public void execute() { if (!summary.getFailed().isEmpty()) { throw new IllegalStateException(summary.getFailed() + " local type transformations have failed"); } else if (summary.getTransformed().isEmpty()) { - getLogger().info("No local types were transformed during plugin execution"); + LOGGER.info("No local types were transformed during plugin execution"); } else { - getLogger().info("Transformed {} local type(s)", summary.getTransformed().size()); + LOGGER.info("Transformed {} local type(s)", summary.getTransformed().size()); } } finally { if (classLoader instanceof Closeable) { From 605e4b34e5bcef33da89ce20a527ac5d66a033ae Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Mon, 2 Oct 2023 10:12:43 +0200 Subject: [PATCH 6/9] Update working regarding ByteBuddyLocalClassesEnhancerTask's scope --- .../android/ByteBuddyAndroidPlugin.java | 2 +- .../ByteBuddyLocalClassesEnhancerTask.java | 20 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java index 87d959206c..185ed25e0f 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java @@ -611,7 +611,7 @@ protected enum GetLocalJarsFunction implements Function1 invoke(ByteBuddyLocalClassesEnhancerTask task) { - return task.getLocalJars(); + return task.getInputJars(); } } diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java index 5d376f52a5..4d1e7b0783 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyLocalClassesEnhancerTask.java @@ -53,7 +53,7 @@ import java.util.*; /** - * Transformation task for instrumenting the project's local classes. + * Transformation task for instrumenting the project's local and dependencies' classes. */ public abstract class ByteBuddyLocalClassesEnhancerTask extends DefaultTask { @@ -82,12 +82,12 @@ public abstract class ByteBuddyLocalClassesEnhancerTask extends DefaultTask { public abstract Property getJavaTargetCompatibilityVersion(); /** - * Target project's local jars. + * Target project's local and dependencies jars. * - * @return The target project's local jars. + * @return The target project's local and dependencies jars. */ @InputFiles - public abstract ListProperty getLocalJars(); + public abstract ListProperty getInputJars(); /** * Target project's local classes dirs. @@ -127,7 +127,7 @@ private static URL[] toUrls(Collection files) { } /** - * Executes the plugin for transforming local classes. + * Executes the plugin for transforming all project's classes. */ @TaskAction public void execute() { @@ -150,7 +150,7 @@ public void execute() { sources.add(new Plugin.Engine.Source.ForFolder(file)); } AndroidDescriptor androidDescriptor = DefaultAndroidDescriptor.ofClassPath(localClasspath); - for (RegularFile jarFile : getLocalJars().get()) { + for (RegularFile jarFile : getInputJars().get()) { sources.add(new Plugin.Engine.Source.ForJarFile(jarFile.getAsFile())); } ClassLoader classLoader = new URLClassLoader( @@ -188,11 +188,11 @@ public void execute() { .with(classFileLocator) .apply(new Plugin.Engine.Source.Compound(sources), new TargetForAndroidAppJarFile(getOutputFile().get().getAsFile()), factories); if (!summary.getFailed().isEmpty()) { - throw new IllegalStateException(summary.getFailed() + " local type transformations have failed"); + throw new IllegalStateException(summary.getFailed() + " type transformations have failed"); } else if (summary.getTransformed().isEmpty()) { - LOGGER.info("No local types were transformed during plugin execution"); + LOGGER.info("No types were transformed during plugin execution"); } else { - LOGGER.info("Transformed {} local type(s)", summary.getTransformed().size()); + LOGGER.info("Transformed {} type(s)", summary.getTransformed().size()); } } finally { if (classLoader instanceof Closeable) { @@ -206,7 +206,7 @@ public void execute() { classFileLocator.close(); } } catch (IOException exception) { - throw new GradleException("Failed to transform local classes", exception); + throw new GradleException("Failed to transform classes", exception); } } From a82563d26730e264d7950e54ea152f60d416b2b8 Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Mon, 2 Oct 2023 10:13:58 +0200 Subject: [PATCH 7/9] Update working regarding ByteBuddyLocalClassesEnhancerTask's scope --- .../build/gradle/android/ByteBuddyAndroidPlugin.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java index 185ed25e0f..05d262e9a3 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java @@ -587,7 +587,7 @@ public void accept(Project project, Variant variant, Configuration configuration try { toTransform.invoke(use.invoke(forScope.invoke(variant.getArtifacts(), scope), provider), artifact, - GetLocalJarsFunction.INSTANCE, + GetProjectJarsFunction.INSTANCE, GetLocalClassesDirsFunction.INSTANCE, GetOutputFileFunction.INSTANCE); } catch (IllegalAccessException exception) { @@ -598,9 +598,9 @@ public void accept(Project project, Variant variant, Configuration configuration } /** - * A function representation of resolving local jars. + * A function representation of resolving local and dependencies jars. */ - protected enum GetLocalJarsFunction implements Function1> { + protected enum GetProjectJarsFunction implements Function1> { /** * The singleton instance. From 63b699f56881218a4aa8f072d6574870bcb141cf Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Mon, 2 Oct 2023 10:32:33 +0200 Subject: [PATCH 8/9] Update wordings and renaming methods in ByteBuddyAndroidPlugin --- .../android/ByteBuddyAndroidPlugin.java | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java index 05d262e9a3..2ef2cae120 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java @@ -260,9 +260,9 @@ protected OfModernAgp(Method getRuntimeConfiguration) { protected FileCollection apply(Variant variant) { try { return ((Configuration) getRuntimeConfiguration.invoke(variant)).getIncoming() - .artifactView(this) - .getArtifacts() - .getArtifactFiles(); + .artifactView(this) + .getArtifacts() + .getArtifactFiles(); } catch (IllegalAccessException exception) { throw new IllegalStateException("Failed to access runtime configuration", exception); } catch (InvocationTargetException exception) { @@ -327,7 +327,7 @@ protected ByteBuddyTransformationConfiguration(Project project, * {@inheritDoc} */ public Unit invoke(ByteBuddyInstrumentationParameters parameters) { - parameters.getByteBuddyClasspath().from(getByteBuddyProperClasspath(project, configuration)); + parameters.getByteBuddyClasspath().from(getByteBuddyClasspath(project, configuration)); parameters.getAndroidBootClasspath().from(project.getExtensions().getByType(BaseExtension.class).getBootClasspath()); parameters.getRuntimeClasspath().from(classPath); parameters.getByteBuddyService().set(byteBuddyAndroidServiceProvider); @@ -486,7 +486,7 @@ enum ForLegacyAndroid implements TransformationDispatcher { public void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath) { TaskProvider provider = project.getTasks().register(variant.getName() + "BytebuddyLocalTransform", LegacyByteBuddyLocalClassesEnhancerTask.class, - new LegacyByteBuddyLocalClassesEnhancerTask.ConfigurationAction(getByteBuddyProperClasspath(project, configuration), project.getExtensions().getByType(BaseExtension.class), classPath)); + new LegacyByteBuddyLocalClassesEnhancerTask.ConfigurationAction(getByteBuddyClasspath(project, configuration), project.getExtensions().getByType(BaseExtension.class), classPath)); variant.getArtifacts() .use(provider) .wiredWith(GetLocalClassesFunction.INSTANCE, GetOutputDirFunction.INSTANCE) @@ -583,7 +583,7 @@ protected ForApk74CompatibleAndroid(Method forScope, Method use, Method toTransf public void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath) { TaskProvider provider = project.getTasks().register(variant.getName() + "BytebuddyLocalTransform", ByteBuddyLocalClassesEnhancerTask.class, - new ByteBuddyLocalClassesEnhancerTask.ConfigurationAction(getByteBuddyProperClasspath(project, configuration), project.getExtensions().getByType(BaseExtension.class))); + new ByteBuddyLocalClassesEnhancerTask.ConfigurationAction(getByteBuddyClasspath(project, configuration), project.getExtensions().getByType(BaseExtension.class))); try { toTransform.invoke(use.invoke(forScope.invoke(variant.getArtifacts(), scope), provider), artifact, @@ -663,22 +663,20 @@ public RegularFileProperty invoke(ByteBuddyLocalClassesEnhancerTask task) { void accept(Project project, Variant variant, Configuration configuration, @MaybeNull FileCollection classPath); } - private static FileCollection getByteBuddyClasspathResources(Configuration byteBuddyConfiguration) { - return byteBuddyConfiguration.getIncoming().artifactView(viewConfiguration -> { + /** + * For external dependencies, it provides their JAR files. For local project's dependencies, it provides their local + * build dirs for both classes and resources. The latter allows for faster and more reliable (up-to-date) compilation processes + * when using local plugins. + */ + private static FileCollection getByteBuddyClasspath(Project project, Configuration byteBuddyConfiguration) { + FileCollection resources = byteBuddyConfiguration.getIncoming().artifactView(viewConfiguration -> { viewConfiguration.lenient(false); viewConfiguration.getAttributes().attribute(ARTIFACT_TYPE_ATTRIBUTE, BYTE_BUDDY_RESOURCES_TYPE); }).getFiles(); - } - - private static FileCollection getByteBuddyClasspathClasses(Configuration byteBuddyConfiguration) { - return byteBuddyConfiguration.getIncoming().artifactView(viewConfiguration -> { + FileCollection classes = byteBuddyConfiguration.getIncoming().artifactView(viewConfiguration -> { viewConfiguration.lenient(false); viewConfiguration.getAttributes().attribute(ARTIFACT_TYPE_ATTRIBUTE, BYTE_BUDDY_CLASSES_TYPE); }).getFiles(); - } - - private static FileCollection getByteBuddyProperClasspath(Project project, Configuration byteBuddyConfiguration) { - return project.files(getByteBuddyClasspathClasses(byteBuddyConfiguration), - getByteBuddyClasspathResources(byteBuddyConfiguration)); + return project.files(classes, resources); } } From 70fbde17931fe165d314388d22e96c0bdb84df01 Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Mon, 2 Oct 2023 10:43:44 +0200 Subject: [PATCH 9/9] Renaming bytebuddy 7.4 transform tasks --- .../bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java index 2ef2cae120..815804efdd 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyAndroidPlugin.java @@ -581,7 +581,7 @@ protected ForApk74CompatibleAndroid(Method forScope, Method use, Method toTransf * {@inheritDoc} */ public void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath) { - TaskProvider provider = project.getTasks().register(variant.getName() + "BytebuddyLocalTransform", + TaskProvider provider = project.getTasks().register(variant.getName() + "BytebuddyTransform", ByteBuddyLocalClassesEnhancerTask.class, new ByteBuddyLocalClassesEnhancerTask.ConfigurationAction(getByteBuddyClasspath(project, configuration), project.getExtensions().getByType(BaseExtension.class))); try {