Android Gradle 插件 0.7.0:“打包 APK 期间重复文件”

Posted

技术标签:

【中文标题】Android Gradle 插件 0.7.0:“打包 APK 期间重复文件”【英文标题】:Android Gradle plugin 0.7.0: "duplicate files during packaging of APK" 【发布时间】:2014-01-07 13:04:55 【问题描述】:

使用带有以下build.gradleandroid Gradle插件0.7.0:

buildscript 
    repositories 
        mavenCentral()
    

    dependencies 
        classpath 'com.android.tools.build:gradle:0.7.0'
    


apply plugin: 'android'

repositories 
    maven  url "https://android-rome-feed-reader.googlecode.com/svn/maven2/releases" 
    maven  url "http://dl.bintray.com/populov/maven" 
    mavenCentral()


android 
    compileSdkVersion 19
    buildToolsVersion '18.1.1'

    defaultConfig 
        minSdkVersion 9
        targetSdkVersion 19
    
    buildTypes 
        release 
            runProguard true
            proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
        
    
    productFlavors 
        defaultFlavor 
            proguardFile 'proguard-rules.txt'
        
    
    sourceSets 
        instrumentTest.setRoot('src/instrumentTest')
    


configurations 
    apt


ext.androidAnnotationsVersion = '2.7.1';

dependencies 
    compile 'com.android.support:support-v4:18.0.0'

    compile 'com.viewpagerindicator:library:2.4.1@aar'

    compile 'com.google.code.android-rome-feed-reader:android-rome-feed-reader:1.0.0-r2'
    compile 'org.jdom:jdom:1.1.1-android-fork'

    apt "com.googlecode.androidannotations:androidannotations:$androidAnnotationsVersion"
    compile "com.googlecode.androidannotations:androidannotations-api:$androidAnnotationsVersion"

    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.j256.ormlite:ormlite-android:4.47'
    compile 'com.j256.ormlite:ormlite-core:4.47'
    compile 'org.springframework.android:spring-android-core:1.0.1.RELEASE'
    compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
    compile 'com.prolificinteractive:actionbarsherlock:4.3.1@aar'

    compile 'com.google.code.geocoder-java:geocoder-java:0.15'

    compile files('libs/CWAC-Pager.jar')


android.applicationVariants.all  variant ->
    aptOutput = file("$project.buildDir/source/apt_generated/$variant.dirName")
    println "****************************"
    println "variant: $variant.name"
    println "manifest:  $variant.processResources.manifestFile"
    println "aptOutput:  $aptOutput"
    println "****************************"

    variant.javaCompile.doFirst 
        println "*** compile doFirst $variant.name"
        aptOutput.mkdirs()
        variant.javaCompile.options.compilerArgs += [
                '-processorpath', configurations.apt.getAsPath(),
                '-AandroidManifestFile=' + variant.processResources.manifestFile,
                '-s', aptOutput
        ]
    

给出错误Duplicate files copied in APK META-INF/LICENSE.txt:

