Skip to content

Commit

Permalink
Using DependenciesClasspathProvider implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
LikeTheSalad committed Jan 10, 2023
1 parent 6ff15e3 commit e8c15a6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,12 +65,14 @@ public class ByteBuddyAndroidPlugin implements Plugin<Project> {
public void apply(Project project) {
@SuppressWarnings("unchecked")
AndroidComponentsExtension<?, ?, Variant> 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)));
}

/**
Expand All @@ -89,6 +90,11 @@ protected static class VariantAction implements Action<Variant> {
*/
private final Configuration configuration;

/**
* The runtime classpath provider.
*/
private final DependenciesClasspathProvider classpathProvider;

/**
* A cache of configurations by built type name.
*/
Expand All @@ -97,12 +103,14 @@ protected static class VariantAction implements Action<Variant> {
/**
* 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<String, Configuration>();
}

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 |
Expand Down

0 comments on commit e8c15a6

Please sign in to comment.