OSに依存しないパス「META-INF/LICENSE」を持つファイルが複数見つかりました 質問する

OSに依存しないパス「META-INF/LICENSE」を持つファイルが複数見つかりました 質問する

アプリをビルドすると、次のエラーが発生します。

エラー: タスク ':app:transformResourcesWithMergeJavaResForDebug' の実行に失敗しました。OS に依存しないパス 'META-INF/LICENSE' を持つファイルが複数見つかりました

これは私のbuild.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'
}

このコードをbuild.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)

これを解決するアイデアを持っている人はいますか?

ベストアンサー1

これをyourProject/app/build.gradle内部に追加することができますandroid{}除外する関数は、名前付きリソースを 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") 
      }          
}

このexclude関数は 7.0.2 では非推奨となり、次のようなものを使用する必要があります。

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

おすすめ記事