Is it possible to publish a library (aar) with mul...
# android
c
Is it possible to publish a library (aar) with multiple variants (i.e. debug and release)? I'm having an issue right now where im trying to publish a library and it keeps generating a BuildConfig with debug = true 😅
t
yeah its pretty automatic these days.
Copy code
android {
    publishing {
        multipleVariants {
            allVariants()
            withSourcesJar()
        }
    }
}
IDE settings has a checkbox to tell it to try and do the multivariant sources etc.
c
Will the different variants have different names? Basically can a single aar contain multiple variants inside of it somehow?
t
doesn't change anything for how you consume things. let me get some screenshots
so this is what is in maven for my
com.aetna.ah.analytics:impl:2025.11.20
artifact. then the module file has all the variant attributes in it so gradle can auto select the right variant from maven automatically just like when you would be doing a
project(":analytics:impl")
p
You need to release a different artifact for each variant with different names. That is when you start hitting scalability walls 🧱
t
honestly this multi variant publishing has only been really usable within the last year. technically the features of gradle have been in place for way longer (i was playing around with it way back in gradle 4/5 days https://github.com/trevjonez/android-components-plugin), but the IDE didn't handle it well. IIRC its only been in the last few months that studio has been able to handle the variant source jar loading correctly.
c
interesting. my artifacts don't have the
-variant
in the name
probably part of some of the confusion
but after some investigative work by me. we were def shipping debug variant thinking it was release variant. lmao. not good. lol
we have other libraries (that are jars. not aars). i wonder if theres an easy way to check if we're releasing the wrong variant of those as well...
hey trev, im trying my hand at the multiple variants declaration and using the docs here https://developer.android.com/build/publish-library/upload-library#groovy how do i setup the publishing block though? the example shows for single release variant
Copy code
publishing {
  publications {
    register<MavenPublication>("release") {
      groupId = "com.my-company"
      artifactId = "my-library"
      version = "1.0"

      afterEvaluate {
        from(components["release"])
      }
    }
  }
}
meanwhile it says "In the preceding example, the name of the component (
components.release
) is based on the name that was given to either
singleVariant()
or
multipleVariants()
." how would i write the afterEvaluate with your example snippet android { publishing { multipleVariants { allVariants() withSourcesJar() } } }
t
Copy code
publishing.publications {
    register<MavenPublication>("aar") {
        project.components.configureEach {
            if (name == "default") {
                from(this)
            }
        }
        suppressAllPomMetadataWarnings()
    }
}
c
interesting! all of this config seems like theres a bunch of footguns. lol. filed this https://issuetracker.google.com/issues/462911908 after googling myself i ended up with a publications block that looks pretty different from yours. but let me try that and look at the generated outputs to see if there is something fundamentally differnet
t
I suppose you could say foot guns. but really this is just a complicated part of gradle mixing with two different plugin boundaries. there will likely always be goblins
c
got it. as a noob here it just seems like the tooling should be able to prevent me from shipping debug when i say release somewhere else 😂
t
maybe. hard part is is not forcing one way of doing things on the consumer. Personally I am a fan of always doing multi variant publishing so that pulling from maven provides as close to 1:1 as doing a project dep / composite build
👍 1