我已经为Android开发了一个使用Kotlin编写的Text2Summar图书馆。我正在使用JitPack分发此库,构建过程进行得非常顺利。

现在,在启用Kotlin的Android项目中,我能够导入库中可用的类。在仅具有Java(未配置Kotlin)的项目中,情况并非如此。在这里,Android Studio只是抱怨无法解析符号Text2Summary

最佳答案

我认为您忘了添加@JvmStatic注释以使您的方法可从Java代码调用。没有它,您必须像Java中的MyObject.Companion.method1()一样调用它。

这是您应该在companion object {}中添加到公共(public)方法中的内容


class Text2Summary() {

    companion object {

        // Summarizes the given text.
        @JvmStatic
        fun summarize( text : String , compressionRate : Float ): String {
            val sentences = Tokenizer.paragraphToSentence( Tokenizer.removeLineBreaks( text ) )
            val tfidfSummarizer = TFIDFSummarizer()
            val p1 = tfidfSummarizer.compute( text , compressionRate )
            return buildString( sentences , p1 )
        }

        // Summarizes the given text. Note, this method should be used whe you're dealing with long texts.
        // It performs the summarization on the background thread. Once the process is complete the summary is
        // passed to the SummaryCallback.onSummaryProduced callback.
        @JvmStatic
        fun summarizeAsync( text : String , compressionRate : Float , callback : SummaryCallback ) {
            SummaryTask( text , compressionRate , callback ).execute()
        }
    }
}

08-18 17:40
查看更多