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" } ``` 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 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..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 @@ -78,9 +78,12 @@ public void execute(InputChanges inputChanges) { case ADDED: case MODIFIED: try { + if (!target.getParentFile().exists()) { + target.getParentFile().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: diff --git a/byte-buddy-gradle-plugin/build.gradle b/byte-buddy-gradle-plugin/build.gradle index b325f00f1ae..d2f9ba47649 100644 --- a/byte-buddy-gradle-plugin/build.gradle +++ b/byte-buddy-gradle-plugin/build.gradle @@ -26,14 +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) { + if (findProject(':android-plugin') != null) { fatJar project(':android-plugin') } compileOnly group: 'com.google.code.findbugs', name: 'findbugs-annotations', version: outerPom.properties.'version.utility.findbugs'