From e8c15a67bc1049ba05f1c9500b4156647dd3b522 Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Tue, 10 Jan 2023 16:58:13 +0100 Subject: [PATCH] Using DependenciesClasspathProvider implementations --- .../android/ByteBuddyAndroidPlugin.java | 31 ++++++++++--------- .../DependenciesClasspathProvider.java | 5 +++ 2 files changed, 22 insertions(+), 14 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 3eb7e6909d..87055f8659 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 @@ -18,14 +18,13 @@ import com.android.build.api.AndroidPluginVersion; import com.android.build.api.artifact.MultipleArtifact; import com.android.build.api.attributes.BuildTypeAttr; -import com.android.build.api.component.impl.ComponentImpl; import com.android.build.api.instrumentation.InstrumentationScope; import com.android.build.api.variant.AndroidComponentsExtension; import com.android.build.api.variant.Variant; import com.android.build.gradle.BaseExtension; -import com.android.build.gradle.internal.publishing.AndroidArtifacts; import kotlin.Unit; import kotlin.jvm.functions.Function1; +import net.bytebuddy.build.gradle.android.classpath.DependenciesClasspathProvider; import org.gradle.api.Action; import org.gradle.api.GradleException; import org.gradle.api.Plugin; @@ -66,12 +65,14 @@ public class ByteBuddyAndroidPlugin implements Plugin { public void apply(Project project) { @SuppressWarnings("unchecked") AndroidComponentsExtension extension = project.getExtensions().getByType(AndroidComponentsExtension.class); - if (extension.getPluginVersion().compareTo(new AndroidPluginVersion(7, 2)) < 0) { - throw new IllegalStateException("Byte Buddy requires at least Gradle Plugin version 7.2+, but found " + extension.getPluginVersion()); + AndroidPluginVersion currentAgpVersion = extension.getPluginVersion(); + 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()))); + extension.onVariants(extension.selector().all(), new VariantAction(project, project.getConfigurations().create("byteBuddy", new ConfigurationConfigurationAction()), + DependenciesClasspathProvider.getInstance(currentAgpVersion))); } /** @@ -89,6 +90,11 @@ protected static class VariantAction implements Action { */ private final Configuration configuration; + /** + * The runtime classpath provider. + */ + private final DependenciesClasspathProvider classpathProvider; + /** * A cache of configurations by built type name. */ @@ -97,12 +103,14 @@ protected static class VariantAction implements Action { /** * Creates a new variant action. * - * @param project The current Gradle project. - * @param configuration The general Byte Buddy configuration. + * @param project The current Gradle project. + * @param configuration The general Byte Buddy configuration. + * @param classpathProvider The runtime classpath provider. */ - protected VariantAction(Project project, Configuration configuration) { + protected VariantAction(Project project, Configuration configuration, DependenciesClasspathProvider classpathProvider) { this.project = project; this.configuration = configuration; + this.classpathProvider = classpathProvider; configurations = new ConcurrentHashMap(); } @@ -126,12 +134,7 @@ public void execute(Variant variant) { configuration = previous; } } - if (!(variant instanceof ComponentImpl)) { - throw new GradleException("Expected " + variant + " to be of type " + ComponentImpl.class.getName()); - } - FileCollection classPath = ((ComponentImpl) variant).getVariantDependencies().getArtifactFileCollection(AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, - AndroidArtifacts.ArtifactScope.ALL, - AndroidArtifacts.ArtifactType.CLASSES_JAR); + FileCollection classPath = classpathProvider.getRuntimeClasspath(variant); variant.getInstrumentation().transformClassesWith(ByteBuddyAsmClassVisitorFactory.class, InstrumentationScope.ALL, new ByteBuddyTransformationConfiguration(project, configuration, byteBuddyAndroidServiceProvider, diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/classpath/DependenciesClasspathProvider.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/classpath/DependenciesClasspathProvider.java index d6883217aa..fdcb9b501b 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/classpath/DependenciesClasspathProvider.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/classpath/DependenciesClasspathProvider.java @@ -3,6 +3,8 @@ import com.android.build.api.AndroidPluginVersion; import com.android.build.api.variant.Variant; import org.gradle.api.file.FileCollection; +import org.gradle.api.logging.Logger; +import org.gradle.api.logging.Logging; import java.lang.reflect.InvocationTargetException; @@ -20,10 +22,13 @@ public interface DependenciesClasspathProvider { */ static DependenciesClasspathProvider getInstance(AndroidPluginVersion currentVersion) { boolean isLowerThan73 = currentVersion.compareTo(new AndroidPluginVersion(7, 3)) < 0; + Logger logger = Logging.getLogger(DependenciesClasspathProvider.class); try { if (isLowerThan73) { + logger.lifecycle("Using legacy classpath provider implementation"); return (DependenciesClasspathProvider) Class.forName("net.bytebuddy.build.gradle.android.classpath.impl.LegacyDependenciesClasspathProvider").getDeclaredConstructor().newInstance(); } else { + logger.lifecycle("Using default classpath provider implementation"); return (DependenciesClasspathProvider) Class.forName("net.bytebuddy.build.gradle.android.classpath.impl.DefaultDependenciesClasspathProvider").getDeclaredConstructor().newInstance(); } } catch (InstantiationException | IllegalAccessException | InvocationTargetException |