com.android.dex.DexException: Multiple dex files define while ant build - android

I know that there are a lot of posts about this exception but non of them seemed to be helpful. I have deleted all the bin and gen folders, i have removed all library dependencies and added them again but this issue still remains.
This is the exception I get:
[dex] Pre-Dexing /home/me/dev/Workspaces/FacebookSDK/libs/android-support-v4.jar -> android-support-v4-40c394661b7676703ffc1366ddef840c.jar
[dex] Converting compiled files and external libraries into /home/me/dev/Workspaces/MyApp/bin/classes.dex...
[dx]
[dx] UNEXPECTED TOP-LEVEL EXCEPTION:
[dx] com.android.dex.DexException: Multiple dex files define Lcom/my/app/Manifest$permission;
[dx] at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:593)
[dx] at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:551)
[dx] at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:532)
[dx] at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:169)
[dx] at com.android.dx.merge.DexMerger.merge(DexMerger.java:187)
[dx] at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
[dx] at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
[dx] at com.android.dx.command.dexer.Main.run(Main.java:230)
[dx] at com.android.dx.command.dexer.Main.main(Main.java:199)
[dx] at com.android.dx.command.Main.main(Main.java:103)
[dx]
could be something with the manifest merger?
didn't find anything unusual in my manifests files (app and libraries etc..)
App compiles and works when built and launched from eclipse.
Any ideas?
Thanks!

Consider showing your AndroidManifest.xml and build.gradle files here.
This problem is mainly because you are trying to compile two modules (mainly app and library) with same package-name for your app.
Try to change your package-name for a while, to see if that's the case.
Also this problem has been seen when libraries are added more than one time accidently. Try to see how you're compiling the libraries.

Related

Android Studio - UNEXPECTED TOP LEVEL EXCEPTION

I can't figure out what I'm doing wrong, or have done wrong to debug my app. Can anyone give me an idea of how to fix this?
I deleted all the libraries and reinstalled them, and I've worked out that it's an issue with the Google Repository. But I don't know what to do. Any ideas would be great.
Thanks
Error:Execution failed for task ':SecuriHUB:dexDebug'.
com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Program Files (x86)\Android\android-studio1\sdk\build-tools\android-4.4W\dx.bat --dex --num-threads=4 --output C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\dex\debug C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\classes\debug C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\dependency-cache\debug C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\avcodec-a23ce361a09c930e68fee244ef4b9f70b8df1da4.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\avcodec-bab13459b97dce683c98960d6342d99522af142f.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\avutil-52522a8339be7c888d2262803acf07db17ec6aa2.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\avutil-6a389e589f441392b5746bb77bf970e260c15ac8.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\classes-2b5def6b4c7c7ce682519b9b0443fe04a26c0919.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\classes-2f5552383b37e78f4ec718f853bc1b1e1ec317a2.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\classes-42b726da5c048a82a08e89a4b70c184b94608aa0.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\classes-6e01a35945fee3c0661a964108bc1f436ada1652.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\classes-76fa51ae23d8bf77e8657dce65fcc7cf5b61044d.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\classes-96196602ae75c43f247b5c746cd935d656b8b543.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\classes-9b73bafe39f49c24dfc02321659c168a6d30c9b9.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\classes-cae5aaa06f449bff8bee4372de96fa33783e31ee.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\classes-ee9ef6ddaaef0705b573495d987a8ea074f0bc0a.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\classes-fe7b9a83f65f77ec6904839666546d97f8c5c435.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\commons-io-1.3.2-76b40519b6d96123e0fafc55a0437071960ae683.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\commons-io-1.3.2-e23cc4dead7a42abc070337b6602255ca7a78ff3.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\GSSc-3ac0bc0f497991728bf7e0dc89da2dc1425604d1.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\GSSc-577c0e7d97895f2804faa5f54f1999124502db90.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\httpclientandroidlib-1.1.2-2caa8f5d76aec2bd9d92323cc8b178d8d67f6081.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\httpclientandroidlib-1.1.2-ec8f9ce27a72b4bf5d7cf1b5784c45f09b73caa0.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\internal_impl-20.0.0-45e7f39ff8ef841d2b829e696be46453d466481b.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\internal_impl-20.0.0-565a411117f2b6f68963042a0d52ec0b2b843b4d.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\json-1.0.0-0722a5a7126e9051d1bd542a6af3e6bc94116d50.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\json-1.0.0-2f404cb0d63823de6ac84000bf670564809870b7.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\redmine-java-api-1.23.1-25bdd461e5cb7eb722db4cdfb2f55f44c1df1fe6.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\redmine-java-api-1.23.1-e491ee895bc5f5e09d8af0b4db6f552cda256ca5.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\slf4j-api-1.7.1-2329e6fbcf26dbe6aed9bd7c2820b4ceaae23de6.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\slf4j-api-1.7.1-b7cd7d60262580384ff9b076e0244ef18212f212.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\support-annotations-20.0.0-1514e8d1e5d6b1f7784080d9e0002766205e8fd5.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\support-annotations-20.0.0-d005872198788c93ef85f437377c5a4608557980.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\swscale-d2110a412161ef1a3b43b811bf48cb6ca2a64c61.jar C:\Mezzo\Koren\mezzo\SecuriHUB\build\intermediates\pre-dexed\debug\swscale-e5534023e6b3b892096f3ad4fd09226e3317701d.jar
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v4/BuildConfig;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
I have no idea how I fixed this, but I did.
I had to reinstall the sdk and the libraries again, and make sure that in the build.gradle it had minimum sdk of 11 or something.
But hey, who am I to complain... IT WORKS NOW WOO!

