Hi guys, I hope you are doing well. I have an And...
# gradle
d
Hi guys, I hope you are doing well. I have an Android project and created a convention plugin inside of it
Copy code
interface 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:
Copy code
> 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
Copy code
* 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
Copy code
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?
e
why com.android.build.gradle.internal.crash.afterEvaluate? that is not what you want.
d
I mean, that works when I have it in the build.gradle.kts instead of the plugin 🤷
e
also this would probably be better off in the Gradle community, but since publishing doesn't have lazy configuration, you probably need to take a different approach
untested but e.g.
Copy code
interface 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))
d
still the same issue, artifact file doesn't exist, I even hardcoded the extension values to make sure they would be there. sad panda
@ephemient using normal
afterEvaluate
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 😃