在Android AAR中将jar文件包含在Java源代码中 [英] Include jar file with Java sources in Android aar

查看:431
本文介绍了在Android AAR中将jar文件包含在Java源代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gradle任务来创建一个包含Java 源文件的jar文件,该文件将包含在Android aar库包中.这些文件将作为JNI的Javadoc到aar软件包中捆绑的C ++库中.

I have a gradle task to create a jar file containing Java source files to be included in an Android aar library package. These files will serve as Javadoc for JNI to a C++ library also bundled in the aar package.

我无法弄清楚如何包括 jar文件,而不能编译其中的文件.似乎jar文件中的Java文件是 compiled ,这对我没有帮助-我只想 include 以便使用该aar包的开发人员可以使用它们

I cannot possibly figure out how to include the jar file and not compile the files within. 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文件具有所有源文件,并且位于libs目录中的输出aar中,但是没有内容.

The created jar file has all the sources and is in the output aar inside its libs directory, but it has no contents.

如何将Java源代码添加到我的aar?

How can I add the Java sources to my aar?

创造瓶子

Jar的创建过程如下,并保存在模块的build/libs文件夹中.

The Jar is created as follows and ends up in the build/libs folder of my module.

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')
}

输出jar包含:

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

该jar位于libs目录内的aar内部,但现在为空.

The jar exists inside the aar inside the libs directory, but now it is empty.

推荐答案

我唯一能想到的就是像这样构建后将您的源代码添加到.aar文件中

Only thing I could come up with is to add your sources to the .aar file after it's built like so

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
}

然后运行

./gradlew myModule:addMySourcesToAar

我没有像您一样向依赖项添加任何内容

I didn't add anything to dependencies like you did

这篇关于在Android AAR中将jar文件包含在Java源代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