-
-
Notifications
You must be signed in to change notification settings - Fork 808
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created the DependenciesClasspathProvider interface and its implement…
…ations
- Loading branch information
1 parent
6a3faca
commit 6ff15e3
Showing
5 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...main/java/net/bytebuddy/build/gradle/android/classpath/DependenciesClasspathProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
37 changes: 37 additions & 0 deletions
37
...t/bytebuddy/build/gradle/android/classpath/impl/DefaultDependenciesClasspathProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...et/bytebuddy/build/gradle/android/classpath/impl/LegacyDependenciesClasspathProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |