测试Android应用时遇多DEX文件定义冲突错误,求解决方法
Hey there, let's get this dex conflict sorted out for you! That error pops up when two or more dependencies in your project define the exact same class—in this case, IInAppBillingService from Google's Play Billing system.
Looking at your build.gradle dependencies, here's what's causing the problem:
- You've added
com.android.billingclient:billing:1.0twice (duplicate dependency) - You're also using
com.anjlab.android.iab.v3:library:1.0.44, a third-party In-App Billing library that includes its own copy of theIInAppBillingServiceclass—this clashes directly with the official Google Billing Client library.
Solution: Pick One Billing Library & Clean Up Dependencies
You can't use both the official Google Billing Client and the third-party ANJLab IAB library together. Choose one, and remove the other. Also, clean up any duplicate dependencies.
Option 1: Use Official Google Billing Client (Recommended)
Remove the third-party IAB library and the duplicate billingclient entry. Here's your cleaned-up dependencies block:
dependencies { api fileTree(include: ['*.jar'], dir: 'libs') api 'com.android.billingclient:billing:1.0' androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) //noinspection GradleCompatible,GradleCompatible api 'com.android.support:appcompat-v7:27.0.2' api 'com.google.android.gms:play-services-ads:11.8.0' api 'com.android.support:design:27.0.2' api 'com.android.support.constraint:constraint-layout:1.0.2' api 'com.android.support:support-v4:27.0.2' testCompile 'junit:junit:4.12' api 'com.android.support:gridlayout-v7:27.0.2' }
Option 2: Stick with the Third-Party ANJLab IAB Library
If you need to keep using the ANJLab library, remove both instances of the official billingclient dependency:
dependencies { api fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) //noinspection GradleCompatible,GradleCompatible api 'com.android.support:appcompat-v7:27.0.2' api 'com.google.android.gms:play-services-ads:11.8.0' api 'com.android.support:design:27.0.2' api 'com.android.support.constraint:constraint-layout:1.0.2' api 'com.anjlab.android.iab.v3:library:1.0.44' api 'com.android.support:support-v4:27.0.2' testCompile 'junit:junit:4.12' api 'com.android.support:gridlayout-v7:27.0.2' }
Extra Tip
I noticed you also have duplicate com.android.support:support-v4:27.0.2 entries—cleaning those up (like I did in the examples above) helps keep your dependency tree tidy and avoids potential future issues.
After making these changes, run Clean Project followed by Rebuild Project in Android Studio, and the dex error should be gone!
内容的提问来源于stack exchange,提问作者Epir

