当我建立我的应用程序,我得到以下错误:

错误:执行失败任务':app:transformResourcesWithMergeJavaResForDebug'。 在OS独立路径'META-INF/LICENSE'中发现了多个文件

这是我的身材。gradle文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "cn.sz.cyrus.kotlintest"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        javaCompileOptions{
            annotationProcessorOptions{
                includeCompileClasspath = true
            }
        }
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
 /*       exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'*/
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    compile 'com.github.GrenderG:Toasty:1.2.5'
    compile 'com.orhanobut:logger:1.15'

    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.umeng.analytics:analytics:latest.integration'
    compile 'ai.api:libai:1.4.8'
    compile 'ai.api:sdk:2.0.5@aar'
// api.ai SDK dependencies
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'commons-io:commons-io:2.4'
    compile 'com.android.support:multidex:1.0.1'
}

当我将此代码添加到我的构建时。gradle文件,

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

这个错误将得到解决,但另一个问题将发生。是这样的:

java.lang.NoClassDefFoundError: com.squareup.leakcanary.internal.HeapAnalyzerService
at com.squareup.leakcanary.LeakCanary.isInAnalyzerProcess(LeakCanary.java:145)
at cn.sz.cyrus.wemz.TestApplication.onCreate(TestApplication.kt:32)

谁有办法解决这个问题?


当前回答

我也有同样的问题,只是通过改变multidex实现来解决

From

implementation 'com.android.support:multidex:1.0.3'

To

implementation 'androidx.multidex:multidex:2.0.1'

其他回答

在android{}中添加multiDexEnabled true

并删除实现'com.android.support:multidex:x.x。X '来自依赖项

如果你使用多模块项目,在espresso测试时遇到这个问题,你需要在每个gradle文件中添加packagingOptions代码。在我的例子中,我为每个gradle文件添加了下面的代码。

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

有类似的信息

错误:执行失败任务':app:transformResourcesWithMergeJavaResForDebug'。 在操作系统独立路径“constant-values.html”中发现了多个文件

为了解决这个问题,我不得不在Android Studio中启用包视图(1),然后浏览树到库,并找到重复的包(2)

然后,ctrl+alt+f12(或RMB菜单)(3)-并找到导致问题的库。 列出这些库中导致问题的文件,并将它们写入应用程序的构建中。Gradle文件在android部分。另一种方法是处理包含重复文件的库

packagingOptions {
    exclude 'allclasses-frame.html'
    exclude 'allclasses-noframe.html'
    <..>

排除所有仅用于文档和信息目的的元信息文件是完全安全的:

android{
    packagingOptions {
       exclude 'META-INF/*'
    }
}

来源:https://stackoverflow.com/a/49101232/13093413

你可以在project /app/build中添加这个。Gradle在android{}内。exclude函数将指定的资源添加到APK中未打包的资源列表中。

android {      
      packagingOptions {
        exclude("META-INF/DEPENDENCIES")
        exclude("META-INF/LICENSE")
        exclude("META-INF/LICENSE.txt")
        exclude("META-INF/license.txt")
        exclude("META-INF/NOTICE")
        exclude("META-INF/NOTICE.txt")
        exclude("META-INF/notice.txt")
        exclude("META-INF/ASL2.0")
        exclude("META-INF/*.kotlin_module") 
      }          
}

排除函数在7.0.2中被弃用,你应该使用类似这样的函数:

android {
   ...
   packagingOptions {
       resources.excludes.add("META-INF/*")
   }
}