1

I've added the kotlin-reflect library to my Android project, and now Proguard is generating hundreds of lines of notes, listing all classes that have a field named INSTANCE, which of course includes every single Kotlin object.

Here's just a short part of the Proguard output:

Note: kotlin.reflect.jvm.internal.KClassImpl$Data$objectInstance$2 accesses a declared field 'INSTANCE' dynamically
      Maybe this is program field 'co.zsmb.example.proguardkttest.MySingleton { co.zsmb.example.proguardkttest.MySingleton INSTANCE; }'
      Maybe this is program field 'kotlin.UNINITIALIZED_VALUE { kotlin.UNINITIALIZED_VALUE INSTANCE; }'
      Maybe this is program field 'kotlin.Unit { kotlin.Unit INSTANCE; }'
      Maybe this is program field 'kotlin._Assertions { kotlin._Assertions INSTANCE; }'
      Maybe this is program field 'kotlin.collections.EmptyIterator { kotlin.collections.EmptyIterator INSTANCE; }'
      Maybe this is program field 'kotlin.collections.EmptyList { kotlin.collections.EmptyList INSTANCE; }'
      Maybe this is program field 'kotlin.collections.EmptyMap { kotlin.collections.EmptyMap INSTANCE; }'
      Maybe this is program field 'kotlin.collections.EmptySet { kotlin.collections.EmptySet INSTANCE; }'
      Maybe this is program field 'kotlin.comparisons.NaturalOrderComparator { kotlin.comparisons.NaturalOrderComparator INSTANCE; }'

As you can see, it includes both my own MySingleton class, as well as many classes from the Kotlin standard library, and it finds these fields in any other libraries too, if they're present.

zsmb13
  • 85,752
  • 11
  • 221
  • 226

1 Answers1

1

The first line of the error tells us what's wrong here, it found that whatever kotlin.reflect.jvm.internal.KClassImpl$Data$objectInstance$2 is, it's accessing a field named INSTANCE via reflection. Proguard can't tell which class it is that this code accesses the INSTANCE field of, so it warns us about every single class that has this field, just in case.

If we look at the source of KClassImpl, we'll find the culprit quickly: it contains a property called objectInstance, which calls the Class#getDeclaredField method in some cases, passing in a constant with the value "INSTANCE" as the parameter. This is what Proguard is detecting.

So, unless you have a singleton class, and you're accessing its instance via its KClass, like so:

val instance = MySingleton::class.objectInstance

You can ignore the Proguard note with the following rule added to your proguard-rules.pro file:

-dontnote kotlin.reflect.jvm.internal.KClassImpl$Data$objectInstance**

You can also end it precisely with objectInstance$2, but an implementation change in the library could change that positional number in the future. This way, you're ignoring all notes that would appear about the objectInstance property and its implementation.

zsmb13
  • 85,752
  • 11
  • 221
  • 226