Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to exclude("META-INF/*kotlin_module")?

I am dealing with a build problem :

A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
   > 2 files found with path 'META-INF/annotation-experimental_release.kotlin_module'

and It can be fixed by adding those lines to build.gradle on app directory, as suggested on other tickets.

packagingOptions {
    exclude("META-INF/*kotlin_module")
}

Is it safe to exclude META-INF/? is it can cause any problems? I am not using kotlin on my code, that's come from other sdks .

like image 414
DevD Avatar asked Oct 24 '25 02:10

DevD


2 Answers

This can be possibly dangerous if you publish a library and exclude META-INF/*kotlin_module. In my case it caused unresolved reference errors when trying to invoke some global Kotlin functions from this library. My advice is avoid excluding this.

like image 153
Miloš Černilovský Avatar answered Oct 25 '25 15:10

Miloš Černilovský


There's a bit of confusing information out there...

TLDR If the jar files are only for yourself and you don't use reflection, you can exclude the files.


As described here: https://blog.jetbrains.com/kotlin/2015/09/kotlin-m13-is-out/

New layout of .class files for top-level declarations

A few months ago we announced this change and now it’s done:

  • By default, each Kotlin source file (e.g. myFile.kt) produces a class file with the same name, capitalized and suffixed with “Kt”: MyFileKt;
  • Top-level functions and properties defined in that file are accessible in Java through this class name (instead of the problematic FooPackage);
  • Consequently, two files in the same package can not have the same name (or the class files would clash);
  • You can specify a @file:JvmName("CustomName") annotation on a source file to change the name of the class;
  • Many files can share the same JVM name if they are additionally marked with @file:JvmMultifileClass annotation.

To make this change work

We had to introduce a new resource file that is required to compile Kotlin code against Kotlin binaries. Its name is META-INF/<module_name>.kotlin_module. Make sure these .kotlin_module files are not stripped by your packaging process.

Also, make sure that module names do not clash in your project


As also commented on further here: https://youtrack.jetbrains.com/issue/KT-9770 (these files are used only for reflection and during the build process)

  • These files are used only if you're using kotlin-reflect to introspect your program at runtime (note that simple ::class literals and callable references do not use reflection by themselves, unless you explicitly depend on kotlin-reflect and use any API from kotlin.reflect.*)

More clarifying reasons for these files:

For some Kotlin classes, we cannot store the metadata in the annotation because the class does not physically exist (Int, List, etc), so we store this information in separate resources. This is what .kotlin_builtins files are for. For top level declarations, we need to know what files does the package consist of, this is what .kotlin_module files are for.

like image 36
Nathan Avatar answered Oct 25 '25 17:10

Nathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!