59

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?

m.myalkin
  • 1,128
  • 1
  • 10
  • 18

12 Answers12

116

./gradlew clean fixed the same error for me.

mixel
  • 25,177
  • 13
  • 126
  • 165
58

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

w3spi
  • 4,380
  • 9
  • 47
  • 80
  • 3
    Yep, this is the fix for Cordova projects. You can also just clean android side by adding `android` at the end of that command. – TheTC May 24 '19 at 17:01
9

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.

m.myalkin
  • 1,128
  • 1
  • 10
  • 18
9

Simple Solution

For Ionic and Cordove Projects

cordova clean
Shashwat Gupta
  • 5,071
  • 41
  • 33
4

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
    }
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
4

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.

oshurmamadov
  • 264
  • 8
  • 14
1

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.

bartonstanley
  • 1,167
  • 12
  • 25
1

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).

dazed
  • 352
  • 3
  • 9
1

Currently using Android Studio 3.3.2 I just disabled the instant run and it worked.

Maxime Claude
  • 965
  • 12
  • 27
0

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'
Mark
  • 520
  • 1
  • 7
  • 21
JenniferG
  • 602
  • 1
  • 5
  • 13
0

None of the current solutions worked for me and it was fixed by simply disabling Instant Run.

Diego Malone
  • 1,044
  • 9
  • 25
-4

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'
}
Fenil Patel
  • 1,528
  • 11
  • 28