Skip to content

Commit

Permalink
Created the DependenciesClasspathProvider interface and its implement…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
LikeTheSalad committed Jan 10, 2023
1 parent 6a3faca commit 6ff15e3
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
2 changes: 1 addition & 1 deletion byte-buddy-gradle-plugin/android-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ repositories {

dependencies {
api gradleApi()
compileOnly group: 'com.android.tools.build', name: 'gradle', version: '7.2.0'
compileOnly group: 'com.android.tools.build', name: 'gradle', version: '7.3.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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.file.FileCollection;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.TaskProvider;
Expand All @@ -47,7 +53,7 @@ public class ByteBuddyAndroidPlugin implements Plugin<Project> {
/**
* The name of the artifact type attribute.
*/
protected static final Attribute<String> ARTIFACT_TYPE_ATTRIBUTE = Attribute.of("artifactType", String.class);
public static final Attribute<String> ARTIFACT_TYPE_ATTRIBUTE = Attribute.of("artifactType", String.class);

/**
* The name of the Byte Buddy jar type.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.bytebuddy.build.gradle.android.classpath;

import com.android.build.api.AndroidPluginVersion;
import com.android.build.api.variant.Variant;
import org.gradle.api.file.FileCollection;

import java.lang.reflect.InvocationTargetException;

/**
* Needed to query the runtime classpath for an Android project, which process has changed in recent versions of the
* AGP plugin, so each method gets its own implementation.
*/
public interface DependenciesClasspathProvider {

/**
* Returns the appropriate {@link DependenciesClasspathProvider} implementation based on the AGP version that the host
* project is running.
*
* @param currentVersion The current AGP version used in the host project.
*/
static DependenciesClasspathProvider getInstance(AndroidPluginVersion currentVersion) {
boolean isLowerThan73 = currentVersion.compareTo(new AndroidPluginVersion(7, 3)) < 0;
try {
if (isLowerThan73) {
return (DependenciesClasspathProvider) Class.forName("net.bytebuddy.build.gradle.android.classpath.impl.LegacyDependenciesClasspathProvider").getDeclaredConstructor().newInstance();
} else {
return (DependenciesClasspathProvider) Class.forName("net.bytebuddy.build.gradle.android.classpath.impl.DefaultDependenciesClasspathProvider").getDeclaredConstructor().newInstance();
}
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

FileCollection getRuntimeClasspath(Variant variant);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.bytebuddy.build.gradle.android.classpath.impl;

import com.android.build.api.variant.Variant;
import net.bytebuddy.build.gradle.android.classpath.DependenciesClasspathProvider;
import org.gradle.api.Action;
import org.gradle.api.artifacts.ArtifactView;
import org.gradle.api.file.FileCollection;

import static net.bytebuddy.build.gradle.android.ByteBuddyAndroidPlugin.ARTIFACT_TYPE_ATTRIBUTE;

/**
* This implementation uses the method {@link Variant#getRuntimeConfiguration()} which was added in AGP version 7.3.0.
*/
public class DefaultDependenciesClasspathProvider implements DependenciesClasspathProvider {

@Override
public FileCollection getRuntimeClasspath(Variant variant) {
return variant.getRuntimeConfiguration().getIncoming()
.artifactView(new JarsViewAction())
.getArtifacts()
.getArtifactFiles();
}

/**
* Needed to query ".jar" files from both, plain Java libraries and Android libraries too. Android libraries
* are files of type ".aar" which contain a ".jar" file inside, without this filter, we'd get Android libraries
* as raw ".aar" files, which cannot be used for Java classpath purposes.
*/
protected static class JarsViewAction implements Action<ArtifactView.ViewConfiguration> {

@Override
public void execute(ArtifactView.ViewConfiguration configuration) {
configuration.setLenient(false);
configuration.getAttributes().attribute(ARTIFACT_TYPE_ATTRIBUTE, "android-classes-jar");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.bytebuddy.build.gradle.android.classpath.impl;

import com.android.build.api.component.impl.ComponentImpl;
import com.android.build.api.variant.Variant;
import com.android.build.gradle.internal.publishing.AndroidArtifacts;
import net.bytebuddy.build.gradle.android.classpath.DependenciesClasspathProvider;
import org.gradle.api.file.FileCollection;

/**
* This implementation is needed for projects running AGP version < 7.3, since the method {@link Variant#getRuntimeConfiguration()}
* was added in AGP 7.3.0. So this legacy implementation uses a workaround due to the missing "getRuntimeConfiguration" method.
*/
public class LegacyDependenciesClasspathProvider implements DependenciesClasspathProvider {

@Override
public FileCollection getRuntimeClasspath(Variant variant) {
return ((ComponentImpl) variant).getVariantDependencies().getArtifactFileCollection(AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH,
AndroidArtifacts.ArtifactScope.ALL,
AndroidArtifacts.ArtifactType.CLASSES_JAR);
}
}

0 comments on commit 6ff15e3

Please sign in to comment.