简体   繁体   English

创建包含 javadoc 和源代码的 Android 库 AAR

[英]Create Android library AAR including javadoc and sources

My team is developing an Android library, to be used in some example applications, also developed by us.我的团队正在开发一个 Android 库,用于一些示例应用程序,也是我们开发的。 The library is created as an AAR file.该库创建为 AAR 文件。 Here is its build.gradle这是它的 build.gradle

apply plugin: 'com.android.library'
apply from: 'deploy.gradle'

android {
    compileSdkVersion 20
    buildToolsVersion '20.0.0'
    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'LICENSE'
        exclude 'NOTICE'
    }
    defaultConfig {
        // Excluded for brevity
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
        // if true, check all issues, including those that are off by default
        checkAllWarnings true
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v13:20.0.+'
    compile 'com.google.android.gms:play-services:5.+'
    compile 'com.squareup.retrofit:retrofit:1.6.1'
}

Here is the dependencies part of the app's build.gradle这是应用程序 build.gradle 的依赖项部分

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile ('com.mycompany.myapp:0.6.5-SNAPSHOT@aar') {
        transitive = true
        downloadJavadoc = true
        downloadSources = true
    }
}

I can use all the lib classes, but I can't see the Javadocs nor the sources.我可以使用所有的 lib 类,但我看不到 Javadoc 和源代码。 How can I package my AAR so it includes the sources and Javadocs?如何打包我的 AAR 以使其包含源代码和 Javadoc?

Seems like it can't be done for now. 好像现在无法做到。 However, adding these gradle tasks in the deploy.gradle , the -javadoc.jar and -sources.jar are created. 但是,在deploy.gradle添加这些gradle任务,将创建-javadoc.jar和-sources.jar。

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
}

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

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

artifacts {
//    archives packageReleaseJar
    archives androidSourcesJar
    archives androidJavadocsJar
}

After that, the sources and javadoc need to be manually imported. 之后,需要手动导入源和javadoc。 According to this thread and this open issue they can't be automatically imported. 根据这个线程这个开放的问题,他们无法自动导入。

This is not a solution but an important note regardless what solution you come up with.这不是一个解决方案,而是一个重要的注意事项,无论您想出什么解决方案。 If you publish your library to Maven, you must make sure the name of the aar, javadocs file and sources all have the same name, otherwise Android Studio will not be able to retrieve the javadocs and let developers move their mouse over a class/function/property to get help.如果您将库发布到 Maven,则必须确保 aar、javadocs 文件和源的名称都具有相同的名称,否则 Android Studio 将无法检索 javadoc 并让开发人员将鼠标移到类/函数上/property 以获得帮助。 For example, if your library is called MyLib and and it includes a version number in the file name such as:例如,如果您的库名为 MyLib 并且它在文件名中包含一个版本号,例如:

MyLib-1.0.0.aar

The javadocs and source files must be named: javadocs 和源文件必须命名为:

MyLib-1.0.0-javadoc.jar
MyLib-1.0.0-sources.jar

This may seem obvious but by default Android builds libraries to include the variant name such as:这似乎很明显,但默认情况下,Android 构建库以包含变体名称,例如:

MyLib-1.0.0-release.aar

If the word "release" doesn't also appear in the filenames for the javadoc and sources, Gradle will fail to retrieve them.如果“发布”一词也没有出现在 javadoc 和源的文件名中,Gradle 将无法检索它们。 The standard naming convention when uploading to Maven is to strip off the variant name before uploading.上传到 Maven 时的标准命名约定是在上传之前剥离变体名称。

I was able to create the sources jar for a library in Kotlin using the below我能够使用以下内容在 Kotlin 中创建源 jar

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.kotlin.sourceFiles
}

task androidJavadocsJar(type: Jar) {
    archiveClassifier.set("javadoc")
    //baseName = artifact_id
    from androidJavadocs.destinationDir
}

task androidSourcesJar(type: Jar) {
    archiveClassifier.set("sources")
    //baseName = artifact_id
    from android.sourceSets.main.kotlin.sourceFiles
}

artifacts {
//    archives packageReleaseJar
    archives androidSourcesJar
    archives androidJavadocsJar
}

I referred the U-crop library.我提到了 U-crop 库。 classifier seems to be deprecated as well.分类器似乎也被弃用了。 using java.sourcefiles created empty src directories.使用 java.sourcefiles 创建空的 src 目录。

This code is added before the android{ } in app > build.gradle这段代码是在 app > build.gradle 中的 android{} 之前添加的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM