禁用gradle android库kotlin项目中的META-INF/*生成 [英] Disable META-INF/* generation in gradle android library kotlin project
问题描述
美好的一天.我已经编写了一个kotlin android库并将其上传到Bintray.但是当我尝试在某些项目中使用它(通过gradle编译)时,它无法构建并出现以下错误:
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档案)包含此文件夹以及该文件.我检查了其他体面的Bintray android库,它们似乎未包含任何 META-INF
文件.但是,包含该文件的程序(在大多数情况下,它们包含许可证文件)会产生相同的 DuplicateFileException
,解决该问题的方法是在使用项目的gradle文件中明确排除它们.
I've looked up both of these .jar files and they both contained META-INF
folder with library_release.kotlin_module
file. But more importantly generated .aar (android archive published in bintray) contained this folder as well as this file. I've checked other decent bintray android libraries and they don't seem to include any META-INF
files. However those that do include it (in most cases they contain license files) produce the same DuplicateFileException
and the way to resolve it is to explicitly exclude them in use-project's gradle file.
此 library_release.kotlin_module
文件有什么用途,如何在发布过程中禁用其生成?因为我不想从正在使用此库的每个项目中明确排除,而且我也不想让其他开发人员这样做.
What's the use of this library_release.kotlin_module
file and how can I disable its generation during publishing? Because I don't want to explicitly exclude from every project which is using this library and I don't want to ask other developers to do so.
这是图书馆的仓库: https://github.com/arslancharyev31/Anko-ExpandableTextView/一个>这是Bintray回购协议: https://bintray.com/arslancharyev31/android/Anko-ExpandableTextView
Here's the library's repo: https://github.com/arslancharyev31/Anko-ExpandableTextView/ And it's bintray repo: https://bintray.com/arslancharyev31/android/Anko-ExpandableTextView
推荐答案
.kotlin_module
文件的原始目的是存储映射到已编译类文件的包部分.编译器使用它们来解析项目附带的库中的顶级函数调用.另外,Kotlin反射使用 .kotlin_module
文件在运行时构造顶级成员元数据.查看更多: 改善Java互操作性:顶级功能和属性
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
来消除重复项,如
Instead, you can get rid of the duplicates by using packagingOptions
in your app build.gradle
, as said here:
android {
// ...
packagingOptions {
exclude 'META-INF/library_release.kotlin_module'
}
}
注意:从应用程序APK中排除这些文件会干扰运行时的反射,因此也不是最佳解决方案.
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"]
选择生成输出的任务,然后将其打包到AAR中.可能不是 compileReleaseKotlin
,并且还请注意,这样做可能会影响此变体的测试编译.
Choose the task that produces the output that is then packed into the AAR.
It might not be compileReleaseKotlin
, and also note that doing this might affect the tests compilation for this variant.
或者,更可靠的是,只需选择唯一的Gradle模块名称,因为Kotlin模块是以Gradle项目命名的.
Or, what's more reliable, just choose unique Gradle module names, because the Kotlin module is named after the Gradle project.
这篇关于禁用gradle android库kotlin项目中的META-INF/*生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!