11

I recently added two Android libraries through JitPack and I have the following error:

Duplicate files copied in APK META-INF/library_release.kotlin_module

I've cleared the cached and I've tried excluding the module using

exclude group: 'org.jetbrains'

and

exclude group: 'org.jetbrains.kotlin'

but neither seems to resolve the issue. Is there any way to stop the kotlin stdlib from being added through JitPack? Oddly, other libraries like DbFlow don't have this issue, though I don't see anything special about their setup (other than that it isn't through JitPack)

Allan W
  • 2,791
  • 4
  • 23
  • 41

3 Answers3

19

As suggested in the post Kotlin M13 is out! by jetbrains:

Make sure these .kotlin_module files are not stripped by your packaging process.

So, we can't use exclude option to exclude this resource file from being generated.

As descripted in Kotlin M13 is out!, we should:

in Maven we use groupId and artifactId for module names, but you can say

<configuration>
    <moduleName>com.example.mymodule</moduleName>
</configuration>

in Gradle it’s project name + build task name, to customize:

compileKotlin {
    kotlinOptions.moduleName = "com.example.mymodule"    
}

This is my configuration for Android library project:

ext {
    GROUP_ID = 'custom.group.id'
    ARTIFACT_ID = 'artifactid'
}

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    compileOptions {
        kotlinOptions.freeCompilerArgs += ['-module-name', "$GROUP_ID.$ARTIFACT_ID"]
    }

    defaultConfig {
        ...
    }
    buildTypes {
        ...
    }
}

Resource file named META-INF/custom.group.id.artifactId.kotlin_module will be generated instead of META-INF/library_release.kotlin_module.No more duplicate files will be found.

You can read this post and this post for more information.

daemon.yang
  • 311
  • 3
  • 6
  • Yes, adding compileOptions { kotlinOptions.freeCompilerArgs += ['-module-name', "$GROUP_ID.$ARTIFACT_ID"] } fixes problem And I think it's better than just exclude – Penzzz Jun 30 '21 at 13:44
  • I tested both solutions (exclude and this one) and both of them work and have no apparent side effects. Can somebody please provide any additional arguments - or example - why one is better over another? – ror Oct 02 '21 at 05:57
  • kotlin module file contains some kotlin meta data such as top level function, top level property, if you using exclude solution, you will get a "Unresolved reference" build error. Not sure whether kotlin module file contains other info @ror – daemon.yang Mar 22 '22 at 08:07
18

You should add this to the build.gradle file of your app inside the android tag

packagingOptions {
    exclude 'META-INF/library_release.kotlin_module'
}
MatPag
  • 41,742
  • 14
  • 105
  • 114
  • 2
    Do you know if the file itself has any significance in the app? Or is it just there as info? – Allan W Jun 12 '17 at 22:31
  • I never seen a problem excluding those META-INF files in Android. Btw i don't know Kotlin enough to be sure that this is useless at 100%. As i suggested in the other comment, you should try with exclude first, if it is working continue to use it (the app will be lighter by some KBs XD), if not switch to pickFirst ;) – MatPag Jun 12 '17 at 22:36
  • Doesn't it mean that when we switch to different buildType or productFlavor the same error appears again because it generates files with new names? – Antimonit Jun 28 '19 at 06:12
  • leads to Kotlin classes not being recognized – Rainmaker Feb 05 '20 at 17:12
  • kotlin module file contains some kotlin meta data such as top level function, top level property, if you using exclude solution, you will get a "Unresolved reference" build error. Not sure whether kotlin module file contains other info @MatPag – daemon.yang Mar 22 '22 at 08:09
10

After looking at other conflicts, it seems like the resolution is

packagingOptions {
    pickFirst 'META-INF/library_release.kotlin_module'
}

under android in the app gradle.

This allows the apk to build

Allan W
  • 2,791
  • 4
  • 23
  • 41