Djuro
12/15/2023, 3:53 PMinterface MyPluginExtension {
val libraryName: Property<String>
val remoteUrl: Property<String>
}
class MyPlugin : Plugin<Project> {
override fun apply(project: Project) {
with(project) {
extensions.create<MyPluginExtension>("myPluginExtension")
applyLibraryPlugins() // works fine
addAndroidBuildConfiguration() // works fine
afterEvaluate {
addPublishingBuildConfiguration()
}
}
}
......
private val Project.publishing: PublishingExtension
get() = extensions.getByType<PublishingExtension>()
private fun Project.addPublishingBuildConfiguration() {
val cloudProperties =
Properties().apply { load(project.rootProject.file("cloud.properties").inputStream()) }
publishing.apply {
publications {
create<MavenPublication>("release") {
groupId = project.properties["groupId"] as String
version = project.properties["version"] as String
artifactId = myPluginExtension.libraryName.get()
com.android.build.gradle.internal.crash.afterEvaluate {
from(components["release"])
}
artifact("${project.buildDir}/outputs/aar/$artifactId-release.aar") {
classifier = "release"
}
}
}
repositories {
maven {
name = myPluginExtension.libraryName.get()
url = project.uri(myPluginExtension.remoteUrl.get())
credentials {
username = cloudProperties.getProperty("cloud.usr") as String
password = cloudProperties.getProperty("cloud.psw") as String
}
}
}
}
}
Though since private fun Project.addPublishingBuildConfiguration()
is in afterEvaluate
block the artifact is not build when publish is attempted and I am getting the following error:
> Task :sdk:moduleName:publishReleasePublicationToLibrarynameRepository FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':sdk:moduleName:publishReleasePublicationToLibrarynameRepository'.
> Failed to publish publication 'release' to repository 'Libraryname'
> Invalid publication 'release': artifact file does not exist: '/Users/username/AndroidStudioProjects/sdk-home/sdk/modulename/build/outputs/aar/libraryname-release.aar'
On the other hand If I don't put it in afterEvaluate
block, I am getting the issue that I can't access the extension at that point and it's of course is giving the error of
* What went wrong:
An exception occurred applying plugin request [id: 'myplugin']
> Failed to apply plugin 'myplugin'.
> Cannot query the value of extension 'myPluginExtension' property 'libraryName' because it has no value available.
Here is how build.gradle.kts
using this plugin looks like
plugins {
id("myplugin")
}
myPluginExtension {
libraryName.set("lbraryName")
remoteUrl.set("https:someremoteurl")
}
So, how do I access these extensions during configuration 😄? Is there some other approach I should take in order to solve this?ephemient
12/15/2023, 3:55 PMDjuro
12/15/2023, 3:57 PMephemient
12/15/2023, 3:57 PMephemient
12/15/2023, 4:04 PMinterface MyPluginExtension {
fun setLibraryName(libraryName: String)
}
abstract class DefaultMyPluginExtensionImplementation @Inject constructor(private val publications: PublicationContainer) {
override fun setLibraryName(libraryName: String) {
publications.all {
artifactId = libraryName
}
}
}
extensions.add(MyPluginExtension::class, myPluginExtension, object.newInstance<DefaultMyPluginExtensionImplementation>(publishing.publications))
Djuro
12/15/2023, 4:21 PMDjuro
12/18/2023, 9:11 AMafterEvaluate
made it work 😖. There was no need for DefaultMyPluginExtensionImplementation
. I spent the weekend over this and it just started working this morning for some reason, maven publishing is working just fine inside the afterEvaluate
block
I guess that the first time I removed it, I didn't clear the cache properly or something and during copy pasta process it auto-imported it.
It works now nevertheless and I hope it doesn't stops working out of blue 🤷, thank you 😃