Android "Multiple Dex files define" error

Source: Internet
Author: User

In the development of Android often see this error, the reason is still worth studying.

If this is the problem that occurs on eclipse, it can basically be judged that there are identical jar packages or different jar packages in the same project, but they have the same class, so the solution is to kill each other. In eclipse, if the jar is also reported wrong after it is deleted, it may be the result of Eclipse's cache, and restarting eclipse will basically solve the problem.

But if it happens on Android Studio, it's complicated. The first thing to do is to rule out the situation in eclipse, and be careful if the problem is not resolved.

If the engineering structure is like this

? Tree root

Root

├──project1

│└──libs

│└──abc.jar

└──project2

└──libs

└──abc.jar

Such a structure is still very common, for example, a lot of times libs below is Android-support-v4.jar, if the two projects only a dependency Abc.jar then delete the problem solved.

But the usual situation may be that both projects do need to be abc.jar, so the problem is tricky (personally, as the bug), each project needs, but a compile error.

One of my gut thoughts is to create a shared project3 that is dedicated to storing the shared jar package, so the structure becomes this way,

? Tree root

Root

├──project1//depends on Project3

│└──libs

├──project2//depends on Project3

│└──libs

└──project3

└──libs

└──abc.jar

But this compilation has caused more "multiple Dex files define" problem, because Project1, project2 because of dependency Project3, in the compilation phase will copy Abc.jar to their own project, The result is that all three projects cause jar packet collisions with each other.

Google Baidu after the conclusion is that this

If you have a local jar or an AAR library that is you want to use the more than one project, you cannot just reference it directl Y as a local dependency. This is because the Android plugin would complain if it finds the same jar file twice when dexing the project and all its D Ependencies. (Note that right now you can ' t actually with a local AAR file even if you have only reference it once).

From the official website of Android Studio, which means that it's best not to use it when using Android Studio.

But it still offers a solution.

The one-to-be-to-be-to-deploy the artifact in a repository. While it's possible, it might not being convenient due to the overhead of managing such a repository.

This scheme means that if you put the jar in a repo, you can refer to it directly, but the management repo is troublesome, for example, we often use the compile ' com.android.support:support-v4:21.0.0 ' The phrase is to get the jar package we need from Repo (jcenter) (so if the SUPPORT-V4 package is repeated, then the problem is solved by changing it directly).

But if it's a private jar, it's too cumbersome to put it in the repo, not too realistic. Or Google to the scheme, still use such a structure, but slightly modified

? Tree root

Root

├──project1//depends on Project3

│└──libs

├──project2//depends on Project3

│└──libs

└──project3

└──libs

└──abc.jar

Add the following two sentences to the project3 build.gradle file

dependencies {

Compile Filetree (dir: ' Libs ', include: [' *.jar '])

}

Artifacts.add ("Default", File (' Abc.jar '))

We use the jar package as a PROJECT3 output package (for specific reasons).

PS: The reason for this is that each project relies on another project's output, the "artifacts", where the action is to force the Abc.jar to be added as the output package for this project. It is recommended to use the beta to test the app in a full range: http://www.ineice.com/

Android "Multiple Dex files define" error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.