From 257b15facdc8b073145b4fd45f87623b98898d51 Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Wed, 7 Sep 2022 15:06:26 +0200 Subject: [PATCH 1/6] Adding android-plugin to final artifact no matter the Java target version --- byte-buddy-gradle-plugin/build.gradle | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/byte-buddy-gradle-plugin/build.gradle b/byte-buddy-gradle-plugin/build.gradle index b325f00f1ae..0c0f97edfd6 100644 --- a/byte-buddy-gradle-plugin/build.gradle +++ b/byte-buddy-gradle-plugin/build.gradle @@ -26,16 +26,11 @@ configurations { setTransitive(false) extendsFrom(fatJar) } - compileClasspath.extendsFrom(fatJarClasspath) } dependencies { implementation gradleApi() - if (targetCompatibility >= JavaVersion.VERSION_1_8 - && sourceCompatibility >= JavaVersion.VERSION_1_8 - && findProject(':android-plugin') != null) { - fatJar project(':android-plugin') - } + fatJar project(':android-plugin') compileOnly group: 'com.google.code.findbugs', name: 'findbugs-annotations', version: outerPom.properties.'version.utility.findbugs' compileOnly group: 'com.google.code.findbugs', name: 'jsr305', version: outerPom.properties.'version.utility.jsr305' testImplementation gradleTestKit() From 003cdab9e24ad16cf4947c976a33d16e30a7803a Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Wed, 7 Sep 2022 15:14:55 +0200 Subject: [PATCH 2/6] Ensuring ByteBuddyCopyOutputTask target file's parent dir exists before copying into it --- .../build/gradle/android/ByteBuddyCopyOutputTask.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyCopyOutputTask.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyCopyOutputTask.java index 293a7d29fe2..0a4d72720ef 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyCopyOutputTask.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyCopyOutputTask.java @@ -78,9 +78,12 @@ public void execute(InputChanges inputChanges) { case ADDED: case MODIFIED: try { + if (!target.getParentFile().exists()) { + target.mkdirs(); + } FileSystem.getInstance().copy(fileChange.getFile(), target); } catch (IOException e) { - throw new IllegalStateException("Failed to copy " + fileChange.getFile() + " to " + target); + throw new IllegalStateException("Failed to copy " + fileChange.getFile() + " to " + target, e); } break; default: From b3763f2f66666133ff08490c17f2d931b93ce991 Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Wed, 7 Sep 2022 15:16:28 +0200 Subject: [PATCH 3/6] Updating README based on configuration name changes --- byte-buddy-gradle-plugin/android-plugin/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/byte-buddy-gradle-plugin/android-plugin/README.md b/byte-buddy-gradle-plugin/android-plugin/README.md index 840025c7f2a..e3fdf6df557 100644 --- a/byte-buddy-gradle-plugin/android-plugin/README.md +++ b/byte-buddy-gradle-plugin/android-plugin/README.md @@ -11,7 +11,7 @@ run at compile time of the host Android project. In order to add your compiler plugin to an Android project, you'd first need to apply the Byte Buddy Gradle plugin to said Android project, the same way as the plain Java Byte Buddy Gradle plugin is added, shown below. Then you need to -add your compiler plugin as a dependency of the Android project, but said dependency needs to be of type `bytebuddy`. +add your compiler plugin as a dependency of the Android project, but said dependency needs to be of type `byteBuddy`. This custom type of dependency is used at compile time, but won't be present at runtime. So, if your compiler plugin needs to add classes that will be referenced at runtime, then those classes will have to be added as a separate, regular dependency, as shown below. @@ -25,7 +25,7 @@ plugins { } dependencies { - bytebuddy "my.plugin:compiler:0.0.0" + byteBuddy "my.plugin:compiler:0.0.0" implementation "my.plugin:library:0.0.0" } ``` From a0d0be274f6c0b13f748a819b721619cfafc04bb Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Wed, 7 Sep 2022 15:38:37 +0200 Subject: [PATCH 4/6] Reusing variant configurations --- .../android/ByteBuddyAndroidPlugin.java | 70 +++++++++++++------ 1 file changed, 50 insertions(+), 20 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 888634e9631..44d9f228ba2 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 @@ -29,7 +29,13 @@ import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; -import org.gradle.api.attributes.*; +import org.gradle.api.attributes.Attribute; +import org.gradle.api.attributes.AttributeCompatibilityRule; +import org.gradle.api.attributes.AttributeContainer; +import org.gradle.api.attributes.AttributeMatchingStrategy; +import org.gradle.api.attributes.Category; +import org.gradle.api.attributes.CompatibilityCheckDetails; +import org.gradle.api.attributes.Usage; import org.gradle.api.provider.Provider; import org.gradle.api.tasks.TaskProvider; @@ -61,7 +67,7 @@ public void apply(Project project) { } 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, new ByteBuddyDependencyConfiguration(project))); } /** @@ -77,7 +83,7 @@ protected static class VariantAction implements Action { /** * The general Byte Buddy configuration. */ - private final Configuration configuration; + private final ByteBuddyDependencyConfiguration configuration; /** * Creates a new variant action. @@ -85,7 +91,7 @@ protected static class VariantAction implements Action { * @param project The current Gradle project. * @param configuration The general Byte Buddy configuration. */ - protected VariantAction(Project project, Configuration configuration) { + protected VariantAction(Project project, ByteBuddyDependencyConfiguration configuration) { this.project = project; this.configuration = configuration; } @@ -121,7 +127,7 @@ protected static class ByteBuddyTransformationConfiguration implements Function1 /** * The general Byte Buddy configuration. */ - private final Configuration configuration; + private final ByteBuddyDependencyConfiguration configuration; /** * A task provider for a {@link ByteBuddyCopyOutputTask}. @@ -138,11 +144,6 @@ protected static class ByteBuddyTransformationConfiguration implements Function1 */ private final Variant variant; - /** - * A cache for configurations by build type. - */ - private final ConcurrentHashMap configurations; - /** * Creates a new Byte Buddy transformation configuration. * @@ -153,7 +154,7 @@ protected static class ByteBuddyTransformationConfiguration implements Function1 * @param variant The current variant. */ protected ByteBuddyTransformationConfiguration(Project project, - Configuration configuration, + ByteBuddyDependencyConfiguration configuration, TaskProvider byteBuddyCopyOutputTaskProvider, Provider byteBuddyAndroidServiceProvider, Variant variant) { @@ -162,7 +163,6 @@ protected ByteBuddyTransformationConfiguration(Project project, this.byteBuddyCopyOutputTaskProvider = byteBuddyCopyOutputTaskProvider; this.byteBuddyAndroidServiceProvider = byteBuddyAndroidServiceProvider; this.variant = variant; - configurations = new ConcurrentHashMap(); } /** @@ -172,14 +172,7 @@ public Unit invoke(ByteBuddyInstrumentationParameters parameters) { if (variant.getBuildType() == null) { throw new IllegalStateException(); } - Configuration configuration = configurations.get(variant.getBuildType()); - if (configuration == null) { - configuration = project.getConfigurations().create(variant.getBuildType() + "ByteBuddy", new VariantConfigurationConfigurationAction(project, - this.configuration, - variant.getBuildType())); - configurations.put(variant.getBuildType(), configuration); - } - parameters.getByteBuddyClasspath().from(configuration); + parameters.getByteBuddyClasspath().from(configuration.getForBuildType(variant.getBuildType())); parameters.getAndroidBootClasspath().from(project.getExtensions().getByType(BaseExtension.class).getBootClasspath()); parameters.getRuntimeClasspath().from(((ComponentImpl) variant).getVariantDependencies().getArtifactFileCollection(AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, AndroidArtifacts.ArtifactScope.ALL, @@ -313,4 +306,41 @@ public void execute(CompatibilityCheckDetails details) { } } } + + private static class ByteBuddyDependencyConfiguration { + + /** + * The current Gradle project. + */ + private final Project project; + + /** + * The base Byte Buddy configuration. + */ + private final Configuration base; + + /** + * A cache for configurations by build type. + */ + private final ConcurrentHashMap configurations; + + public ByteBuddyDependencyConfiguration(Project project) { + this.project = project; + base = project.getConfigurations().create("byteBuddy", new ConfigurationConfigurationAction()); + configurations = new ConcurrentHashMap(); + } + + public Configuration getForBuildType(String buildType) { + Configuration configuration = configurations.get(buildType); + + if (configuration == null) { + configuration = project.getConfigurations().create(buildType + "ByteBuddy", new VariantConfigurationConfigurationAction(project, + base, + buildType)); + configurations.put(buildType, configuration); + } + + return configuration; + } + } } \ No newline at end of file From 5e6d66afe7618bcd041cf97fb7aef05ab41e0515 Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Wed, 7 Sep 2022 15:44:53 +0200 Subject: [PATCH 5/6] Reintroducing Java 9 validation --- byte-buddy-gradle-plugin/build.gradle | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/byte-buddy-gradle-plugin/build.gradle b/byte-buddy-gradle-plugin/build.gradle index 0c0f97edfd6..d2f9ba47649 100644 --- a/byte-buddy-gradle-plugin/build.gradle +++ b/byte-buddy-gradle-plugin/build.gradle @@ -30,7 +30,9 @@ configurations { dependencies { implementation gradleApi() - fatJar project(':android-plugin') + if (findProject(':android-plugin') != null) { + fatJar project(':android-plugin') + } compileOnly group: 'com.google.code.findbugs', name: 'findbugs-annotations', version: outerPom.properties.'version.utility.findbugs' compileOnly group: 'com.google.code.findbugs', name: 'jsr305', version: outerPom.properties.'version.utility.jsr305' testImplementation gradleTestKit() From e48c26b358d0b87d35b0a137f3ca33034a99b967 Mon Sep 17 00:00:00 2001 From: Cesar Munoz Date: Wed, 7 Sep 2022 18:23:52 +0200 Subject: [PATCH 6/6] Creating parent dir from parent file --- .../bytebuddy/build/gradle/android/ByteBuddyCopyOutputTask.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/ByteBuddyCopyOutputTask.java b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyCopyOutputTask.java index 0a4d72720ef..b80c1620024 100644 --- a/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyCopyOutputTask.java +++ b/byte-buddy-gradle-plugin/android-plugin/src/main/java/net/bytebuddy/build/gradle/android/ByteBuddyCopyOutputTask.java @@ -79,7 +79,7 @@ public void execute(InputChanges inputChanges) { case MODIFIED: try { if (!target.getParentFile().exists()) { - target.mkdirs(); + target.getParentFile().mkdirs(); } FileSystem.getInstance().copy(fileChange.getFile(), target); } catch (IOException e) {