使用javadocs编译库项目以使用gradle进行jar

时间:2015-09-13 23:08:39

标签: android gradle android-gradle jcenter

我目前正在尝试将我的库项目编译成一个也包含javadocs的.jar文件,并取得了一些成功。

当我在我的build.gradle文件中包含compile 'com.squareup:otto:1.3.8'时,我最终得到了我想要的自己的库。当我从库中鼠标悬停函数时,我看到它们包含的javadocs,它是一个.jar文件。

我当前的代码编译得很好,但我最终得到一个.aar文件(尽管我的库项目中没有任何资源),而且我也没有包含javadoc(虽然它们是在项目中编写的)。 / p>

我当前的build.gradle代码:

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
}

然后我只是运行'gradlew clean build generateRelease',我最终得到一个.zip文件,我可以上传到JCenter(工作正常)。

我刚才在StackOverflow上找到了这个代码,我似乎无法按照我的意愿编辑它,我似乎无法找到其他正常工作的例子。

我不需要自动上传到Bintray(因为我已经看过很多指南)。

这是我目前上传的图书馆:
https://bintray.com/moonbloom/android/Boast/v0.3.5#files

这是奥托,因为我想要它:
https://bintray.com/bintray/jcenter/com.squareup%3Aotto/1.3.8/view#files

我希望有人可以帮助我。

2 个答案:

答案 0 :(得分:1)

我设法以一种我必须承认的hacky方式解决我的问题。

我有原始代码(创建库.aar文件和sources / javadocs .jar文件)
我找到了一些可以创建我的库.jar文件的附加代码 然后我自己编写了一些代码,将库.jar文件移动到正确的文件夹中并删除了一些不需要的文件 我还发现了一些代码可以轻松地将所有gradle函数转换为一个简单的函数(这比我想象的更难)。

最终得到一个完美的.zip文件,可以轻松上传到jcenter,当我通过gradle下载时,javadocs可以正常工作。

只需运行'gradlew all',即可完成所有操作。

如果有人遇到与我相同的问题,我的代码如下所示:

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

答案 1 :(得分:0)

如果您不想将项目编译为库,请更改此行:

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