1

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?

enter image description here

NickUnuchek
  • 11,794
  • 12
  • 98
  • 138

2 Answers2

1

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.

Sam YC
  • 10,725
  • 19
  • 102
  • 158
1

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.

Sergio
  • 658
  • 1
  • 9
  • 22