Good day. I've written a kotlin android library and uploaded it on bintray. But when I try to use it (via gradle compile) in some project, it fails to build with following errors:

> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/library_release.kotlin_module
File1: C:\Users\Charlie\.android\build-cache\2939fbc6b0336396c9c4912d615880645b2c729d\output\jars\classes.jar
File2: C:\Users\Charlie\OneDrive\Dev\Projects\AndroidStudio\MetuCardLib\demo\build\intermediates\bundles\default\classes.jar

这两个我都查过了.jar文件,它们都包含META-INF个文件夹和library_release.kotlin_module个文件.但更重要的是,它产生了.aar(在bintray中发布的android archive)包含此文件夹和此文件.我查看了其他一些不错的android库,它们似乎没有包含任何META-INF个文件.然而,那些包含它的(在大多数情况下,它们包含许可证文件)会产生相同的DuplicateFileException,解决方法是在使用project的gradle文件中明确排除它们.

这个library_release.kotlin_module文件有什么用途?我如何在发布过程中禁用它的生成?因为我不想明确地从每个使用这个库的项目中排除,我也不想要求其他开发人员这样做.

Here's the library's repo: https://github.com/arslancharyev31/Anko-ExpandableTextView/ And it's bintray repo: https://bintray.com/arslancharyev31/android/Anko-ExpandableTextView

推荐答案

The original purpose of .kotlin_module files is to store the package parts mapping to the compiled class files. The compiler uses them to resolve top-level function calls in a library attached to the project. Also, Kotlin reflection uses the .kotlin_module files to construct the top level members metadata at runtime. See more: Improving Java Interop: Top-Level Functions and Properties

Given that, you don't want to disable their generation in library projects, because it might break compilation against the libraries.

相反,你可以在你的应用程序build.gradle中使用packagingOptions来消除重复项,如here所述:

android {
    // ...

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

Note: excluding these files from an application APK interferes with reflection at runtime and therefore is is not the best solution either.


Another option is to use unique module names in your libraries:

compileReleaseKotlin.kotlinOptions.freeCompilerArgs += ["-module-name", "my.library.id"]

Select 产生输出的任务,然后将输出打包到AAR中.

或者,更可靠的是,只需 Select 惟一的Gradle模块名称,因为Kotlin模块是根据Gradle项目命名的.

Kotlin相关问答推荐

DataSnapshot.getValue()未记录的奇怪行为

在Kotlin中求n个ClosedRange实例相交的最常用方法是什么?

Kotlin中一个接口的实现问题

kotlin 模式匹配如何像 scala 一样工作

使用 StateFlow 时如何移除监听器?

为什么记得不将 StateFlow 转换为特定类型?

在 Kotlin 中 import 如何找到文件路径?

Eclipse:无法安装 Kotlin 插件

为什么 Kotlin 在 sumOf 函数 lambda 中默认不将数字视为Int?

Kotlin 条件格式字符串

如何使用 Android CameraX 自动对焦

Kotlin 使用迭代索引过滤 lambda 数组

具有多个不同类型来源的 LiveData

Kotlin RxJava 可空的错误

Kotlin - 当表达式返回函数类型

在 Spring Framework 5.1 中注册具有相同名称的测试 bean

什么是开放式property?为什么我不能将其设置器设为private私有?

Kotlin:子构造函数如何使用其父构造函数的辅助构造函数?

当被Spring代理类访问时,Kotlin实例变量为null

如何使用mockk库模拟android上下文