WARNING: Dependency commons-logging:commons-logging:1.0.4 is ignored for defaultFlavorDebug as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency commons-logging:commons-logging:1.0.4 is ignored for defaultFlavorRelease as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Please read http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on the replacement for dynamic properties.
Deprecated dynamic property: "aptOutput" on "com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@5705013c", value: "/Users/david/Developer...".
****************************
variant: defaultFlavorDebug
manifest:  /Users/david/Developer/.../build/manifests/defaultFlavor/debug/AndroidManifest.xml
aptOutput:  /Users/david/Developer/.../build/source/apt_generated/defaultFlavor/debug
****************************
Deprecated dynamic property "aptOutput" created in multiple locations.
****************************
variant: defaultFlavorRelease
manifest:  /Users/david/Developer/.../build/manifests/defaultFlavor/release/AndroidManifest.xml
aptOutput:  /Users/david/Developer/.../build/source/apt_generated/defaultFlavor/release
****************************
:TravelGuard:compileDefaultFlavorDebugNdk UP-TO-DATE
:TravelGuard:preBuild UP-TO-DATE
:TravelGuard:preDefaultFlavorDebugBuild UP-TO-DATE
:TravelGuard:preDefaultFlavorReleaseBuild UP-TO-DATE
:TravelGuard:prepareComProlificinteractiveActionbarsherlock431Library UP-TO-DATE
:TravelGuard:prepareComViewpagerindicatorLibrary241Library UP-TO-DATE
:TravelGuard:prepareDefaultFlavorDebugDependencies
:TravelGuard:compileDefaultFlavorDebugAidl UP-TO-DATE
:TravelGuard:compileDefaultFlavorDebugRenderscript UP-TO-DATE
:TravelGuard:generateDefaultFlavorDebugBuildConfig UP-TO-DATE
:TravelGuard:mergeDefaultFlavorDebugAssets UP-TO-DATE
:TravelGuard:mergeDefaultFlavorDebugResources UP-TO-DATE
:TravelGuard:processDefaultFlavorDebugManifest UP-TO-DATE
:TravelGuard:processDefaultFlavorDebugResources UP-TO-DATE
:TravelGuard:generateDefaultFlavorDebugSources UP-TO-DATE
:TravelGuard:compileDefaultFlavorDebugJava
*** compile doFirst defaultFlavorDebug
/Users/david/Developer/.../src/main/java/com/travelguard/service/TravelGuardService.java:53: cannot find symbol
symbol  : class TravelGuardPrefs_
location: class com.travelguard.service.TravelGuardService
    TravelGuardPrefs_ travelGuardPrefs;
    ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CategoriesActivity.java:14: cannot find symbol
symbol  : class AssistanceFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.AssistanceFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CategoriesActivity.java:16: cannot find symbol
symbol  : class CategoryFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.CategoryFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CategoriesActivity.java:17: cannot find symbol
symbol  : class ContactFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.ContactFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CountriesActivity.java:25: cannot find symbol
symbol  : class TravelGuardPrefs_
location: package com.travelguard.service
import com.travelguard.service.TravelGuardPrefs_;
                              ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CountriesActivity.java:44: cannot find symbol
symbol  : class TravelGuardPrefs_
location: class com.travelguard.ui.CountriesActivity
    TravelGuardPrefs_ travelGuardPrefs;
    ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/InstructionsActivity.java:18: cannot find symbol
symbol  : class AssistanceFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.AssistanceFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/InstructionsActivity.java:19: cannot find symbol
symbol  : class ContactFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.ContactFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/InstructionsActivity.java:21: cannot find symbol
symbol  : class InstructionsFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.InstructionsFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/MainActivity.java:36: cannot find symbol
symbol  : class TravelGuardPrefs_
location: package com.travelguard.service
import com.travelguard.service.TravelGuardPrefs_;
                              ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/MainActivity.java:40: cannot find symbol
symbol  : class AssistanceFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.AssistanceFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/MainActivity.java:41: cannot find symbol
symbol  : class ContactFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.ContactFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/MainActivity.java:55: cannot find symbol
symbol  : class TravelGuardPrefs_
location: class com.travelguard.ui.MainActivity
    TravelGuardPrefs_ travelGuardPrefs;
    ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/TermsActivity.java:17: cannot find symbol
symbol  : class TravelGuardPrefs_
location: package com.travelguard.service
import com.travelguard.service.TravelGuardPrefs_;
                              ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/TermsActivity.java:23: cannot find symbol
symbol  : class TravelGuardPrefs_
location: class com.travelguard.ui.TermsActivity
    TravelGuardPrefs_ travelGuardPrefs;
    ^
