Kotlin, Proguard and Lambda

I have a neat function that does something in appearance:

fun<T : View> Activity.withView(nr : Int, fn : T.()->Unit) { (findViewById(nr) as T?)?.fn() } 

Now, when I use this function in my activity:

  withView<Spinner>(R.id.spinner_toolbar) { adapter = AdapterIndeksuDlaSpinnera( this@NewMainActivity , PlaylistIndex) 

... everything is fine until I use ProGuard. I see that AdapterIndeksuDlaSpinnera gets crippled, as expected, but the application fails when it runs with "Cannot load AdapterIndeksuDlaSpinnera class" (while it must complain about the name of the adapted adapter).

I managed to create a temporary workaround by disabling the manipulation of all adapters that can be used inside my withView

 -keep class pl.qus.xenoamp.adapter.** { *; } 

but I don’t think this is a good solution (and I don’t know what other classes can fail this way!). So can anyone explain what the problem is, and which ProGuard line should be added to potentially fix similar occurrences of other classes used inside withView ?

+5
source share
1 answer

It is not simple. In short, Proguard does not know about Kotlin. It uses simple code analysis to detect things like Class.forName() , and works around them, but it may fail for something more complex. You need to look at the generated .class files from the build subdirectories (can you post the appropriate messages?) To find out what is actually happening.

Now you can do two things:

  • Ask Kotlin developers to add proper obfuscation / optimization support to the Kotlin compiler: this is really the right way to do something, as every non-java compiler testifies.
  • Eliminate your own sources from obfuscation (most activities and types will not be well confused anyway).
+1
source

Source: https://habr.com/ru/post/1243392/


All Articles