简体   繁体   English

Gradle仅为公共接口生成sources.jar

[英]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. 我正在开发一个闭源Android库(作为AAR发布),并希望为消费者提供一些javadoc,这需要一个sources.jar。

I know I could cherry-pick each file using an includes property or maybe even a whole package/folder. 我知道我可以使用includes 属性或甚至整个包/文件夹来挑选每个文件。

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? 相反,有没有办法只包括public类,接口,方法等? This seems like a problem that would've come up before. 这似乎是一个以前会出现的问题。

You could try adding something like this, instead of your include : 你可以尝试添加这样的东西,而不是你的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". 由于Gradle实际上没有进行任何代码分析,因此您不能简单地说“只包含具有公共类的文件”。 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 . 首先,它获取文件的内容,然后检查该文件是否包含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. 如果您有任何问题,请随时再提问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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