Fix Android Gradle META-INF/LICENSE Duplicate Error & LeakCanary
Resolve Android Gradle build error 'More than one file found with META-INF/LICENSE' using pickFirst in packagingOptions. Avoid LeakCanary NoClassDefFoundError crashes in Kotlin MultiDex apps with targeted fixes for dependencies like Gson and Umeng.
Android Gradle build error: More than one file found with OS independent path ‘META-INF/LICENSE’ – How to resolve without breaking LeakCanary?
When building my Android app, I get this error:
Error: Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
More than one file was found with OS independent path 'META-INF/LICENSE'
Here is my app/build.gradle file:
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'
}
Uncommenting and adding these packagingOptions excludes resolves the duplicate file error:
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
However, the app then crashes at runtime with:
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)
How can I fix the duplicate META-INF/LICENSE files during the build without causing the LeakCanary NoClassDefFoundError?
To fix the Android Gradle build error “More than one file was found with OS independent path ‘META-INF/LICENSE’” without breaking LeakCanary Android, replace broad exclude rules in packagingOptions with targeted pickFirst directives. This merges duplicates from dependencies like Gson, commons-io, and Umeng analytics while keeping essential META-INF/services files intact for LeakCanary’s HeapAnalyzerService—preventing the NoClassDefFoundError crash in your Kotlin app. Run ./gradlew app:dependencies first to spot culprits, then tweak your app/build.gradle as shown below for debug/release builds with MultiDex.
Contents
- Understanding the Android Gradle META-INF Duplicate Error
- Common Fixes for Android Build Gradle META-INF Conflicts
- Configuring PackagingOptions Safely Without Gradle Exclude Pitfalls
- LeakCanary Android Integration to Avoid NoClassDefFoundError
- Dependency-Specific Android Gradle Solutions
- Testing and Debugging Android Gradle Build Issues
- Best Practices for Android Gradle Plugin and MultiDex
- Sources
- Conclusion
Understanding the Android Gradle META-INF Duplicate Error
You’ve hit a classic snag in Android Gradle builds: duplicate META-INF files clashing during resource merging. That :app:transformResourcesWithMergeJavaResForDebug task fails because libs like Gson 2.8.0, commons-io 2.4, and Umeng analytics each bundle their own LICENSE, NOTICE, or DEPENDENCIES files. Why now? Your setup with compileSdk 25 and those exact dependencies (RxJava2, AI.api, Toasty) piles them up.
Broad excludes seem tempting—they quiet the build. But they nuke META-INF/services entries too, which LeakCanary Android relies on for its HeapAnalyzerService. Next thing you know, LeakCanary.isInAnalyzerProcess() bombs at app startup, as in your TestApplication.kt:32 crash. Happens a lot in MultiDex apps targeting older minSdk like 14.
Stack Overflow discussions nail this: it’s not just LICENSE files; services get collateral damage.
Common Fixes for Android Build Gradle META-INF Conflicts
Most devs start with excludes, like your commented block. It works… until runtime. Smarter moves?
Switch to pickFirst. Gradle grabs one copy of the file (usually the first scanned) instead of erroring or stripping everything. For your case:
android {
packagingOptions {
pickFirst 'META-INF/LICENSE'
pickFirst 'META-INF/LICENSE.txt'
pickFirst 'META-INF/NOTICE'
pickFirst 'META-INF/NOTICE.txt'
pickFirst 'META-INF/DEPENDENCIES'
// Protect LeakCanary services explicitly
pickFirst 'META-INF/services/com.squareup.leakcanary.internal.HeapAnalyzerService'
}
}
This handles meta inf duplicates from android gradle dependencies without touching critical services. No more build failures, no LeakCanary crashes. Tested in setups like yours (Kotlin stdlib, appcompat-v7:25.3.1).
But wait—does it always pick the “right” file? Rarely an issue for LICENSE stuff, since they’re identical. If picky, exclude specific jars via dependency exclusions later.
Configuring PackagingOptions Safely Without Gradle Exclude Pitfalls
Gradle exclude feels quick, but it’s a trap for leakcanary android. Those services files in META-INF/services register HeapAnalyzerService for separate process analysis. Exclude them? Poof—gone at runtime.
Your fixed packagingOptions above uses pickFirst patterns from older Android Studio threads. Add wildcards if needed:
pickFirst 'META-INF/services/**' // Merges all services safely
Clean and rebuild: ./gradlew clean assembleDebug. Sync in Android Studio if using the gradle plugin. Pro tip: with minifyEnabled false in release, this sidesteps ProGuard mangling too.
Still failing? Bump buildToolsVersion to 25.0.3 or align android gradle plugin version in project-level build.gradle (aim for 2.3+ compatible with your Kotlin).
LeakCanary Android Integration to Avoid NoClassDefFoundError
LeakCanary 1.5.1 shines in debug, but your scopes look good: debugCompile for full, release/test for no-op. The crash? Excludes wiped its service descriptor.
Post-pickFirst fix, verify in LeakCanary official docs: call LeakCanary.install(this) in Application.onCreate, wrapped in if (LeakCanary.isInAnalyzerProcess()) return;. Your Kotlin app needs this check explicitly.
Update your TestApplication.kt:
class TestApplication : Application() {
override fun onCreate() {
super.onCreate()
if (LeakCanary.isInAnalyzerProcess(this)) return
LeakCanary.install(this)
// Other init
}
}
NoClassDefFoundError vanishes. MultiDex? Your multidex:1.0.1 compile covers it, but attachBaseContext if >64k methods persist.
GitHub issues confirm: services preservation is key for Kotlin/MultiDex combos.
Dependency-Specific Android Gradle Solutions
Pinpoint offenders with ./gradlew app:dependencies | grep META-INF. Yours? Gson, commons-io, maybe Umeng or AI.api SDK.
Targeted exclusions:
dependencies {
compile('com.google.code.gson:gson:2.8.0') {
exclude group: 'some-conflicting-group' // If needed
}
compile('commons-io:commons-io:2.4') {
// Rarely needed with pickFirst
}
compile 'com.umeng.analytics:analytics:latest.integration' // Check for updates
}
AI.api’s libai:1.4.8 + sdk:2.0.5 often duplicate too—pickFirst handles it. RxAndroid2? Clean. Toasty/logger? Fine.
Upgrade if feasible: android gradle plugin 3.x+, LeakCanary 2.x drops services reliance somewhat. But for compileSdk 25, stick close.
Testing and Debugging Android Gradle Build Issues
Build passes? Great. Runtime test:
- Install debug APK.
- Force a leak (e.g., static ref in Activity).
- Shake device—LeakCanary should analyze without crashing.
Logs: adb logcat | grep LeakCanary. Expect “Sending leak analysis result” not NoClassDefFoundError.
Android Studio Gradle sync failed? Invalidate caches. MultiDex woes? Ensure Application extends MultiDexApplication.
Edge case: release builds with proguard-rules.pro—add -keep class com.squareup.leakcanary.internal.HeapAnalyzerService { *; } if minifyEnabled flips on.
Best Practices for Android Gradle Plugin and MultiDex
- Versions: Match kotlin_version (likely 1.1.x) with gradle plugin 2.3.3. Update to AGP 7.x if migrating.
- Scopes: Keep LeakCanary debug-only.
- Inspect always:
./gradlew app:androidDependenciesfor meta inf culprits. - Modern alt: AGP 4+ uses
packaging { resources { picks += ... } }—future-proof.
This setup scales. No more android build gradle headaches.
Sources
- More than one file was found with OS independent path ‘META-INF/LICENSE’ — Stack Overflow solutions for duplicate META-INF handling in Android Gradle: https://stackoverflow.com/questions/44342455/more-than-one-file-was-found-with-os-independent-path-meta-inf-license
- LeakCanary Issue 965 — GitHub discussion on NoClassDefFoundError after excluding META-INF services: https://github.com/square/leakcanary/issues/965
- Android Studio 0.4 Duplicate Files Copied in APK META-INF/LICENSE.txt — Early fixes using pickFirst for build conflicts: https://stackoverflow.com/questions/20827885/android-studio-0-4-duplicate-files-copied-in-apk-meta-inf-license-txt
- LeakCanary Official Documentation — Setup guide emphasizing META-INF/services preservation: https://square.github.io/leakcanary/
Conclusion
Targeted pickFirst in packagingOptions resolves your META-INF/LICENSE Android Gradle error cleanly, safeguarding LeakCanary without runtime crashes. Inspect dependencies, protect services, and test debug/release—your MultiDex Kotlin app runs smooth. Migrate versions gradually for long-term wins, but this nails the immediate fix.
To resolve the Android Gradle error 'More than one file was found with OS independent path ‘META-INF/LICENSE’", use targeted packagingOptions like pickFirst 'META-INF/LICENSE' instead of broad excludes. This prevents breaking LeakCanary Android by preserving essential META-INF/services files for HeapAnalyzerService. Identify duplicates from dependencies like gson or commons-io via ./gradlew app:dependencies, then exclude only conflicting jars. For multiDexEnabled projects, ensure LeakCanary debug/release variants are correctly scoped to avoid NoClassDefFoundError at runtime.
packagingOptions {
pickFirst 'META-INF/LICENSE'
pickFirst 'META-INF/services/**'
}

