generate Javadoc terminal with gradle for android java library
July 19th 2024

Step-by-Step Configuration

 

1. Add Javadoc Task: Configure the Javadoc task.

2. Include Dependencies: Ensure that the dependencies are included in the classpath.

3. Handle Exclusions: Exclude unnecessary files from the Javadoc generation.

 

Here’s an example build.gradle configuration for an Android Java library:

apply plugin: 'com.android.library'
//apply plugin: 'java'

android {
    compileSdkVersion 33
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 1
        versionName "1.0"
    }

    sourceSets {
        main {
            java.srcDirs = ['src/main/java']
        }
    }
}

dependencies {
    // Add your dependencies here
    implementation 'com.android.support:appcompat-v7:33.1.0'
    implementation 'com.google.code.gson:gson:2.8.9'
}

task androidJavadocs(type: Javadoc) {
    failOnError false
    source = android.sourceSets.main.java.srcDirs
    classpath += files(android.getBootClasspath().join(File.pathSeparator))
    classpath += files(project.android.libraryVariants.collect { variant ->
        variant.javaCompileProvider.get().classpath.files
    })

    exclude '**/R.html', '**/R.*.html', '**/index.html'
}

// Ensure Javadoc task runs after the project is evaluated
afterEvaluate {
    android.libraryVariants.all { variant ->
        def javaCompile = variant.javaCompileProvider.get()
        androidJavadocs.dependsOn javaCompile
        androidJavadocs.classpath += javaCompile.classpath
    }
}

 

Explanation

 

1. Apply Plugins: Apply the necessary plugins for the Android library and Java.

2. Configure Android: Set up the Android block with compileSdkVersion, minSdkVersion, and other configurations.

3. Define Dependencies: Add dependencies required for your library.

4. Javadoc Task: Create a Javadoc task named androidJavadocs:

failOnError: Set to false to continue build even if there are Javadoc errors.

source: Set to the Java source directories.

classpath: Add the Android boot classpath and the classpath from the library variants.

exclude: Exclude unnecessary files like R.html.

5. After Evaluate: Ensure the Javadoc task runs after the project is fully evaluated and all classpaths are set correctly.

 

Running the Task

 

Run the task using:

./gradlew androidJavadocs

Additional Tips

Customizing Javadoc: You can customize the Javadoc generation by adding more options to the androidJavadocs task, such as specifying the destination directory or adding additional options.

Dependencies: Ensure all dependencies are correctly specified in the dependencies block to avoid missing classes during Javadoc generation.

 

This configuration ensures that the Javadoc is generated correctly, including all necessary dependencies, for an Android Java library project.
 

"if got error "path may not be null or empty string. path=''"

tasks.register('generateJavadoc', Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))

    // Include dependencies in the classpath
    android.libraryVariants.all { variant ->
        if (variant.name == 'release') {
            classpath += variant.javaCompileProvider.get().classpath
        }
    }

    // Exclude generated files
    exclude '**/R.java'
    exclude '**/BuildConfig.java'

    options {
        links "https://docs.oracle.com/javase/8/docs/api/"
        linksOffline "https://developer.android.com/reference", "${android.sdkDirectory}/docs/reference"
    }

    // Specify the output directory for the generated documentation
    destinationDir = file("${project.buildDir}/docs/javadoc/")

    // Set title for the generated documentation
    title = "${project.name} ${project.version} API"

    // Optional: Set to false if you want to see warnings
    failOnError false
}

run script:

./gradlew generateJavadoc

 

Huy Le
Huy Le
[email protected]

Full-stack developer passionate about React and react native, firebase

Tags
Java
gradle
javadoc
gradle 8.x
java18
Social Share
Loading...