Skip to content

Commit

Permalink
Clean up Android fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Oct 4, 2023
1 parent a901306 commit d597723
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.scaffold.inline.MethodNameTransformer;
import net.bytebuddy.utility.QueueFactory;
import net.bytebuddy.utility.nullability.MaybeNull;
import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
Expand All @@ -46,7 +47,13 @@

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
Expand Down Expand Up @@ -160,7 +167,7 @@ public void execute() {
try {
buildLogger = (BuildLogger) Class.forName("net.bytebuddy.build.gradle.GradleBuildLogger")
.getConstructor(Logger.class)
.newInstance(LOGGER);
.newInstance(getProject().getLogger());
} catch (Exception exception) {
throw new GradleException("Failed to resolve Gradle build logger", exception);
}
Expand All @@ -181,7 +188,7 @@ public void execute() {
}
}
Plugin.Engine.Summary summary = Plugin.Engine.Default.of(new EntryPoint.Unvalidated(EntryPoint.Default.DECORATE),
classFileVersion,Logger
classFileVersion,
MethodNameTransformer.Suffixing.withRandomSuffix())
.with(classFileLocator)
.apply(new Plugin.Engine.Source.Compound(sources), new TargetForAndroidAppJarFile(getOutputFile().get().getAsFile()), factories);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ protected ByteBuddyTransformationConfiguration(Project project,
* {@inheritDoc}
*/
public Unit invoke(ByteBuddyInstrumentationParameters parameters) {
parameters.getByteBuddyClasspath().from(getByteBuddyClasspath(project, configuration));
parameters.getByteBuddyClasspath().from(ByteBuddyViewConfiguration.toClassPath(project, configuration));
parameters.getAndroidBootClasspath().from(project.getExtensions().getByType(BaseExtension.class).getBootClasspath());
parameters.getRuntimeClasspath().from(classPath);
parameters.getByteBuddyService().set(byteBuddyAndroidServiceProvider);
Expand Down Expand Up @@ -482,7 +482,9 @@ enum ForLegacyAndroid implements TransformationDispatcher {
public void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath) {
TaskProvider<LegacyByteBuddyLocalClassesEnhancerTask> provider = project.getTasks().register(variant.getName() + "BytebuddyLocalTransform",
LegacyByteBuddyLocalClassesEnhancerTask.class,
new LegacyByteBuddyLocalClassesEnhancerTask.ConfigurationAction(getByteBuddyClasspath(project, configuration), project.getExtensions().getByType(BaseExtension.class), classPath));
new LegacyByteBuddyLocalClassesEnhancerTask.ConfigurationAction(ByteBuddyViewConfiguration.toClassPath(project, configuration),
project.getExtensions().getByType(BaseExtension.class),
classPath));
variant.getArtifacts()
.use(provider)
.wiredWith(GetLocalClassesFunction.INSTANCE, GetOutputDirFunction.INSTANCE)
Expand Down Expand Up @@ -579,7 +581,7 @@ protected ForApk74CompatibleAndroid(Method forScope, Method use, Method toTransf
public void accept(Project project, Variant variant, Configuration configuration, FileCollection classPath) {
TaskProvider<ByteBuddyLocalClassesEnhancerTask> provider = project.getTasks().register(variant.getName() + "BytebuddyTransform",
ByteBuddyLocalClassesEnhancerTask.class,
new ByteBuddyLocalClassesEnhancerTask.ConfigurationAction(getByteBuddyClasspath(project, configuration), project.getExtensions().getByType(BaseExtension.class)));
new ByteBuddyLocalClassesEnhancerTask.ConfigurationAction(ByteBuddyViewConfiguration.toClassPath(project, configuration), project.getExtensions().getByType(BaseExtension.class)));
try {
toTransform.invoke(use.invoke(forScope.invoke(variant.getArtifacts(), scope), provider),
artifact,
Expand Down Expand Up @@ -660,19 +662,32 @@ public RegularFileProperty invoke(ByteBuddyLocalClassesEnhancerTask task) {
}

/**
* For external dependencies, it provides their JAR files. For local project's dependencies, it provides their local
* build dirs for both classes and resources. The latter allows for faster and more reliable (up-to-date) compilation processes
* when using local plugins.
* A view configuration for creating the Byte Buddy class path.
*/
private static FileCollection getByteBuddyClasspath(Project project, Configuration byteBuddyConfiguration) {
FileCollection resources = byteBuddyConfiguration.getIncoming().artifactView(viewConfiguration -> {
viewConfiguration.lenient(false);
viewConfiguration.getAttributes().attribute(ARTIFACT_TYPE_ATTRIBUTE, BYTE_BUDDY_RESOURCES_TYPE);
}).getFiles();
FileCollection classes = byteBuddyConfiguration.getIncoming().artifactView(viewConfiguration -> {
viewConfiguration.lenient(false);
viewConfiguration.getAttributes().attribute(ARTIFACT_TYPE_ATTRIBUTE, BYTE_BUDDY_CLASSES_TYPE);
}).getFiles();
return project.files(classes, resources);
protected enum ByteBuddyViewConfiguration implements Action<ArtifactView.ViewConfiguration> {

/**
* The singleton instance.
*/
INSTANCE;

/**
* For external dependencies, it provides their JAR files. For local project's dependencies, it provides their local
* build dirs for both classes and resources. The latter allows for faster and more reliable (up-to-date) compilation processes
* when using local plugins.
*/
protected static FileCollection toClassPath(Project project, Configuration configuration) {
return project.files(
configuration.getIncoming().artifactView(INSTANCE).getFiles(),
configuration.getIncoming().artifactView(INSTANCE).getFiles());
}

/**
* {@inheritDoc}
*/
public void execute(ArtifactView.ViewConfiguration configuration) {
configuration.lenient(false);
configuration.getAttributes().attribute(ARTIFACT_TYPE_ATTRIBUTE, BYTE_BUDDY_RESOURCES_TYPE);
}
}
}

0 comments on commit d597723

Please sign in to comment.