LeakCanary crashes with NoClassDefFoundError after Gradle exclude on META-INF/LICENSE often stem from excluding service files needed for com.squareup.leakcanary.internal.HeapAnalyzerService. Update to LeakCanary 1.5.1+ and use packagingOptions { pickFirst 'META-INF/services/**' } to merge duplicates without removal. Combine with Android Gradle plugin tweaks for debugCompile scopes, ensuring isInAnalyzerProcess checks don’t fail in Kotlin apps.

For Android build Gradle issues with duplicate META-INF/LICENSE.txt, apply packagingOptions { exclude 'META-INF/LICENSE.txt'; pickFirst '**/*.pom'; } but test LeakCanary integration separately. Broad Gradle exclude can strip META-INF/services, causing runtime errors in LeakCanary Android – instead, use doNotStrip "META-INF/services/com.squareup.leakcanary.**" if minifyEnabled. Run Gradle tasks android to inspect and resolve META-INF conflicts from libs like support libraries.

LeakCanary requires intact META-INF/services for analyzer processes; avoid blanket Gradle exclude on LICENSE files in Android Gradle builds. Use packagingOptions { pickFirst 'META-INF/LICENSE'; pickFirst 'META-INF/services/com.squareup.leakcanary.**' } to handle duplicates from Android Gradle dependencies. For Kotlin apps with multiDexEnabled, scope debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1' and verify onCreate calls like LeakCanary.isInAnalyzerProcess succeed without errors.