简体   繁体   English

在 Android aar 中包含带有 Java 源代码的 jar 文件

[英]Include jar file with Java sources in Android aar

I have a gradle task to create a jar file containing Java source files to be included in an Android aar library package.我有一个 gradle 任务来创建一个 jar 文件,其中包含要包含在 Android aar 库包中的 Java源文件 These files will serve as Javadoc for JNI to a C++ library also bundled in the aar package.这些文件将作为 JNI 的 Javadoc 到同样捆绑在 aar 包中的 C++ 库。

I cannot possibly figure out how to include the jar file and not compile the files within.我无法弄清楚如何包含jar 文件而不是编译其中的文件。 it seems that the Java files in the jar file are compiled , which does not help me - I just want to include them so that they are available to developers using that aar package. jar 文件中的 Java 文件似乎已编译,这对我没有帮助 - 我只想包含它们,以便使用该 aar 包的开发人员可以使用它们。

The created jar file has all the sources and is in the output aar inside its libs directory, but it has no contents.创建的 jar 文件包含所有源代码,并且在其libs目录中的输出 aar 中,但它没有内容。

How can I add the Java sources to my aar?如何将 Java 源代码添加到我的 aar 中?

Jar creation Jar 创建

The Jar is created as follows and ends up in the build/libs folder of my module. Jar 是按如下方式创建的,并最终位于我的模块的build/libs文件夹中。

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

artifacts {
    archives generateMySources
}

preBuild.dependsOn(":myModule:generateMySources")

dependencies {
    // The jar is included but it is empty inside the aar.
    compile files('build/libs/myModule-sources.jar')
}

The output jar contains:输出 jar 包含:

.
├── com
│   └── my
│       └── app
│           └── jni
│               ├── File1.java
│               ├── File2.java
│               ├── File3.java
│               └── File4.java
└── META-INF
    └── MANIFEST.MF // Contains "Manifest-Version: 1.0" only.

The jar exists inside the aar inside the libs directory, but now it is empty. jar 存在于libs目录内的 aar 中,但现在它是空的。

Only thing I could come up with is to add your sources to the .aar file after it's built like so我唯一能想到的就是在像这样构建后将您的源添加到 .aar 文件中

task generateMySources(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}
task addMySourcesToAar(type: Jar) {
    archiveName "myModuleWithSources.aar"
    destinationDir file("build")
    from zipTree("build/outputs/aar/myModule-release.aar")
    from fileTree("build").include("libs/myModule-sources.jar")
}
afterEvaluate { project ->
    project.tasks.preBuild.dependsOn generateMySources
    project.addMySourcesToAar.dependsOn build
}
artifacts {
    archives addMySourcesToAar.archivePath
}

and run并运行

./gradlew myModule:addMySourcesToAar

I didn't add anything to dependencies like you did我没有像你那样向依赖项添加任何东西

For thus who are using Gradle Kotlin DSL:对于使用 Gradle Kotlin DSL 的人来说:

tasks.register<Jar>(name = "sourceJar") {
    from(android.sourceSets["main"].java.srcDirs)
    classifier = "sources"
}

publishing {
    publications {
        create<MavenPublication>(name = "Maven") {
            run {
                groupId = "groupId"
                artifactId = "module-name"
                version = "1.0"
                artifact("$buildDir/outputs/aar/module-name-release.aar")
                artifact(tasks["sourceJar"])
            }
        }
    }
}

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

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