Note: Starting AndroidAnnotations annotation processing
Note: AndroidManifest.xml file found: /Users/david/Developer/.../build/manifests/defaultFlavor/debug/AndroidManifest.xml
Note: Number of files generated by AndroidAnnotations: 22
Note: Generating source file: com.travelguard.service.SmartTravellerService_
Note: Generating source file: com.travelguard.service.SmartTravellerWebservice_
Note: Generating source file: com.travelguard.service.TravelGuardPrefs_
Note: Generating source file: com.travelguard.service.TravelGuardService_
Note: Generating source file: com.travelguard.service.TravelGuardWebservice_
Note: Generating source file: com.travelguard.ui.AdviceActivity_
Note: Generating source file: com.travelguard.ui.CategoriesActivity_
Note: Generating source file: com.travelguard.ui.ContactDetailActivity_
Note: Generating source file: com.travelguard.ui.ContactListActivity_
Note: Generating source file: com.travelguard.ui.ContactTravelGuardActivity_
Note: Generating source file: com.travelguard.ui.CountriesActivity_
Note: Generating source file: com.travelguard.ui.InformationActivity_
Note: Generating source file: com.travelguard.ui.InstructionsActivity_
Note: Generating source file: com.travelguard.ui.MainActivity_
Note: Generating source file: com.travelguard.ui.TermsActivity_
Note: Generating source file: com.travelguard.ui.TravelGuardActivity_
Note: Generating source file: com.travelguard.ui.fragments.AdviceDetailFragment_
Note: Generating source file: com.travelguard.ui.fragments.AdviceFragment_
Note: Generating source file: com.travelguard.ui.fragments.AssistanceFragment_
Note: Generating source file: com.travelguard.ui.fragments.CategoryFragment_
Note: Generating source file: com.travelguard.ui.fragments.ContactFragment_
Note: Generating source file: com.travelguard.ui.fragments.InstructionsFragment_
Note: Time measurements: [Whole Processing = 913 ms], [Generate Sources = 358 ms], [Extract Manifest = 163 ms], [Process Annotations = 158 ms], [Extract Annotations = 120 ms], [Validate Annotations = 82 ms], [Find R Classes = 28 ms],
Note: Time measurements: [Whole Processing = 0 ms],
Note: Time measurements: [Whole Processing = 0 ms],
warning: The following options were not recognized by any processor: '[androidManifestFile]'
Note: /Users/david/Developer/.../src/main/java/com/travelguard/ui/ContactDetailActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /Users/david/Developer/.../src/main/java/com/travelguard/service/SmartTravellerService.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:TravelGuard:preDexDefaultFlavorDebug UP-TO-DATE
:TravelGuard:dexDefaultFlavorDebug
:TravelGuard:processDefaultFlavorDebugJavaRes UP-TO-DATE
:TravelGuard:validateDebugSigning
:TravelGuard:packageDefaultFlavorDebug
Error: duplicate files during packaging of APK /Users/david/Developer/.../build/apk/TravelGuard-defaultFlavor-debug-unaligned.apk
    Path in archive: META-INF/LICENSE.txt
    Origin 1: /Users/david/.gradle/caches/modules-2/files-2.1/commons-httpclient/commons-httpclient/3.1/964cd74171f427720480efdec40a7c7f6e58426a/commons-httpclient-3.1.jar
    Origin 2: /Users/david/.gradle/caches/modules-2/files-2.1/commons-codec/commons-codec/1.2/397f4731a9f9b6eb1907e224911c77ea3aa27a8b/commons-codec-1.2.jar
:TravelGuard:packageDefaultFlavorDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':TravelGuard:packageDefaultFlavorDebug'.
> Duplicate files copied in APK META-INF/LICENSE.txt
    File 1: /Users/david/.gradle/caches/modules-2/files-2.1/commons-httpclient/commons-httpclient/3.1/964cd74171f427720480efdec40a7c7f6e58426a/commons-httpclient-3.1.jar
    File 2: /Users/david/.gradle/caches/modules-2/files-2.1/commons-httpclient/commons-httpclient/3.1/964cd74171f427720480efdec40a7c7f6e58426a/commons-httpclient-3.1.jar


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 21.957 secs

在我更新 Android Studio 之前,今天早上一切正常。有什么想法吗?

编辑:我可以通过运行类似的命令来临时修复它

zip -d spring-android-core-1.0.1.RELEASE.jar META-INF/notice.txt

直到所有重复的错误消失。

【问题讨论】:

