Skip to content

Commit

Permalink
Merge pull request #1317 from LikeTheSalad/master
Browse files Browse the repository at this point in the history
Merger master
  • Loading branch information
raphw committed Sep 8, 2022
2 parents 691bcd7 + e48c26b commit 94d7602
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
4 changes: 2 additions & 2 deletions byte-buddy-gradle-plugin/android-plugin/README.md
Expand Up @@ -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.
Expand All @@ -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"
}
```
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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)));
}

/**
Expand All @@ -77,15 +83,15 @@ protected static class VariantAction implements Action<Variant> {
/**
* The general Byte Buddy configuration.
*/
private final Configuration configuration;
private final ByteBuddyDependencyConfiguration configuration;

/**
* Creates a new variant 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;
}
Expand Down Expand Up @@ -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}.
Expand All @@ -138,11 +144,6 @@ protected static class ByteBuddyTransformationConfiguration implements Function1
*/
private final Variant variant;

/**
* A cache for configurations by build type.
*/
private final ConcurrentHashMap<String, Configuration> configurations;

/**
* Creates a new Byte Buddy transformation configuration.
*
Expand All @@ -153,7 +154,7 @@ protected static class ByteBuddyTransformationConfiguration implements Function1
* @param variant The current variant.
*/
protected ByteBuddyTransformationConfiguration(Project project,
Configuration configuration,
ByteBuddyDependencyConfiguration configuration,
TaskProvider<ByteBuddyCopyOutputTask> byteBuddyCopyOutputTaskProvider,
Provider<ByteBuddyAndroidService> byteBuddyAndroidServiceProvider,
Variant variant) {
Expand All @@ -162,7 +163,6 @@ protected ByteBuddyTransformationConfiguration(Project project,
this.byteBuddyCopyOutputTaskProvider = byteBuddyCopyOutputTaskProvider;
this.byteBuddyAndroidServiceProvider = byteBuddyAndroidServiceProvider;
this.variant = variant;
configurations = new ConcurrentHashMap<String, Configuration>();
}

/**
Expand All @@ -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,
Expand Down Expand Up @@ -313,4 +306,41 @@ public void execute(CompatibilityCheckDetails<String> 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<String, Configuration> configurations;

public ByteBuddyDependencyConfiguration(Project project) {
this.project = project;
base = project.getConfigurations().create("byteBuddy", new ConfigurationConfigurationAction());
configurations = new ConcurrentHashMap<String, Configuration>();
}

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;
}
}
}
Expand Up @@ -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:
Expand Down
5 changes: 1 addition & 4 deletions byte-buddy-gradle-plugin/build.gradle
Expand Up @@ -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'
Expand Down

0 comments on commit 94d7602

Please sign in to comment.