MohamedRejeb / Ksoup

Ksoup is a lightweight Kotlin Multiplatform library for parsing HTML, extracting HTML tags, attributes, and text, and encoding and decoding HTML entities.
Apache License 2.0
384 stars 10 forks source link

Missing Prougard rule #44

Open eyedol opened 7 months ago

eyedol commented 7 months ago

Usage of the library causes app to crash with Android release builds because R8 removes unused classes or methods from the Ksoup library. Specifically, Builder#onOpenTag is stripped out, causing a crash when only Builder#onCloseTag is called in the app.

Here's a simplified example of how the library is used in the app:

val handler = KsoupHtmlHandler
  .Builder()
  .onOpenTag { name, attributes, _ -> Logger.d { "tag opening name: $name" } }
  .onCloseTag { name, _ -> Logger.d { "tag closing name: $name" } }
  .build()

val parser = KsoupHtmlParser(handler)
try {
  val html = "<p> Welcome to my home page. </p>"
  parser.write(html)
} finally {
  parser.end()
}  

Adding the following ProGuard rule resolves the issue:

-keep class com.mohamedrejeb.ksoup.html.parser.KsoupHtmlHandler.Builder**, * { *; }

Ideally, this rule could be shipped with the library to avoid manual configuration by consumers?

I'm unsure of the best way forward, but I believe this issue warrants further investigation.

P.S. Thanks for the library, saved me a ton of time parsing html tags in a KMM project

MohamedRejeb commented 7 months ago

Hi, You are right, it's better to have the necessary ProGuard rules shipped with the library. It will be available in the next release.

eyedol commented 7 months ago

Thanks for confirming and considering it for inclusion in the next release.

vanniktech commented 7 months ago

Are we using reflection? If not, this should not be stripped and if so is a bug with R8. Adding that proguard rule does not seem right, since onOpenTag should be stripped out if no one is using it!