另见***.com/questions/20673888/… 这似乎是错误code.google.com/p/android/issues/detail?id=61573。我不知道为什么在这个版本中它才开始给你带来麻烦。 遇到了同样的问题。决定继续使用 Android Studio 0.3.7 和 gradle 插件 0.6.3 直到 bug 解决 你应该清楚你的措辞:你没有使用 gradle 0.7.0 - 它是 android gradle 插件。它在 1.9.0 版中使用 gradle (with wrapper)。除了给定的上下文之外,错误的措辞会导致混淆。 请注意pickFirst 选项自Gradle 0.9.1 起可用。 【参考方案1】:

自 Android Studio 版本 0.8.14 起

你应该添加:

 android 
     packagingOptions  
         exclude 'META-INF/LICENSE.txt'
         exclude 'META-INF/NOTICE.txt'
         exclude '...'
     
   

到您的build.gradle 文件。

历史:

根据此错误中的评论 14:https://issuetracker.google.com/issues/36982149#comment14 这是 Android Gradle 插件 v0.7.0 中的错误,将很快在 0.7.1 中修复。

以下是该错误中关于 0.7.1 添加的注释:

0.7.1 已经解决了这个问题。

排除文件的DSL是:

android 
    packagingOptions 
        exclude 'META-INF/LICENSE.txt'
    

您可以根据需要添加任意数量的排除语句。该值是存档路径。尚无通配符或 glob 支持。

文件名“LICENSE.txt”和“NOTICE.txt”区分大小写。请尝试使用“license.txt”和“notice.txt”。

【讨论】:

谢谢!我还必须排除 'META-INF/NOTICE.txt' ;) 日复一日,我对程序员在 *** 到来之前如何远程高效工作感到惊讶。感谢您的修复!我不得不排除 META-INF/LICENSE 和 /NOTICE。 (没有 txt,即使在最新的 gradle 上也是如此。) 我应该编辑什么文件以及在哪里可以找到它?谢谢,埃亚尔。 @um.anusorn Sean 为您提供了一个很好的 gradle 文件示例,该文件在打包期间将这些文件从库中排除。这就是我的:packagingOptions exclude 'META-INF/LICENSE' exclude 'META-INF/NOTICE' exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/license.txt' exclude 'META-INF/ dependenices.txt' 排除 'META-INF/notice.txt' 在 v0.12 的内置工具中仍然普遍存在错误。只是说【参考方案2】:

问题在于 META-INF 文件夹不会被过滤,因此 NOTICELICENSE 的多个条目在构建时会导致重复,因此会尝试将它们复制在一起。

肮脏的快速修复:

打开.gradle/caches/... 文件夹中的.jar 文件(使用与zip 兼容的工具)并删除或重命名META-INF 文件夹中导致错误的文件(通常是NOTICELICENSE)。 (我知道这也在 OP 中,但对我来说,直到我阅读谷歌论坛才真正清楚)

编辑:

此问题已在 0.7.1 中修复。只需添加要排除的冲突文件即可。

android 
    packagingOptions 
        exclude 'META-INF/LICENSE'
    

【讨论】:

对不起,我现在才明白。 也许是最糟糕的答案:更改 gradle 缓存中的 jars 可能会更改它们的校验和,因此不会长时间工作......此外,那些不是你的 jars 是的,这就是为什么它用粗体字显示“Dirty Quick Fix”。正确的解决方案也只是一段距离。您确实意识到这是在没有干净解决方案的时候编写的,但人们仍然需要编译他们的应用程序。【参考方案3】:

我在 AOSP 中注意到了这个提交注释,解决方案是使用 DSL 排除一些文件。可能在 0.7.1 发布时。

commit e7669b24c1f23ba457fdee614ef7161b33feee69
Author: Xavier Ducrohet <--->
Date:   Thu Dec 19 10:21:04 2013 -0800

    Add DSL to exclude some files from packaging.

    This only applies to files coming from jar dependencies.
    The DSL is:

    android 
      packagingOptions 
        exclude 'META-INF/LICENSE.txt'
      
    

【讨论】:

