问题
I have a library I use for Espresso tests that when I added to my project I'm not able to compile my tests.
Gradle outputs this error
Caused by: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
at com.android.dx.merge.DexMerger$8.updateIndex(DexMerger.java:565)
at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:276)
at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:574)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:166)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:198)
Which is really weird because I already have multiDex enabled in my project
My Project build.gradle
defaultConfig {
minSdkVersion 16
targetSdkVersion 21
versionName versionNameFromGitTagVia()
versionCode versionCodeFromJenkins()
multiDexEnabled true
testInstrumentationRunner "app.test.general.InstrumentationRunner" ...
}
dependencies {
...
androidTestImplementation project(':test-utils')
...
implementation 'com.android.support:multidex:1.0.2'
}
My Application Class
public class RiderApplication extends MultiDexApplication implements Application.ActivityLifecycleCallbacks,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
....
}
AndroidManifest
<application
android:name=".RiderApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@style/MyAppTheme"
tools:replace="android:theme,android:icon">
Removing the library solves the problem
Any suggestions?
EDIT I tried to fix it in several ways, and I discovered that this only happends when I include the library as
androidTestImplementation
But when used as a regular
implementation
The dex error disappears
Really strange
EDIT
It only happens with gradle 3.0.1, if I go back to gradle 2.3.3 the problem is no more
回答1:
Solved by updating gradle build tools from version 3.0.1 to 3.1.0-rc02. So please try:
classpath 'com.android.tools.build:gradle:3.1.0-rc02'
回答2:
Well, this is a strange one but it has happened before.
I've stumbled upon this thread, whish suggested this addition:
dexOptions {
jumboMode = true
// Avoid the OutOfMemoryError: GC overhead limit exceeded:
incremental true
javaMaxHeapSize "4g"
}
This should handle the issue of the string number being too large, as opposed to the number of methods being too large.
More on jumbo mode vs multidex here: Android: Jumbo Mode vs Multidex
Happy testing.
回答3:
Well, after many hours of testing I finally got it to run.
I have changed several things in the project, so I can't tell what was the one that caused the problem, but for the greater good I'll write here everything I did and I hope that if someone will ever encounter with such problem at least one solution will help her.
So I changed minSdkVersion to 21. Yes, it wasn't a requirement with Android Studio 2.3.3 but apparently it is now if you're going to use espresso.
But worry not, your application can still support older versions when not testing. To configure it that way I've added this to my build.gradle
def isTest = gradle.startParameter.taskNames.find { it.contains("AndroidTest") }
if (isTest != null) {
println("is test true")
minSdkVersion 21
} else {
println("is not test")
minSdkVersion 16
}
Other than that, I updated multiDex dependency from 1.0.1
to 1.0.2
and I enforced that on all dependencies.
I also updated guava
library to com.google.guava:guava:22.0-android
, which led to a dependency conflict that I solved by forcing another library (all forcing strategy is posted below).
I then had a dependency conflict with espresso-web
that I needed to enforce too.
This is my resolution strategy at the moment
configurations.all {
resolutionStrategy {
force 'com.android.support:multidex:1.0.2'
force 'com.google.guava:guava:22.0-android'
force 'com.google.code.findbugs:jsr305:2.0.1'
force 'com.android.support.test.espresso:espresso-web:3.0.1'
}
}
I hope this helps anyone, thank you so much for all the people who tried to help, you brought many ideas that helped me succeeded with the final resolution
来源:https://stackoverflow.com/questions/47432055/com-android-dex-dexindexoverflowexception-on-android-studio-3-0