java.lang.IllegalStateException: Dex archives: setting .DEX extension only for .CLASS files
AndroidBuildContinuous IntegrationKotlinGitlabAndroid Problem Overview
I build the project at gitlab ci
./gradlew assembleDebug --stacktrace
and sometimes it throws an error:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:transformClassesWithDexBuilderForDebug'.
> com.android.build.api.transform.TransformException: java.lang.IllegalStateException: Dex archives: setting .DEX extension only for .CLASS files
At my local pc it works correctly.
kotlin version is 1.2
multidex is enabled
What is the reason of this error?
Android Solutions
Solution 1 - Android
./gradlew clean
fixed the same error for me.
Solution 2 - Android
For Cordova developers,
If you get this build error in your project, as said Pierrick Martellière in the comments of this answer, in you project folder use :
cordova clean
It makes a cleaning and a build immediately
Solution 3 - Android
It seems I found the solution. At the build moment gradle was showing warnings for me:
Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
app: 'androidProcessor' dependencies won't be recognized as kapt annotation processors. Please change the configuration name to 'kapt' for these artifacts: 'com.arello-mobile:moxy-compiler:1.5.3' and apply the kapt plugin: "apply plugin: 'kotlin-kapt'".
I made a misspelling and forgot to remove unnecessary annotationProcessor for library:
annotationProcessor "com.arello-mobile:moxy-compiler:$moxyVersion"
kapt "com.arello-mobile:moxy-compiler:$moxyVersion"
So I removed first line.
After that I applied kapt plugin apply plugin: 'kotlin-kapt'
and fixed some build errors in code after it.
After all I realized that I forgot to replace compile
to implementation
in some places. It is weird but without it build didn't work.
This changes fix my error build.
Solution 4 - Android
> Simple Solution
For Ionic and Cordove Projects
cordova clean
Solution 5 - Android
Above answer is mostly right but in my case, i get this exception when i crate same name java and kotlin file then deletes one of them.
Solutions are: just Build -> Clean Project my project and it works. And my project also enabled multiDex.
defaultConfig {
...
// Enabling multidex support.
multiDexEnabled true
}
Solution 6 - Android
As mentioned above by @mixel cleaning gets the job done. But as an option not to do it manually just add the gradle 'clean' task into app run configuration so it will be done all the time before launch. Of cource, it can slow down the whole process a bit.
Solution 7 - Android
Configuring for multidexing did not solve this issue for me.
However I did come up with a resolution...of sorts. Basically it involved creating a pull request for a second branch on the same commit as the build that was failing. The build for this pull request succeeded, and then Bitbucket thought that the original pull request was ok and allowed us to merge, even though we had made no changes on that branch. There is some unexplained weirdness there but the technique worked.
Here's how I did it:
Assume that the branch that is failing is called bad-branch
.
I created a new branch called bad-branch-copy
on the commit that was common between bad-branch
and develop
. Then I merged bad-branch
into bad-branch-copy
. The end result of this was a fast forward such that bad-branch-copy
ended up on the same commit as bad-branch
. I was expecting a separate commit so this result surprised me, but I was grasping at straws anyway so I kept going.
I then pushed bad-branch-copy
to GitHub and created a pull request from bad-branch-copy
to develop
. This triggered a build on bad-branch-copy
-> develop
, which was successful.
At that point, buddybuild showed a successful build on bad-branch-copy
-> develop
and still showed a failure on bad-branch
-> develop
. However, Bitbucket showed a successful build on the pull request for bad-branch
. Yes, that's right: buddybuild showed a failure but Bitbucket said it was ok.
We were then able to merge the bad-branch
pull request and all was well with the world. Please don't ask me why, I will not answer. :)
I think the same thing could be accomplished with
git checkout bad-build
git checkout -b bad-build-copy
git push origin bad-build-copy
followed by creating a pull request for bad-build-copy.
Solution 8 - Android
I was able to get the problem to go away by closing and restarting Android Studio. Perhaps even a Rebuild Project would have done it as well (did not try that though).
Solution 9 - Android
Currently using Android Studio 3.3.2 I just disabled the instant run and it worked.
Solution 10 - Android
What resolved the issue for me was manually adding all the conflicting files manually. For example in my gradle file I had:
implementation 'com.android.support:support-compat:27.1.1'
it was underlined with red. I hovered over the line and android studio said there was a conflict with another file using a lower version. The error was similar to
> come.android.support:support-annotations:26.0.1 conflicts/mix versions with 27.1.1.
It gave me the names of the files and I manually added them with the corresponding version so they would all match
compile 'com.android.support:support-annotations:27.1.1'
compile 'com.android.support:support-compat:27.1.1'
compile 'com.android.support:support-core-ui:27.1.1'
compile 'com.android.support:animated-vector-drawable:27.1.1'
compile 'com.android.support:appcompat-v7:27.1.1'
compile 'com.android.support:design:27.1.1'
Solution 11 - Android
None of the current solutions worked for me and it was fixed by simply disabling Instant Run
.
Solution 12 - Android
please find here the solution of this problem,
defaultConfig {
...
minSdkVersion 14
targetSdkVersion 21
...
// Enabling multidex support.
multiDexEnabled true
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
}