是的,他们刚刚在构建站点上编写了发行说明,因此希望他们能尽快将工件上传到 maven Central,以便我们可以使用它! 如果我不想排除它们怎么办,只需重命名它们,以免它们发生冲突?【参考方案4】:
packagingOptions 
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'

【讨论】:

我需要.txt 后缀。 我正在做的其他事情让我回到了这里,我不需要.txt 后缀。所以我猜是 YMMV(或 MMMV,取决于一年中的时间)。【参考方案5】:

就我而言,我必须包括几个额外的排除项。看起来它不喜欢正则表达式,这会使它成为一个不错的单行。

android 
    packagingOptions 
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/notice.txt'
    

【讨论】:

我只需要LICENSENOTICE 在我添加commons-lang3 之后。 这个对我有帮助(我有 AndroidAnnotations + Spring REST 模板)。由于不得不添加这样的 hack,这个 IntelliJ 产品开始非常像 Eclipse... 不知道是哪个影响,但是这个真的很有帮助。 对我不起作用,仍然显示警告:依赖 org.apache.httpcomponents:httpclient:4.3 被忽略以进行调试,因为它可能与 Android 提供的内部版本冲突。如有问题,请用jarjar重新打包更改类包【参考方案6】:

这个bug在0.8+/1.10依然存在

杰克逊

compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.2.2'

在编译之前我必须包含上述建议

exclude 'META-INF/services/com.fasterxml.jackson.core.JsonFactory'

【讨论】:

如何排除“META-INF/services/com.fasterxml.jackson.core.JsonFactory”。我应该编辑哪个文件?【参考方案7】:

看看Sakiboy's comment!


过时的答案

从 Gradle 0.9.1 开始支持以下内容:

android.packagingOptions 
    pickFirst 'META-INF/LICENSE.txt'

更多信息请关注Gradle release notes。

【讨论】:

这是一个更好的解决方案,因为开发人员在使用开源软件时有义务包含这些文件。 不错的选择,但此评论完全错误。只有第一个库中的许可证将被包括在内,其余的将被忽略。 这在 2 个库使用相同的 .so 文件时很有用 我可以将它用于包含相同 .class 文件的两个 JAR 吗?我有两个 JAR,它们都包含 org/xmlpull/v1/XmlPullParser.class @PSIXO,如果您想包含所有许可证,您可以在下面看到答案:***.com/a/47828425/2371425【参考方案8】:

当我在 Android Studio 0.8.6 中导出库 httclient-4.3.5 时出现同样的问题 我需要包括这个:

packagingOptions
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'

库 zip 内容下一个 jar:

commons-codec-1.6.jar
commons-logging-1.1.3.jar
fluent-hc-4.3.5.jar
httpclient-4.3.5.jar
httpclient-cache-4.3.5.jar
httpcore-4.3.2.jar
httpmime-4.3.5.jar

【讨论】:

【参考方案9】:

重要的是要知道这个错误出现在哪个文件中(在你的例子中它是 META-INF/LICENSE.txt),在我的情况下它是在 META-INF/LICENSE [没有“.txt”],然后在 META-INF/ASL2.0 文件中,所以我在 build.gradle 中添加了以下几行:

android 
    packagingOptions 
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/ASL2.0'
    

非常重要 (!) -> 以您在错误消息中看到的相同样式添加文件名:文本区分大小写,*.txt 和 *( 不带 "文本文件”)。

【讨论】:

【参考方案10】:

在我的情况下,我只需要添加到项目的 build.gradle 文件中:

android 
  packagingOptions 
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
  

   ...

【讨论】:

【参考方案11】:

当我在 Android Studio 1.0.1 中使用 'org.springframework.android:spring-android-rest-template:2.0.0.M1' 时出现同样的问题。我需要将其包含在build.gradle

android
...
    packagingOptions
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
    
...

【讨论】:

【参考方案12】:

这里也一样

dependencies 
    compile 'org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:1.0.0'


packagingOptions 
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'

我为那个奇怪的错误损失了 2 天......为什么这仍然发生在 gradle 1.0.0 中?这对新手来说是非常令人不安的...... 无论如何,感谢您提供的信息,我认为它在我的代码中:)

