What are .kotlin_builtins files and can I omit them from my uberjars?

后端 未结 2 1403
野趣味
野趣味 2020-12-31 10:17

I\'m working on integrating proguard to my gradle build for an application written in Kotlin. I\'m finding that proguard is stripping out the Kotlin standard library (as it

相关标签:
2条回答
  • 2020-12-31 11:04

    These files contain data for declarations of standard ("built-in") Kotlin classes which are not compiled to .class files, but rather are mapped to the existing types on the platform (in this case, JVM). For example, kotlin/kotlin.kotlin_builtins contains the information for non-physical classes in package kotlin: Int, String, Enum, Annotation, Collection, etc.

    There are two main scenarios when these files are used:

    1. The compiler looks them up from kotlin-stdlib on the classpath to determine which built-in declarations are available.

    2. The reflection library (kotlin-reflect) loads these files as resources to provide reflection capabilities for built-in declarations. For example, String::class.members returns all members of the class kotlin.String exactly in the same way as the Kotlin compiler sees those members (despite the fact that there's no kotlin/String.class file and it's erased to java.lang.String in bytecode).

    The first point is clearly not applicable in your case. And if you don't use reflection on built-in classes, I think it's safe to exclude .kotlin_builtins files completely from the resulting jar.

    0 讨论(0)
  • 2020-12-31 11:04

    You can optimize/omit these from yours JARs/APKs:

    packagingOptions {
      exclude "/META-INF/*.kotlin_module"
      exclude "**/kotlin/**"
    }
    

    Even better:

    packagingOptions {
      exclude "/META-INF/*.kotlin_module"
      exclude "**/kotlin/**"
      exclude "**/*.txt"
      exclude "**/*.xml"
      exclude "**/*.properties"
    }
    

    Source: https://github.com/jaredsburrows/android-gif-example/blob/master/build.gradle.kts#L127

    0 讨论(0)
提交回复
热议问题