What is Android Pre-Dexing and how to does it increase performance?

I see ant script of Android has message that it is "pre-dexing". However dex and pre-dex uses same options, except that in one case one jar at a time, in other case multiple jars at a time.
What is the real need? Is pre-dex mandatory?
e.g
The ' characters around the executable and arguments are
not part of the command.
[dex] Pre-Dexing F:\users\tejasoft\work\fnd\cmn\tools\social\sz\demo\libs\pinit-sdk-1.0.jar -> pinit-sdk-1.0-e3bebafa2727504605edf3d8d85b5d46.jar
[dx] Current OS is Windows 7
[dx] Executing 'D:\Apps\java\embd\cmn\android\oems\google\adt\4.x\4.3.x\4.3\sdk\build-tools\18.1.0\dx.bat' with arguments:
[dx] '--dex'
[dx] '--output'
[dx] 'F:\users\tejasoft\work\fnd\cmn\tools\social\sz\demo\bin\dexedLibs\pinit-sdk-1.0-e3bebafa2727504605edf3d8d85b5d46.jar'
[dx] 'F:\users\tejasoft\work\fnd\cmn\tools\social\sz\demo\libs\pinit-sdk-1.0.jar'
[dx]
[dx] The ' characters around the executable and arguments are
[dx] not part of the command.
Execute:Java13CommandLauncher: Executing 'D:\Apps\java\embd\cmn\android\oems\google\adt\4.x\4.3.x\4.3\sdk\build-tools\18.1.0\dx.bat' with arguments:
'--dex'
'--output'
'F:\users\tejasoft\work\fnd\cmn\tools\social\sz\demo\bin\dexedLibs\pinit-sdk-1.0-e3bebafa2727504605edf3d8d85b5d46.jar'
'F:\users\tejasoft\work\fnd\cmn\tools\social\sz\demo\libs\pinit-sdk-1.0.jar'
The ' characters around the executable and arguments are
not part of the command.
[dex] Converting compiled files and external libraries into F:\users\tejasoft\work\fnd\cmn\tools\social\sz\demo\bin\classes.dex...
[dx] Current OS is Windows 7
[dx] Executing 'D:\Apps\java\embd\cmn\android\oems\google\adt\4.x\4.3.x\4.3\sdk\build-tools\18.1.0\dx.bat' with arguments:
[dx] '--dex'
[dx] '--output'
[dx] 'F:\users\tejasoft\work\fnd\cmn\tools\social\sz\demo\bin\classes.dex'
[dx] 'F:\users\tejasoft\work\fnd\cmn\tools\social\sz\demo\bin\classes'
[dx] 'F:\users\tejasoft\work\fnd\cmn\tools\social\sz\demo\bin\dexedLibs\classes-144740ee5cf8b90b747300d19fb8201e.jar'
[dx] 'F:\users\tejasoft\work\fnd\cmn\tools\social\sz\demo\bin\dexedLibs\classes-1593da1bb60c5ec741aca494963e04a3.jar'
[dx] 'F:\users\tejasoft\work\fnd\cmn\tools\social\sz\demo\bin\dexedLibs\classes-37bb5269e4fbd6dda9900fea95c0c29b.jar'
It's useful to remember in this case that you are working in Java, which is not exactly the same thing as the Dalvik runtime. Java, from Oracle, uses a completely different virtual machine, while Dalvik is specifically fine-tuned for mobile devices.
The DEX processes take your Java compiled JAR files and converts them into Dalvik Executable Files (.dex) files that will run on Android. The Oracle JVM uses different bytecode than the Dalvik runtime, making this step a necessity.
There is a process for libraries you might have included in your project, as they are also compiled with a standard Java compiler, and then another process for your actual project.
It's done in two steps as the final process, your application, requires access to already dexed library files to avoid having references to code change while the process is busy.
You can find out more at http://source.android.com/devices/tech/dalvik/index.html - where you will also see that Dalvik is scheduled for replacement with something called ART.