【讨论】:

【参考方案13】:

在 Android Studio 1.1.0 中,我需要小写名称:

packagingOptions
      exclude 'META-INF/license.txt'
      exclude 'META-INF/notice.txt'

【讨论】:

【参考方案14】:

不止一个错误

apply plugin: 'android-library'

添加这个 ::

android 
    packagingOptions 
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    

如果文件重复,这很容易,查看META-INF 目录下的JAR 并查看导致错误的原因。它可能是多个。就我而言,Couchbase Lite 插件。 随着您添加更多插件,您将需要更多例外

【讨论】:

愚蠢的问题:那个 JAR 文件在哪里? 在 Android Studio 中,使用 --info 标志运行 Gradle 让我看到添加到 apk 的类的日志,然后我可以向上搜索以找到添加的同一类的日志条目来自不同的 JAR。这也向我展示了两个 JAR 文件的文件路径。我找不到一个好方法来选择一个 .class 文件而不是另一个,所以我修改了一个 JAR 文件并删除了重复的 .class【参考方案15】:

这对我有用:

android 
   packagingOptions 
       exclude 'LICENSE.txt'
   

【讨论】:

【参考方案16】:

我认为您只需要在 build.gradle 中包含这些选项:

packagingOptions 
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
    

p.s 与我的帖子相同的答案: Error :: duplicate files during packaging of APK

【讨论】:

【参考方案17】:

文件“LICENSE.txt”和“NOTICE.txt”区分大小写。所以对于 SPring android 库,我必须添加

android 
    packagingOptions 
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/notice.txt'
    

【讨论】:

【参考方案18】:

在 LICENSE 删除我的错误后删除 .txt:

packagingOptions 
    exclude 'META-INF/LICENSE'

【讨论】:

【参考方案19】:
  packagingOptions 
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/LGPL2.1'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/notice.txt'

【讨论】:

【参考方案20】:

如果您想作为开发人员尽自己的一份力量,利用开源库,您应该尝试在您的apk 中包含所有这些开源许可证。为此,您可以在packagingOptions 中使用merge 方法。

示例:

packagingOptions 
        // This will get include every license and notice regardless of what dir it’s in.
        merge '**/LICENSE.txt'
        merge '**/NOTICE.txt'
        merge '**/notice.txt'
        merge '**/license.txt'
        merge '**/NOTICE'
        merge '**/LICENSE'
        merge '**/notice'
        merge '**/license'
        merge '**/LGPL2.1'
        // This will exclude any README files, regardless of the dir or the file type.
        exclude '**/README.*'

这个答案比使用pickFirst 更好,因为该方法选择它找到的第一个许可证并忽略所有其余的,在这种情况下有点使它无用。

因此,简而言之,使用 merge 方法包含来自您一直在使用的那些 kickass 开源库的所有许可证。

更多关于GradlePackagingOptions的信息。

【讨论】:

以上是关于Android Gradle 插件 0.7.0:“打包 APK 期间重复文件”的主要内容,如果未能解决你的问题,请参考以下文章

Android Gradle 插件Gradle 构建机制 ⑤ ( 在 Android Studio 中查看 Android Gradle 插件源码 )

Android Gradle 插件Gradle 自定义 Plugin 插件 ③ ( 自定义插件作用 | Android Gradle 插件的扩展 | 自定义 Extension 扩展 )

Android Gradle 插件Gradle 自定义 Plugin 插件 ⑥ ( 在 buildSrc 模块中依赖 Android Gradle 插件 | 完整代码示例 )

Android Gradle 插件Gradle 自定义 Plugin 插件 ⑥ ( 在 buildSrc 模块中依赖 Android Gradle 插件 | 完整代码示例 )

Android Gradle 插件Gradle 自定义 Plugin 插件 ② ( buildSrc 目录中实现 Gradle 插件 | 实现 Gradle 插件代码 | 模块引入插件并编译 )

Android Gradle 插件Android Module 模块 build.gradle 构建脚本 Groovy 语法分析 ① ( Gradle 二进制插件引入 | Gradle依赖配置 )