Mike Schvedov
Mike Schvedov

Reputation: 156

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.

Upvotes: 2

Views: 4166

Answers (2)

Spiker
Spiker

Reputation: 575

I spent almost 2 days figuring out this solution for generating aar, pom , sha files needed for aar publishing in any artifactory.

this worked for me using latest Android versions. as of Jul 8th, 2024

build.gradle (Module)

plugins {
    .
    .
    id("maven-publish")
}

depedndencis{
.
.
.

}
def libraryGroupId = 'com.example'
def libraryArtifactId = 'sdk'
def libraryVersion = '1.0.0'

publishing {
    publications {
        maven(MavenPublication) {
            //from components.android
            groupId libraryGroupId
            artifactId libraryArtifactId
            version libraryVersion

            afterEvaluate {
                from components.release
            }
        }
    }
    repositories {
        maven {
            url "$buildDir/releases"
        }
    }
}

Finally run this below command and you can see your artifacts generated in your sdk module /build/releases/ folder.

./gradlew publish

Hope this helps every one to solve this use case of generating artifacts locally using maven

Upvotes: 1

Mike Schvedov
Mike Schvedov

Reputation: 156

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'
}

Upvotes: 3

Related Questions