adding 'emma' to Android ant build triggers "local variable type mismatch" exception

I am trying to run Emma on the tests for an Android project that is a combination of Java and C/JNI code. The build and tests work fine, but whenever I add emma, I get a mysterious exception. I'm using the Android SDK v20.1 and NDK r8b.
The project is here, its an Android library project:
https://github.com/guardianproject/IOCipher
and the tests are here:
https://github.com/guardianproject/IOCipherTests
The build.xml file is generated using android update test-project. Running ant clean debug install test works everytime while ant clean emma debug install test triggers the exception:
here's the exception:
-dex:
[dex] Converting compiled files and external libraries into /var/lib/jenkins/workspace/IOCipherTests/IOCipherTests/bin/classes.dex...
[dx]
[dx] EXCEPTION FROM SIMULATION:
[dx] local variable type mismatch: attempt to set or access a value of type int using a local variable of type info.guardianproject.libcore.io.ErrnoException. This is symptomatic of .class transformation tools that ignore local variable information.
[dx]
[dx] ...at bytecode offset 0000002e
[dx] locals[0000]: Linfo/guardianproject/iocipher/File;
[dx] locals[0001]: Linfo/guardianproject/iocipher/FileDescriptor;
[dx] locals[0002]: <invalid>
[dx] locals[0003]: <invalid>
[dx] locals[0004]: <invalid>
[dx] locals[0005]: [Z
[dx] stack[top0]: int{0x00000001 / 1}
[dx] ...while working on block 002c
[dx] ...while working on method createNewFile:()Z
[dx] ...while processing createNewFile ()Z
[dx] ...while processing info/guardianproject/iocipher/File.class
[dx]
[dx] 1 error; aborting
BUILD FAILED
/opt/android-sdk/tools/ant/build.xml:850: The following error occurred while executing this line:
/opt/android-sdk/tools/ant/build.xml:852: The following error occurred while executing this line:
/opt/android-sdk/tools/ant/build.xml:864: The following error occurred while executing this line:
/opt/android-sdk/tools/ant/build.xml:266: null returned: 1
I was getting the same error. In my project we had a unit test app in one project, which wrapped another project containing the unit tests themselves. Both projects held references to my SDK which, as chaitanya suggested, was causing emma to instrument the SDK code twice. By removing the reference to the SDK in the unit test project I was able to resolve the error.
I build the Android APK project from the command line (without ant, but with some help of CMake. I guess this is not important here) and I met the same error, two things helped me:
From here, I got the info that "If you instrument class files that have local variable debug information in them emma does not correctly maintain the local variable table. This will cause an error when you try to convert the class files for Android. The workaround for this to compile the java classes with only line and source debug information, not local information.", so I add a flag to java compiler-g:{lines,source}
I also excluded Emma classes itself from instrumentation. My instrumentation command is java -cp emma/emma_device.jar emma instr -ix -*.test.*,-com.google.android.*,-com.vladium.* -m overwrite -cp ${CMAKE_JAVA_TARGET_OUTPUT_DIR}. Pay attention to -ix -com.vladium.* parameter, this excludes Emma classes

Failed to build Android app (refer to both ActionBarSherlock & ViewPagerTabs) with Ant

I have one Android app which uses ActionBarSherlock & ViewPagerTabs. I use Eclipse to write and build it, and it just works well until I try to build it with Ant. Here's what I did:
go to ActionBarSherlock folder, run "android update lib-project --path ."
go to ViewPagerTabs folder, run "android update lib-project --path ." too
go to app folder, run "android update project --path ."
run "and debug" under app folder, and I got following errors:
:
[javac] C:\Android\TestApp\src\com\test\App\TestActivity.java:46: cannot find symbol
[javac] symbol : method getSupportActionBar()
[javac] location: class com.test.App.TestActivity
[javac] final ActionBar ab = getSupportActionBar();
[javac] ^
So question NO. 1: I have correct library references in app's project.properties, and ActionBarSherlock & ViewPagerTabs could be built successfully, why do I still get these errors?
There's a workaround for this issue -- copy all classes.jar under library's bin folder into app's libs folder, and run "ant debug" again. But I need to delete these .jar files under app's libs folder after all .java files of app could be compiled.
Running "ant debug" again after this, I will get following errors:
[dx] processing archive C:\Android\ActionBarSherlock\library\bin\classes.jar...
[dx] ignored resource META-INF/MANIFEST.MF
[dx] processing android/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoStubImpl.class...
[dx] processing android/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl.class...
[dx] processing android/support/v4/accessibilityservice/AccessibilityServiceInfoCompat.class...
[dx] processing android/support/v4/accessibilityservice/AccessibilityServiceInfoCompatIcs.class...
[dx] processing android/support/v4/app/ActionBar$LayoutParams.class...
[dx] processing android/support/v4/app/ActionBar$OnMenuVisibilityListener.class...
[dx] processing android/support/v4/app/ActionBar$OnNavigationListener.class...
[dx] processing android/support/v4/app/ActionBar$Tab.class...
[dx] processing android/support/v4/app/ActionBar$TabListener.class...
[dx] processing android/support/v4/app/ActionBar.class...
[dx] processing android/support/v4/app/ActivityCompatHoneycomb.class...
[dx]
[dx] UNEXPECTED TOP-LEVEL EXCEPTION:
[dx] java.lang.IllegalArgumentException: already added: Landroid/support/v4/app/ActivityCompatHoneycomb;
[dx] at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
[dx] at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
[dx] at com.android.dx.command.dexer.Main.processClass(Main.java:486)
[dx] at com.android.dx.command.dexer.Main.processFileBytes(Main.java:455)
[dx] at com.android.dx.command.dexer.Main.access$400(Main.java:67)
[dx] at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:394)
[dx] at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
[dx] at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:131)
[dx] at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
[dx] at com.android.dx.command.dexer.Main.processOne(Main.java:418)
[dx] at com.android.dx.command.dexer.Main.processAllFiles(Main.java:329)
[dx] at com.android.dx.command.dexer.Main.run(Main.java:206)
[dx] at com.android.dx.command.dexer.Main.main(Main.java:174)
[dx] at com.android.dx.command.Main.main(Main.java:95)
[dx] 1 error; aborting
My question NO.2 is: how can I fix this issue?
Thanks!
I finally resolve it myself. I find that there's a android-support-v4.jar in ViewPagerTabs' libs folder. The solution is -- deleting this android-support-v4.jar, then make ViewPagerTabs depend on ActionBarSherlock (because ActionBarSherlock actually have one copy of Support Package).
I hope it's useful for people who wants to use ActionBarSherlock & ViewPagerTabs in one application and use Ant to built his/her application. Good luck.
You stated its because both library have android-support-v4.jar. See #porter-liu answer for non-maven builds.
For people using maven, set your ViewPager dependancy as:
<dependency>
<groupId>com.viewpagerindicator</groupId>
<artifactId>library</artifactId>
<version>2.4.1</version>
<type>apklib</type>
<exclusions>
<exclusion>
<groupId>com.google.android</groupId>
<artifactId>support-v4</artifactId>
</exclusion>
</exclusions>
</dependency>
The exclusion tag stops the double import fixing the issue.
When you update lib-project, you're updating the project so that it compiles as a library. There's a separate step to update your main project so that it sees the library it requires.
For each library project you're going to use, run
android update project --library ../pathToTheLibrary

