Skip to content

Update Gradle configuration to allow resolver to work with aar artifacts#1394

Closed
cheister wants to merge 1 commit intobazel-contrib:masterfrom
cheister:gradle-resolve-aar-artifacts
Closed

Update Gradle configuration to allow resolver to work with aar artifacts#1394
cheister wants to merge 1 commit intobazel-contrib:masterfrom
cheister:gradle-resolve-aar-artifacts

Conversation

@cheister
Copy link
Copy Markdown
Collaborator

@cheister cheister commented Jul 2, 2025

cc: @smocherla-brex

The default gradle "implementation" configuration only resolves "jar" artifacts. I added a new configuration that also resolves "aar" artifacts but might be missing a better way to do this in Gradle?

@cheister cheister force-pushed the gradle-resolve-aar-artifacts branch from a9d4cb2 to 39148a1 Compare July 2, 2025 23:45
attributes {
attribute(
LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
objects.named(LibraryElements, "aar")
Copy link
Copy Markdown
Contributor

@smocherla-brex smocherla-brex Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that this will only fetch AARs (or rather resolve them in the graph) and will break the default configuration of fetching JARs (from runtimeClassPath). I think this might break the other tests.

I thought I had fixed this test, my bad. But I think this could be resolved with an ArtifactView to fetch AARs explicitly. Looking at the gradle module metadata for this https://dl.google.com/android/maven2/androidx/compose/foundation/foundation-android/1.9.0-beta02/foundation-android-1.9.0-beta02.module the dependency constraints seem to be the same for this artifact across variants, just the variant is different with different attribute. You might be able to add something like (I haven't tested to be frank)

ArtifactView aars =
    cfg.getIncoming()
        .artifactView(
            spec -> {
              spec.setLenient(true);
              spec.withVariantReselection(); // force variant reselection
              spec.attributes(
                  attrs -> {
                    attrs.attribute(
                        LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
                        project.getObjects().named(LibraryElements.class, LibraryElements.AAR));
                  });
            });

here and collect the AAR artifact https://github.com/bazel-contrib/rules_jvm_external/blob/master/private/tools/java/com/github/bazelbuild/rules_jvm_external/resolver/gradle/plugin/GradleDependencyModelBuilder.java#L319 and it should probably work and be additive.

A separate configuration would probably work too but it might need to additive from the default configuration. Both can probably be done here in the gradle file or in the plugin itself.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm definitely open to other ways of fixing this.

I tried removing the changes to build.gradle.hbs and adding your changes:

+++ b/private/tools/java/com/github/bazelbuild/rules_jvm_external/resolver/gradle/plugin/GradleDependencyModelBuilder.java
@@ -53,6 +53,7 @@ import org.gradle.api.artifacts.result.ResolvedComponentResult;
 import org.gradle.api.artifacts.result.ResolvedDependencyResult;
 import org.gradle.api.artifacts.result.UnresolvedDependencyResult;
 import org.gradle.api.attributes.Attribute;
+import org.gradle.api.attributes.LibraryElements;
 import org.gradle.api.attributes.Usage;
 import org.gradle.tooling.provider.model.ToolingModelBuilder;
 
@@ -322,10 +323,14 @@ public class GradleDependencyModelBuilder implements ToolingModelBuilder {
                 spec -> {
                   spec.setLenient(true);
                   spec.attributes(
-                      attrs ->
-                          attrs.attribute(
-                              Usage.USAGE_ATTRIBUTE,
-                              project.getObjects().named(Usage.class, Usage.JAVA_API)));
+                      attrs -> {
+                        attrs.attribute(
+                                Usage.USAGE_ATTRIBUTE,
+                                project.getObjects().named(Usage.class, Usage.JAVA_API));
+                        attrs.attribute(
+                                LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
+                                project.getObjects().named(LibraryElements.class, "aar"));
+                      });
                 });
 
     // collect JAR artifacts

but running REPIN=1 bazel run @regression_testing_gradle//:pin still fails with

com.github.bazelbuild.rules_jvm_external.resolver.remote.UriNotFoundException: Unable to download androidx.compose.animation:animation-core:1.2.1 from any of [https://repo1.maven.org/maven2, https://maven.google.com]. Required because: androidx.compose.animation:animation-core:1.2.1
	at com.github.bazelbuild.rules_jvm_external.resolver.cmd.Main.lambda$fulfillDependencyInfos$1(Main.java:149)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1700)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)

Do you know if the ArtifactView works if Gradle says it failed to resolve the dependency with just regular Gradle?

For example if you create a build.gradle file:

apply plugin: 'java'

repositories {
    maven {
        url "https://repo1.maven.org/maven2"
        allowInsecureProtocol = false
    }

    maven {
        url "https://maven.google.com"
        allowInsecureProtocol = false   
    }
}

dependencies {
    implementation("androidx.compose.animation:animation-core:1.2.1")
}

and run gradle :dependencies --configuration runtimeClasspath it returns

runtimeClasspath - Runtime classpath of source set 'main'.
\--- androidx.compose.animation:animation-core:1.2.1 FAILED

which I assume means it didn't even try to download the aar file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me take a quick look

Copy link
Copy Markdown
Contributor

@smocherla-brex smocherla-brex Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I think I understand what's going on. It looks like Android depndencies don't even resolve in the dependency graph with gradle, so using ArtifactView might not help actually. We might have to try to incorporate the android plugin configuration in the gradle build file for this to resolve from the looks of it or use a detached configuration. I'm trying it now, it's a bit tricky, will report back if it works.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright I think I was able to resolve it, with #1395. More details there

@cheister
Copy link
Copy Markdown
Collaborator Author

cheister commented Jul 3, 2025

Closing in favor of #1395

@cheister cheister closed this Jul 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants