androidandroid-studiomavenjavadocaar

How do I publish an AAR to Maven Local With JavaDocs


I need to publish my android library (aar) using Gradle to Maven local repo. But the publication script needs to also generate the Javadocs, while ONLY including Public and Protected methods and classes.

Can't seem to find any information online, especially about the Javadocs part... Help, I never published a library before.


Solution

  • Ok, after much research I found a solution, so I'm going to share it here if anyone will need this.
    (I don't want you to be frustrated like I was).

    1) Create an android library as a new module inside your project.

    2) Inside the build gradle of your library place this code:

    plugins {
        id 'com.android.library'
        id 'maven-publish'
    }
    
    android {
       nothing special here...
    }
    

    This is the code for creating the Javadocs(still inside build.gradle):

    task androidJavadocs(type: Javadoc){
        source = android.sourceSets.main.java.srcDirs
    
        classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
        android.libraryVariants.all{ variant->
            if (variant.name == 'release'){
                owner.classpath += variant.javaCompileProvider.get().classpath
            }
        }
        // excluding a specific class from being documented
        exclude '**/NameOfClassToExclude.java'
    
        title = null
    
        options{
            doclet = "com.google.doclava.Doclava"
            docletpath = [file("libs/doclava-1.0.6.jar")]
            noTimestamp = false
    
            // show only Protected & Public
            memberLevel = JavadocMemberLevel.PROTECTED
        }
    
    }
    
    task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs){
        archiveClassifier.set('javadoc')
        from androidJavadocs.destinationDir
    }
    
    
    task androidSourcesJar(type: Jar){
        archiveClassifier.set('sources')
        from android.sourceSets.main.java.srcDirs
    }
    

    This is to publish the library to MavenLocal(still inside build.gradle):

       afterEvaluate {
            publishing{
                publications{
                    release(MavenPublication){
                        groupId = "com.example.mylibrary"
                        artifactId = "mycoollibrary"
                        version = "1.0"
                        // Applies the component for the release build variant
                        from components.release
                        // Adds javadocs and sources as separate jars.
                        artifact androidSourcesJar
                        artifact androidJavadocsJar
                    }
                }
            }
        }
    

    Your default dependencies block:

    dependencies {
           your dependencies...
        }
    

    3) Now you can download the doclava doclet:
    Extract the zip, copy the doclava-1.0.6.jar and paste it into your LibraryName/libs folder (can be found using the project view). You only need doclava if you want to be able to use @hide. With this annotation, you can exclude specific methods from your Javadocs.

    4) Build and publish your library: Find the gradle tab at the top right side of android studio, or find it from the toolbar View->Tool Windows->Gradle.
    Now find your library -> tasks -> publishing -> publishReleasePublicationToMavenLocal.

    5) To consume the library from another project: Go to the settings.gradle file (of the consuming project) and add MavenLocal() as the first repository in the the dependencyResolutionManagement block.
    And inside the module build gradle add your library as a dependency:

    dependencies{
        implementation 'com.example.mylibrary:mycoollibrary:1.0'
    }