Accessing common jar file from App as well as from Android Library Project

I have a static java library(classes.jar) file which has source code common to a Android Library Project and the dependent app which is calling this Android Library project.
I'm using ant env to compile the code on linux machine.
But when I include this jar file under libs folders of both ALP and App, it gives the following compilation error :
[dex] Converting compiled files and external libraries into classes.dex...
[dx]
[dx] UNEXPECTED TOP-LEVEL EXCEPTION:
[dx] java.lang.IllegalArgumentException: already added: Lcom/android/test/ProgramCache;
[dx] at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
[dx] at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
[dx] at com.android.dx.command.dexer.Main.processClass(Main.java:486)
[dx] at com.android.dx.command.dexer.Main.processFileBytes(Main.java:455)
[dx] at com.android.dx.command.dexer.Main.access$400(Main.java:67)
[dx] at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:394)
[dx] at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
[dx] at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:131)
[dx] at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
[dx] at com.android.dx.command.dexer.Main.processOne(Main.java:418)
[dx] at com.android.dx.command.dexer.Main.processAllFiles(Main.java:329)
[dx] at com.android.dx.command.dexer.Main.run(Main.java:206)
[dx] at com.android.dx.command.dexer.Main.main(Main.java:174)
[dx] at com.android.dx.command.Main.main(Main.java:95)
[dx] 1 error; aborting
How would I solve this problem of common jar?
Hopefully I won't mix up rules about building android projects in eclipse with building projects in ant with the android toolchain... (And with any luck, you won't have to do both like I do :))
Any jar files in the /libs folders of android shared libraries are automatically included by the command line android build tools. While I can't remember if they are copied on disk, or only added to the build path during compilation, it should suffice to simply remove the jar from App/libs.
Another word of advice when dealing with this problem, ant clean early and often. Its very easy to accidentally build some classes into one of your project jars and then have conflicts later.

Categories

Resources