简体   繁体   中英

Gradle generate sources.jar for only public interfaces

I am working on a closed-source Android library (published as an AAR), and want to include some javadocs for consumers, which requires a sources.jar.

I know I could cherry-pick each file using an includes property or maybe even a whole package/folder.

task('androidSourcesJar', type: Jar) {
    classifier = 'sources'
    baseName = artifactBaseName
    from android.sourceSets.main.java.srcDirs
    include ('MyInterface1.kt', 'MyInterface2.kt', 'MyInterface3.kt')
}

Instead, is there a way to include only public classes, interfaces, methods, etc? This seems like a problem that would've come up before.

You could try adding something like this, instead of your include :

from 'src/main/java'
eachFile { currentFile ->
    String contents = new File(currentFile.getSourcePath()).text
    if(!contents.contains("public class")) {
        currentFile.exclude()
    }
}

I'm not entirely sure if that works, but it should set you on the right path to where you want to go.

Since Gradle does not actually do any code analysis, you can't just simply say "only include files that have classes that are public". Instead, you have to either write a custom plugin that will only include public classes, or do something like what I provided. It includes everything from the source directory, but runs a little bit of code on each file. First, it gets the contents of the file, then it checks if that file contains public class . If not, the file does't have a public class, and should be excluded.

Hope this helps! Feel free to ask any more questions if you have any.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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