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

错误:执行失败任务':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,并检查生成错误日志。 如果该日志与INotification类相关。在android{}中使用

configurations {
    all*.exclude group: 'com.android.support', module: 'support-v4'
}

这对我有帮助。

其他回答

在许多关于这个问题的SO答案中,有人建议添加排除'META-INF/DEPENDENCIES'和其他一些排除。然而,这些方法对我都不起作用。我的情况是这样的:

我在依赖关系中添加了这个:

implementation 'androidx.annotation:annotation:1.1.0'

我还在gradle.properties中添加了这个:

android.useAndroidX=true

这两个我都添加了,因为我得到了构建错误“无法找到符号类Nullable”,它被建议作为解决这个问题的一些答案,比如这里

然而,最终我陷入了get错误:

 More than one file was found with OS independent path 'androidsupportmultidexversion.txt'

没有排斥对我有效。最后,我只是删除了上面添加的那些行只是出于怀疑(解决了'无法找到符号类Nullable'以某种替代方式),最后我摆脱了这个“不止一个文件被发现与操作系统…”构建错误。我浪费了好几个小时。但最后还是摆脱了。

将其放入构建中。gradle(模块AppName)

android {
   // ....
   packagingOptions{
     pickFirst "androidsupportmultidexversion.txt"
  }
}

有类似的信息

错误:执行失败任务':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'
    <..>

如果你有这个问题,你有一个gradle .jar依赖,像这样:

implementation group: 'org.mortbay.jetty', name: 'jetty', version: '6.1.26'

间隔版本,直到一个匹配并解决异常,并应用此线程的最佳答案。

基本上,当gradle将apk文件放在一起时,它会从所有编译依赖项中复制内容,它足够智能,可以看到有一个来自两个不同jar文件的重复文件..这可以是任何文件,如a.txt或META-INF/DEPENDENCIES。最好使用下面的方法排除这些文件,以防文件只是为了提供信息而存在。

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

或者,如果在这种情况下,该文件是一个强制性的文件,就像类文件一样,已经在两个相互关联的jar依赖项之间复制,那么最好找到这些jar的替代方案,以更兼容的版本。