Wrap JavadocJar and SourcesJar via gradle standalone plugin

I am building a standalone gradle and for some reason I want to wrap the process of creating publication and including sourcesJar and javadocJar inside this plugin. The publication is created fine but I have problems with javadoc and sources tasks. Here is the code for javadocJar:

 private fun addJavadocJarTask(project: Project) {
        project.afterEvaluate {
            val javadocTask: Task? = project.tasks.findByPath("javadoc") <-- This returns null
            val javadocJar: Jar = project.tasks.create("javadocJar", Jar::class.java)
            javadocJar.dependsOn(javadocTask) <-- Here gradle complains that dependency cannot be empty
            javadocJar.classifier = "javadoc"
            val javadoc: Javadoc? = javadocTask as Javadoc
            javadocJar.from(javadoc?.destinationDir)
        }
    }

and here is the sourcesJar:

private fun addSourcesJar(project: Project) {
        project.afterEvaluate {
            val javaConvention: JavaPluginConvention =
                project.convention.getPlugin(JavaPluginConvention::class.java)
            val sourceSet: SourceSet = javaConvention.sourceSets.getByName("main") <-- Here gradle complains that there is no sourceSet called "main"
            val sourcesJar: Jar = project.tasks.create("sourcesJar", Jar::class.java)
            sourcesJar.classifier = "sources"
            sourcesJar.from(sourceSet.allSource)
        }
    }

Please not that the project I want to use the plugin with is Android library so java plugins cannot be used.

If I add in build.gradle those lines:

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}
// Generate javadoc
task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

it works fine but when I try to add those tasks in plugin then it does not.

Any ideas? Is there some way to call defined tasks in build.gradle from plugin?