简体   繁体   English

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

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

I have a project with the standard java library and this creates several .jar files, one being a docset and another the source set.我有一个带有标准 java 库的项目,这会创建几个.jar文件,一个是文档集,另一个是源集。 Now I have successfully created an android-library using themaven-publish plugin, but when I add the .aar files to another project, they don't contain neither source nor comments as the .jar version did, so I don't get help inside the IDE or look into the implementation of methods.现在我已经使用maven-publish插件成功创建了一个 android-library,但是当我将.aar文件添加到另一个项目时,它们既不包含源代码也不包含注释,因为.jar版本没有,所以我不明白IDE 里面的帮助或查看方法的实现。

What do I need to add to my build.gradle.kts to include docstrings and sources in the debug version of the .aar I'm publishing locally as a file?我需要添加什么到我的build.gradle.kts以在我作为文件在本地发布的.aar的调试版本中包含文档字符串和源? The linked gradle documentation from that Android developer page does not mention anything about docstrings or sources at all.来自 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"
            }
        }
    }
}

While I've read about other devs being able to cram the javadocs into the .aar file, this is not explicitly necessary at all to gain documentation and source code inspection from inside IDEs like Android Studio.虽然我读过其他开发人员能够将 javadocs 塞进.aar文件中,但这对于从像 Android Studio 这样的 IDE 内部获取文档和源代码检查来说完全没有必要。 In fact, looking at the way a typical java library works, it creates files like:事实上,看看典型的 java 库的工作方式,它会创建如下文件:

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

The only difference with an Android library would be having these files:与 Android 库的唯一区别是拥有这些文件:

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

Which means that the sources and javadoc jars can still be copied along the .aar and the IDE will load them.这意味着源代码和 javadoc jars 仍然可以沿.aar复制,并且 IDE 将加载它们。 Having said that, the publish example code only creates the .aar file, looking at other questions like this one I was able to modify the script to create the .aar plus the two other .jar packages:话虽如此,发布示例代码仅创建.aar文件,查看像这样的其他问题,我能够修改脚本以创建.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"
            }
        }
    }
}

With these modifications the ./gradlew publish task will generate all files, and despite one of them having .aar they work the same as .jar when copied together into another project.通过这些修改,. ./gradlew publish任务将生成所有文件,尽管其中一个文件具有.aar ,但当它们一起复制到另一个项目中时,它们的工作方式与.jar相同。

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

相关问题 如何使用 Maven-Publish Android Gradle Plugin 生成的组件在 Kotlin DSL (build.gradle.kts) 中? - How do you use Maven-Publish Android Gradle Plugin's generated components in Kotlin DSL (build.gradle.kts)? 如何构建和 maven 发布 flutter aar? - How to build and maven-publish a flutter aar? 使用 maven-publish 发布 Android apk 和 aar with build variant (Android Gradle plugin 4.0) - Use maven-publish to publish Android apk and aar with build variant (Android Gradle plugin 4.0) 如何使用maven-publish gradle插件附加AAR文件的源? - How to attach sources of an AAR file with maven-publish gradle plugin? 用 Kotlin 中的另一个库 aar 打包库 (build.gradle.kts) - Packaging Library with another library aar in Kotlin(build.gradle.kts) android build.gradle.kts 风格的实现 - android build.gradle.kts flavoured implementation Android Studio:如何在 Kotlin 多平台共享模块 build.gradle.kts 中访问项目级 compileSdkVersion 和 targetSdkVersion? - Android Studio: How to access project level compileSdkVersion and targetSdkVersion in Kotlin Multiplatform shared module build.gradle.kts? androidTest Groovy 模拟 build.gradle.kts - androidTest Groovy analog in build.gradle.kts 未解决的参考:build.gradle.kts 中的 grgit - Unresolved reference: grgit in build.gradle.kts Android库:maven vs maven-publish gradle项目 - Android Libraries: maven vs maven-publish gradle project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM