Search code examples
javaandroidgradlebintrayjcenter

Build sources.jar with .class files rather than .java files in android gradle


I am trying to upload my binaries to an OSS bintray repository which mandates the upload of sources.jar. I am okay to upload a sources.jar if it has the compiled .class files. Right now, the script I have, generates sources with java files.

task sourcesJar(type: Jar) {

    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'

}

artifacts {
    archives sourcesJar
}

Is there a way to generate sources.jar only with .class files?

Note : I cannot include apply plugin: 'java' as this has android dependencies.


Solution

  • Though I havent found a way to upload the .class files as part of sources.jar, I found a work around to not expose the source code in jcenter/bintray.

    I believe this may be helpful for someone who do not want to upload their sources to jcenter.

    task sourcesJar(type: Jar) {
    //Comment out the below line
    //from android.sourceSets.main.java.srcDirs 
    classifier = 'sources'
    
    }
    
    artifacts {
      archives sourcesJar
    }
    

    Commenting out the line above,will generate a empty sources.jar which will be approved by jcenter as well because they just check for the maven project structure and not the contents inside. Doing so, will not expose your source code.

    Thought this might be helpful for someone.