-
-
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.
Merge pull request #1389 from LikeTheSalad/master
Providing Android runtimeClasspath using new tools from API 7.3
- Loading branch information
Showing
6 changed files
with
234 additions
and
17 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
56 changes: 56 additions & 0 deletions
56
...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,56 @@ | ||
/* | ||
* Copyright 2014 - Present Rafael Winterhalter | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
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 org.gradle.api.logging.Logger; | ||
import org.gradle.api.logging.Logging; | ||
|
||
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; | ||
Logger logger = Logging.getLogger(DependenciesClasspathProvider.class); | ||
try { | ||
if (isLowerThan73) { | ||
logger.debug("Using legacy classpath provider implementation"); | ||
return (DependenciesClasspathProvider) Class.forName("net.bytebuddy.build.gradle.android.classpath.impl.LegacyDependenciesClasspathProvider").getDeclaredConstructor().newInstance(); | ||
} else { | ||
logger.debug("Using default classpath provider implementation"); | ||
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); | ||
} |
52 changes: 52 additions & 0 deletions
52
...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,52 @@ | ||
/* | ||
* Copyright 2014 - Present Rafael Winterhalter | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
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"); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...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,36 @@ | ||
/* | ||
* Copyright 2014 - Present Rafael Winterhalter | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
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); | ||
} | ||
} |
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