2

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

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

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

我当前的 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”,最后得到一个可以上传到 JCenter 的 .zip 文件(工作正常)。

不久前,我在 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

我希望这里有人可以帮助我。

4

2 回答 2

1

我设法以一种我必须承认的骇人听闻的方式解决了我的问题。

我有我的原始代码(它创建了库 .aar 文件和源/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}"
于 2015-09-16T13:05:56.770 回答
0

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

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

于 2015-09-13T23:38:18.670 回答