Search code examples
androidgradleandroid-gradle-pluginjcenter

Compile library project with javadocs to jar with gradle


I'm currently trying to, and having a little success, in compiling my library project into a .jar file that also contains javadocs.

When i include compile 'com.squareup:otto:1.3.8' in my build.gradle file, i end up with exactly what i want for my own library. When i mouseover functions from the library, i see the javadocs they included, and it's a .jar file.

My current code compiles fine, but i end up with an .aar file (despite not having any resources in my library project), and i also don't have javadocs included (They are written in the project though).

My current build.gradle code:

apply plugin: 'com.android.library'
apply plugin: 'maven'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 23
        versionCode 1
        versionName "0.3.5"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        abortOnError false
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

def groupId = 'com.moonbloom'
def artifactId = 'boast'
def version = '0.3.5'

def localReleaseDest = "${buildDir}/release/${version}"

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
    classifier = 'javadoc'
    from androidJavadocs.destinationDir
}

task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

uploadArchives {
    repositories.mavenDeployer {
        pom.groupId = groupId
        pom.artifactId = artifactId
        pom.version = version
        // Add other pom properties here if you want (developer details / licenses)
        repository(url: "file://${localReleaseDest}")
    }
}

task zipRelease(type: Zip) {
    from localReleaseDest
    destinationDir buildDir
    archiveName "release-${version}.zip"
}

task generateRelease << {
    println "Release ${version} can be found at ${localReleaseDest}/"
    println "Release ${version} zipped can be found ${buildDir}/release-${version}.zip"
}

generateRelease.dependsOn(uploadArchives)
generateRelease.dependsOn(zipRelease)

artifacts {
    archives androidSourcesJar
    archives androidJavadocsJar
}

And then i simply run 'gradlew clean build generateRelease', and i end up with a .zip file i can upload to JCenter (which works fine).

I found this code here on StackOverflow a while ago, and i can't seem to edit it to work as i want, and i can't seem to find other examples of this working properly.

I don't need automatic upload to Bintray (as i've seen quite a few guides for).

This my current uploaded library:
https://bintray.com/moonbloom/android/Boast/v0.3.5#files

This is Otto, as i want it:
https://bintray.com/bintray/jcenter/com.squareup%3Aotto/1.3.8/view#files

I hope someone here can help me.


Solution

  • I managed to fix my problem, in a kind of hacky way i must admit.

    I have my original code (which created the library .aar file and sources/javadocs .jar files)
    I found some additional code which could create my library .jar file.
    Then i wrote some code myself that moved the library .jar file to the correct folder and removed some unneeded files.
    And i also found some code which could easily turn all the gradle functions into one simple function (which was harder than i had imagined).

    This ends up with a perfect .zip file which can easily be uploaded to jcenter, and when i download it by gradle, javadocs work perfectly.

    Simply run 'gradlew all', and it'll do everything.

    My code is shown below, if anyone has the same issue as me:

    apply plugin: 'com.android.library'
    apply plugin: 'maven'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.1"
    
        defaultConfig {
            minSdkVersion 9
            targetSdkVersion 23
            versionCode 1
            versionName "0.4.1"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
        lintOptions {
            abortOnError false
        }
    }
    
    dependencies {
        compile fileTree(dir: "libs", include: ["*.jar"])
    }
    
    def identifier = "com"
    def name = "testname"
    
    def groupId = "${identifier}.${name}"
    def artifactId = "libraryname"
    def version = "0.4.1"
    
    def localReleaseDest = "${buildDir}/release/${version}"
    def nearFullPath = "${localReleaseDest}/${identifier}/${name}/${artifactId}"
    def fullPath = "${nearFullPath}/${version}"
    
    task androidJavadocs(type: Javadoc) {
        source = android.sourceSets.main.java.srcDirs
        classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    }
    
    task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
        classifier = "javadoc"
        from androidJavadocs.destinationDir
    }
    
    task androidSourcesJar(type: Jar) {
        classifier = "sources"
        from android.sourceSets.main.java.srcDirs
    }
    
    task makeJar(type: Copy) {
        from("build/intermediates/bundles/release/")
        into("${fullPath}/")
        include("classes.jar")
        rename ("classes.jar", "${artifactId}-${version}.jar")
    
        doLast {
            println "- '${artifactId}-${version}.jar' has been built"
        }
    }
    
    uploadArchives {
        repositories.mavenDeployer {
            pom.groupId = groupId
            pom.artifactId = artifactId
            pom.version = version
    
            repository(url: "file://${localReleaseDest}")
        }
    }
    
    task removeUnwantedFiles(type: Delete) {
        delete fileTree(dir: "${nearFullPath}/", include: ["*.md5", "*.sha1", "*.aar"])
        delete fileTree(dir: "${fullPath}/", include: ["*.md5", "*.sha1", "*.aar"])
    
        doLast {
            println "- Unwanted files have been removed in these folders:"
            println "--- ${nearFullPath}"
            println "--- ${fullPath}"
        }
    }
    
    task zipRelease(type: Zip) {
        from localReleaseDest
        destinationDir buildDir
        archiveName "${artifactId}-${version}.zip"
    
        doLast {
            println "- Release ${version} can be found at ${localReleaseDest}/"
            println "- Release ${version} zipped can be found ${buildDir}/release-${version}.zip"
        }
    }
    
    artifacts {
        archives androidSourcesJar
        archives androidJavadocsJar
    }
    
    def buildAliases = [
            'all' : ['clean', 'build', 'makeJar', 'uploadArchives', 'removeUnwantedFiles', 'zipRelease']
    ]
    
    def expandedTaskList = []
    
    gradle.startParameter.taskNames.each {
        expandedTaskList << (buildAliases[it] ? buildAliases[it] : it)
    }
    
    gradle.startParameter.taskNames = expandedTaskList.flatten()
    
    println "Following tasks have been run: ${gradle.startParameter.taskNames}"