Build failure when using JAR from artifact with AAR packaging in POM
Context
The Microsoft Cognitive Services Speech SDK for Java is released in two formats:
- .aar package (e.g., client-sdk-1.45.0.aar) for Android development, containing Android-specific files
- .jar package (e.g., client-sdk-1.45.0.jar) for non-Android development, without Android-specific files
Both formats are available in the Maven repository: https://repo1.maven.org/maven2/com/microsoft/cognitiveservices/speech/client-sdk/1.45.0/
However, its POM file declares <packaging>aar</packaging>.
Problem Description
When trying to use the JAR format of the Speech SDK in a non-Android Bazel project, the build fails with AAR-related errors even when explicitly specifying :jar packaging in the Maven coordinate.
Steps to Reproduce
- Use Maven coordinate with explicit JAR packaging:
bazel_dep(name = "rules_jvm_external", version = "6.8")
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
artifacts = [
"com.microsoft.cognitiveservices.speech:client-sdk:jar:1.45.0"
],
repositories = ["https://repo1.maven.org/maven2"],
)
use_repo(maven, "maven")
- Run bazel build
Error Output
ERROR: Traceback (most recent call last):
File "/external/rules_jvm_external++maven+maven/BUILD", line 695, column 11, in <toplevel>
aar_import(
Error in aar_import: Couldn't auto load 'aar_import' from '@rules_android//rules:rules.bzl'. Ensure that you have a 'bazel_dep(name = "rules_android", ...)' in your MODULE.bazel file or add an explicit load statement to your BUILD file.
Question
Does rules_jvm_external have similar functionality to Gradle's ext parameter to specify packaging type, or can it read the packaging directly from Maven coordinates (the :jar part) to override POM packaging declaration?
Comparison with Gradle
Gradle handles this correctly with the ext parameter:
dependencies {
implementation group: 'com.microsoft.cognitiveservices.speech',
name: 'client-sdk',
version: '1.45.0',
ext: 'jar'
}
This successfully uses the JAR file even though the POM declares <packaging>aar</packaging>.
You should be able to declare the dependency as com.microsoft.cognitiveservices.speech:client-sdk:1.45.0@jar.
The problem is that jar is the default classifier for java dependencies, and so a lot of code treats jar and the empty string as the same thing. It's likely that the resolver is being passed com.microsoft.cognitiveservices.speech:client-sdk:1.45.0, which will then pick up the default packaging from the pom file. Even if that weren't the case, there's logic in our supporting classes that makes that assumption.
I suspect fixing this will be a challenge, but it's something that we should fix.