英文:
How to generate .jar of android library with *class files?
问题
目前我的Gradle任务如下:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
...
}
task sourcesJar(type: Jar) {
getArchiveClassifier().set('sources')
from android.sourceSets.main.java.sourceFiles
}
artifacts {
archives sourcesJar
}
但是生成的Jar构件中包含了 *.java
和 *.kt
文件,那么如何生成包含 *.class
文件的Jar构件呢?
英文:
Currently my gradle task looks like:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
...
}
task sourcesJar(type: Jar) {
getArchiveClassifier().set('sources')
from android.sourceSets.main.java.sourceFiles
}
artifacts {
archives sourcesJar
}
But in result jar artifact has *.java
and *.kt
files, but how to generate jar artifact with *.class
files?
答案1
得分: 1
根据您的截图,您正在打开xxx-sources.jar
文件。当然,它里面只包含Java源文件。这是正确的结果,因为您按照下面的配置进行了设置:
task sourcesJar(type: Jar) {
getArchiveClassifier().set('sources')
from android.sourceSets.main.java.sourceFiles
}
您的类文件通常会分开保存在另一个jar文件中。您没有指定您执行了哪些任务。所以让我们假设您运行了默认的Android构建任务,它会生成aar
文件,其中应该包含您的模块类文件。您可以解压缩sapi-1.6.aar
文件来检查。
您还可以参考这个文档。
更新:
您没有指明为什么您需要Jar文件而不是AAR文件,让我们假设您想在其他项目中使用Jar文件,那么您需要使用其他Gradle插件进行构建(不能使用android
Gradle插件),可以是java
或base
Gradle插件。一些答案指出,您可以在aar文件中找到jar文件,您可以参考这个链接。
英文:
Based on your screenshot, you are opening xxx-sources.jar
file. Of course it contains only Java source file inside. This is the correct result as you configure as below:
task sourcesJar(type: Jar) {
getArchiveClassifier().set('sources')
from android.sourceSets.main.java.sourceFiles
}
You classes file normally are separated in another jar file. You didn't specify what are the tasks you executed. So let's assume you run the default Android build task, it will generate the aar
file, it should contain your module classes file. You can unzip sapi-1.6.aar
to check.
You can refer to this doc as well.
Updated:
You didn't specify why you need the Jar file instead of AAR file, let's assume you want to use Jar file in other project, then you need to use other gradle plugin to build (can't use android
gradle plugin), it could be java
or base
gradle plugin. Some answer saying, you can find the jar file inside aar file, you can check this.
答案2
得分: 1
从命令行,您只需键入:
jar uf0 路径至您的_jar_文件\yourJarFile.jar 路径至您的_classes\*.class
当然,如果您不想添加目录中的所有类,您也可以使用类名替代通配符(*)。
jar --help
将提供更多选项。
英文:
From command line, you just type:
jar uf0 path_to_your_jar_file\yourJarFile.jar path_to_your_classes\*.class
and of course, you can use the class name instead of a wildcard (*) if you don't want to add all classes from that directory.
jar --help
will give you more options.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论