Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle withJavadocJar() and withSourcesJar() for android library

Tags:

android

gradle

Is there a way to leverage the new functions withSourcesJar() and withJavadocJar() for Android library projects? Currently when I try to use it I get:

> SourceSet with name 'main' not found.
like image 895
ligi Avatar asked Nov 27 '19 23:11

ligi


People also ask

What is ProcessResources in gradle?

Class ProcessResourcesCopies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.

What is jar task in gradle?

You can control when a jar is built with something like this: jar { onlyIf { condition } } You can set jar to be built when you declare something else to be true, or hard set it to false to never build a jar. You can include sourcesets in your toJar custom task, to include compiled files into your jar.

What is runtimeClasspath in gradle?

main. runtimeClasspath' contains the compiled classes of the source set, and task autowiring automatically adds the necessary task dependencies. Maybe you want 'sourceSets. main. compileClasspath'.


2 Answers

With the latest Gradle version 6.0.1, there seems to be no way to use these new methods in Android library projects. Here’s why I believe that’s the case:

  • The two methods withJavadocJar() and withSourcesJar() on the java extension have the default main source set hardcoded, see here and here.

  • There are two methods with the same names (withJavadocJar() and withSourcesJar()) which can be used in feature variant declarations. However, it seems that Android Gradle builds don’t use feature variants, i.e., these methods can’t be used either.

like image 117
Chriki Avatar answered Sep 28 '22 19:09

Chriki


The documentation states, that these come from the JavaPluginExtension - but not from Android DSL. So they can only be used in conjunction with apply plugin: "java" or apply plugin: "java-library", but not with apply plugin: "com.android.application", apply plugin: "com.android.library" and alike. The name of these tasks also suggest that it's common Java (*.jar) and not Android (*.aar). On Android, this would only make sense for a *.jar library, which uses pure Java features, but no Android features at all (which is limited in functionality).

In short, apply plugin: "java-library" would permit accessing these.

like image 22
Martin Zeitler Avatar answered Sep 28 '22 19:09

Martin Zeitler