如何将文档字符串添加到 android maven-publish .aar 文件中 build.gradle.kts?

How can I add docstrings to android maven-publish .aar files in build.gradle.kts?

我有一个带有标准 java 库的项目,它创建了几个 .jar 文件,一个是文档集,另一个是源集。现在我已经使用 maven-publish 插件成功创建了一个 android-library,但是当我将 .aar 文件添加到另一个项目时,它们既不包含源也不包含注释,因为 .jar 版本,所以我没有在 IDE 中获得帮助或查看方法的实现。

我需要在我的 build.gradle.kts 中添加什么才能在 .aar 我作为文件在本地发布的调试版本中包含文档字符串和源代码?来自 Android 开发者页面的链接 gradle 文档根本没有提及任何关于文档字符串或来源的内容。

import org.jetbrains.kotlin.config.KotlinCompilerVersion

plugins {
    id("com.android.library")
    kotlin("android")
    `maven-publish`
}

group = "com.wavelt.libs"
version = "1.0.0"


android {
    compileSdkVersion(30)
    buildToolsVersion = "30.0.2"
    defaultConfig {
        minSdkVersion(16)
        targetSdkVersion(30)
        versionCode = 1
        versionName = "1.0.0"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles("consumer-rules.pro")
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
        freeCompilerArgs = listOf("-Xinline-classes")
    }
}

dependencies {
    implementation(kotlin("stdlib-jdk7", KotlinCompilerVersion.VERSION))
    implementation("androidx.core:core-ktx:1.3.2")
    implementation("androidx.appcompat:appcompat:1.2.0")
    implementation("com.google.android.material:material:1.3.0")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.2")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
    androidTestImplementation("com.android.support.test:runner:1.0.2")
    androidTestImplementation("com.android.support.test:rules:1.0.2")
}

// https://developer.android.com/studio/build/maven-publish-plugin
afterEvaluate {
    publishing {
        repositories {
            maven {
                // https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:repositories
                url = uri("$buildDir/repository")
            }
        }
        publications {
            create<MavenPublication>("debug") {
                // Applies the component for the release build variant.
                from(components["debug"])
                artifactId = "wavelt-android-debug"
                artifact("androidJavadocsJar") // Doesn't seem to work
            }
            create<MavenPublication>("release") {
                // Applies the component for the release build variant.
                from(components["release"])
                artifactId = "wavelt-android"
            }
        }
    }
}

虽然我读到过其他开发人员能够将 java 文档塞进 .aar 文件中,但这对于从内部获取文档和源代码检查来说并不是明确必要的 IDE就像 Android 工作室。事实上,看看典型的 java 库的工作方式,它会创建如下文件:

  • foo-ver.jar
  • foo-ver-sources.jar
  • foo-ver-javadoc.jar

与 Android 库的唯一区别是拥有这些文件:

  • foo-ver.aar
  • foo-ver-sources.jar
  • foo-ver-javadoc.jar

这意味着源代码和 javadoc jar 仍然可以沿 .aar 复制,IDE 将加载它们。话虽如此,发布示例代码仅创建 .aar 文件,查看 other questions like this one 我能够修改脚本以创建 .aar 加上另外两个 .jar 包:

import org.jetbrains.kotlin.config.KotlinCompilerVersion

plugins {
    id("com.android.library")
    kotlin("android")
    `maven-publish`
    id("org.jetbrains.dokka") version "0.9.17"
}

group = "com.wavelt.libs"
version = "1.0.0"


android {
    compileSdkVersion(30)
    buildToolsVersion = "30.0.2"
    defaultConfig {
        minSdkVersion(16)
        targetSdkVersion(30)
        versionCode = 1
        versionName = "1.0.0"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles("consumer-rules.pro")
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
        freeCompilerArgs = listOf("-Xinline-classes")
    }
}

dependencies {
    //implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
    implementation(kotlin("stdlib-jdk7", KotlinCompilerVersion.VERSION))
    implementation("androidx.core:core-ktx:1.3.2")
    implementation("androidx.appcompat:appcompat:1.2.0")
    implementation("com.google.android.material:material:1.3.0")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.2")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
    androidTestImplementation("com.android.support.test:runner:1.0.2")
    androidTestImplementation("com.android.support.test:rules:1.0.2")
}

tasks.dokka {
    outputFormat = "html"
    outputDirectory = "$buildDir/javadoc"
    moduleName = rootProject.name
}

val dokkaJar by tasks.creating(Jar::class) {
    group = JavaBasePlugin.DOCUMENTATION_GROUP
    description = "Assembles Kotlin docs with Dokka"
    archiveClassifier.set("javadoc")
    from(tasks.dokka)
    dependsOn(tasks.dokka)
}

val sourcesJar by tasks.creating(Jar::class) {
    group = JavaBasePlugin.DOCUMENTATION_GROUP
    description = "Assembles sources JAR"
    archiveClassifier.set("sources")
    from(android.sourceSets.getByName("main").java.srcDirs)
}

artifacts {
    archives(sourcesJar)
    archives(dokkaJar)
}

// https://developer.android.com/studio/build/maven-publish-plugin
afterEvaluate {
    publishing {
        repositories {
            maven {
                // https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:repositories
                url = uri("$buildDir/repository")
            }
        }
        publications {
            create<MavenPublication>("debug") {
                // Applies the component for the release build variant.
                from(components["debug"])
                artifactId = "wavelt-android-debug"
                artifact(sourcesJar)
                artifact(dokkaJar)
            }
            create<MavenPublication>("release") {
                // Applies the component for the release build variant.
                from(components["release"])
                artifactId = "wavelt-android"
            }
        }
    }
}

通过这些修改,./gradlew publish 任务将生成所有文件,尽管其中一个文件有 .aar,但当它们一起复制到另一个项目时,它们的工作方式与 .jar 相同。