UNEXPECTED TOP-LEVEL EXCEPTION in Android Studio - android

In one of my projects i am getting this Exception while building gradle file
Error:Execution failed for task ':emBazaarV4:dexDebug'.
com.android.ide.common.internal.LoggedErrorException: Failed to run command: F:\AndroidSDK\build-tools\21.1.2\dx.bat --dex
--no-optimize --output F:\AndroidStudioWorkspace\EmBazaarV4\emBazaarV4\build\intermediates\dex\debug
--input- list=F:\AndroidStudioWorkspace\EmBazaarV4\emBazaarV4\build\intermediates\tmp\dex\debug\inputList.txt
Error Code: 2 Output: UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define
Lcom/google/gson/JsonSerializer; at
com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189) at
com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303) at
com.android.dx.command.dexer.Main.run(Main.java:246) at
com.android.dx.command.dexer.Main.main(Main.java:215) at
com.android.dx.command.Main.main(Main.java:106)
and here is my dependencies
dependencies {
compile 'com.facebook.android:facebook-android-sdk:3.22.0'
compile 'com.aviary.android.feather.sdk:aviary-sdk:3.6.3'
compile 'com.android.support:support-v4:21.0.3'
compile 'com.google.android.gms:play-services:+'
compile 'com.squareup.okhttp:okhttp:2.2.0'
compile 'com.squareup.okio:okio:1.1.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.picasso:picasso:2.4.0'
compile files('libs/commons-io-2.4.jar')
compile files('libs/crashlytics.jar')
compile files('libs/httpmime-4.1.3.jar')
compile files('libs/jumblr-0.0.8-jar-with-dependencies.jar')
compile files('libs/signpost-commonshttp4-1.2.1.2.jar')
compile files('libs/signpost-core-1.2.1.2.jar')
compile files('libs/simplesocialsharing.jar')
compile files('libs/gcm.jar') }
I have tried everything but not getting how to exclude this multiple dex files. I have found solution for v4 like this
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
so is there any general solution for these kind of errors?

This error
UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files
happens when you are using different versions of the same library.
In you case it happens with the gson library:
Lcom/google/gson/JsonSerializer;
Check your dependencies. For example OkHttp uses the 2.2.3 version.
https://github.com/square/okhttp/blob/master/pom.xml#L44

As Gabriele says, TopLevel Exception always occur due to conflict of different version of same library.
First of all I would suggest, avoid using adding file dependency as much as possible for you, Always try to add dependency from the jcenter repository. Looks like gradle takes care of conflicting dependencies.
So now, There are some ways to avoid it:
Use only one version which contains all your required classes. like app compact has all v4 support classes, SO you dont need to import v4 support veresion if you're importing appcompact v7.
You can always exclude a package from the compiled dependencies. For Example:
//for the package:
dependencies {
compile("com.android.support:support-v4:21.0.3") {
exclude group: 'com.android.support', module: 'support-v4'
}
}

You are using the mastodontic Google Play Services in your app. Recently, you can get more granularity when using it.
Following this guide, you can use only parts that you want. Probably this will fix the problem. You can use it that way:
compile com.google.android.gms:play-services-base:6.5.87
compile <... some other google play services part ...>
You can read the entire list of "parts" in this link

Related

Android Studio - UNEXPECTED TOP-LEVEL EXCEPTION: on importing a eclipse project

I have imported a project in Android Studio. I am attempting to run the code but the app fails with the following errors, not sure what the problem is.
These are the libraries i am using.
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:+'
compile files('libs/android-async-http-1.4.6.jar')
compile files('libs/android-query-full.0.26.8.jar')
compile files('libs/android-viewbadger.jar')
compile files('libs/Android-WebRequest.jar')
compile files('libs/AndroidSwipeLayout-v1.1.6.jar')
compile files('libs/google-api-client-1.20.0.jar')
compile files('libs/google-api-services-bigquery-v2-rev200-1.20.0.jar')
compile files('libs/google-http-client-1.20.0.jar')
compile files('libs/google-http-client-jackson2-1.20.0.jar')
compile files('libs/google-oauth-client-1.20.0.jar')
compile files('libs/jackson-core-2.1.3.jar')
compile files('libs/libGoogleAnalyticsServices.jar')
compile files('libs/nineoldandroids-2.4.0.jar')
compile files('libs/Parse-1.9.2.jar')
compile files('libs/YouTubeAndroidPlayerApi.jar')
compile 'com.facebook.android:facebook-android-sdk:4.1.0'
compile files('libs/com-crashlytics-sdk-android_answers-classes.jar')
compile files('libs/com-crashlytics-sdk-android_beta-classes.jar')
compile files('libs/com-crashlytics-sdk-android_crashlytics-classes.jar')
compile files('libs/io-fabric-sdk-android_fabric-classes.jar')
and error i am getting is
Error:Execution failed for task ':app:dexDebug'.
com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Users\ch-e01062\AppData\Local\Android\android-sdk\build-tools\23.0.0-preview\dx.bat --dex --no-optimize --output C:\Users\ch-e01062\Downloads\FreeB2\app\build\intermediates\dex\debug --input-list=C:\Users\ch-e01062\Downloads\FreeB2\app\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/analytics/internal/Command$1;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:502)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
at com.android.dx.command.dexer.Main.run(Main.java:277)
at com.android.dx.command.dexer.Main.main(Main.java:245)
at com.android.dx.command.Main.main(Main.java:106)
i have tried many available solutions but nothing is working.Any help will be appreciated.
After hours with the same problem i found the answer.
The problem is that you have multiple libs with the same classes inside, so the solution would be to either remove the repeating files from one of the libs.
You have to:
Open the lib libGoogleAnalyticsServices.jar with WinRAR, 7Zip or whatever.
Go to com/google/android/gms/analytics/internal and delete the files Command$1.class and Command.class.
In your case you may have to remove other repeating files, not sure because you have many files. Either way, you can also force it to ignore some of those files IF the method above didn't work. Put the following code inside the Android block on your build.gradle file:
defaultConfig {
multiDexEnabled true
}
dexOptions {
preDexLibraries = false
}

:app:dexDebug on adding libGoogleAnalyticsServices

On trying to run my project, I get the following error:
Error:Execution failed for task ':app:dexDebug'.
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_67\bin\java.exe'' finished with non-zero exit value 2
I am adding the google analytics jar file from here. The issue goes away if I remove this line compile files('libs/libGoogleAnalyticsServices.jar') from the dependencies in the build.gradle
Is 1libGoogleAnalyticsServices.jar` interfering with something else I have in my dependencies?
dependencies {
//compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.facebook.android:facebook-android-sdk:4.1.1'
compile files('libs/nineoldandroids-2.4.0.jar')
compile files('libs/newrelic.android.jar')
compile files('libs/libGoogleAnalyticsServices.jar')
compile files('libs/YouTubeAndroidPlayerApi.jar')
compile 'com.google.android.gms:play-services:7.5.0'
compile('de.keyboardsurfer.android.widget:crouton:1.8.5#aar') {
}
compile ("com.doomonafireball.betterpickers:library:1.6.0")
compile ("com.doomonafireball.betterpickers:library:1.6.0") {
transitive = true
exclude group: 'com.android.support', module: 'support-v4'
}
compile project(':circularImageView')
}
This error could also indicate that you've hit the 65k method limit in Android.
If this is the case, then you have 3 options
Selectively use the parts of the play-services library that are required. This library itself will bring you close to the limit.
Let Progaurd minimize your application.
Use Multi-dex to split your dex index.
These guides will help you to achieve the options above;
http://googleadsdeveloper.blogspot.ie/2015/01/reducing-google-play-services-impact-on.html
https://developer.android.com/tools/building/multidex.html
Per the page you linked:
Important: This document describes a legacy version of the SDK. New users should use the latest SDK.
As the latest SDK is included in your 'com.google.android.gms:play-services:7.5.0' dependency, the two are overlapping and causing issues when building your application. You should instead follow the getting started guide for Google Analytics v4

Error resolving dependencies with Gradle, in Android project

I have configured the following dependencies in Gradle, inside Android Studio.
compile 'com.google.code.gson:gson:2.2.4'
compile files('libs/android-support-v13.jar')
compile files('libs/apache-mime4j-storage-0.7.2.jar')
compile files('libs/commons-cli-1.1.jar')
compile files('libs/commons-io-1.2.jar')
compile files('libs/httpclient-4.3.4.jar')
compile files('libs/httpcore-4.3.2.jar')
compile files('libs/httpmime-4.3.4.jar')
compile files('libs/rabbitmq-client.jar')
compile files('libs/retrofit-1.7.1.jar')
compile files('libs/universal-image-loader-1.9.3-with-sources.jar')
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
compile 'com.google.android.gms:play-services:7.0.0'
But Gradle gives me the following error:
Execution failed for task ':app:dexDebug'.
Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
D:\AS sdk\build-tools\21.1.2\dx.bat --dex --no-optimize --output C:\Users\ttuser\ASProjects\MyProject\app\build\intermediates\dex\debug --input-list=C:\Users\ttuser\ASProjects\MyProject\app\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
at com.android.dx.command.dexer.Main.run(Main.java:246)
at com.android.dx.command.dexer.Main.main(Main.java:215)
at com.android.dx.command.Main.main(Main.java:106)
How can I solve it?
app:dexDebug error in general means that you've tried to link in the duplicate class to your project more than once. So remove your Duplicate Library
I don't know if you figured it out yet, but seeing as all of those are publicly available libraries, I suggest getting rid of the JARs and getting them as a dependency from JCenter/Maven Central. If you are not sure how to do this, here is an example. One library you are using is Retrofit, so you would go to a website like Maven Central (http://search.maven.org/) and search for the library and get the Gradle dependency for your version (http://search.maven.org/#artifactdetails|com.squareup.retrofit|retrofit|1.7.1|jar):
compile 'com.squareup.retrofit:retrofit:1.7.1'
Do this for all of your dependencies. Once you have them set up as Gradle dependencies instead of JARs, you can do what is explained here: https://stackoverflow.com/a/21100040/2245921
Basically, go into your :app folder through a terminal, and run the following:
../gradlew -q dependencies
And Gradle will draw a dependency tree. There you will be able to see duplicate dependencies and more easily deal with them.

DexException: Multiple dex files

Okay so currently i am making an Android app that utilizes the Google Sheets API version 3.0 and Drive API Client Library for Java, i need the app to read a spreadhsheet from a users drive account and edit it. Following the documentation i have included the following jars in my /lib folder(Android Studio 1.1).dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
compile files('libs/google-api-client-1.19.1.jar')
compile files('libs/google-api-client-android-1.19.1.jar')
compile files('libs/google-api-services-drive-v2-rev158-1.19.1.jar')
compile files('libs/google-http-client-1.19.0.jar')
compile files('libs/google-http-client-android-1.19.0.jar')
compile files('libs/google-http-client-gson-1.19.0.jar')
compile files('libs/google-oauth-client-1.19.0.jar')
compile files('libs/gson-2.1.jar')
compile files('libs/jackson-core-2.1.3.jar')
compile files('libs/jackson-core-asl-1.9.11.jar')
compile files('libs/jsr305-1.3.9.jar')
compile files('libs/protobuf-java-2.4.1.jar')
compile files('libs/mail.jar')
compile files('libs/jsr305.jar')
compile files('libs/guava-11.0.2.jar')
compile files('libs/gdata-spreadsheet-meta-3.0.jar')
compile files('libs/gdata-spreadsheet-3.0.jar')
compile files('libs/gdata-docs-meta-3.0.jar')
compile files('libs/gdata-docs-3.0.jar')
compile files('libs/gdata-core-1.0.jar')
compile files('libs/gdata-client-meta-1.0.jar')
compile files('libs/gdata-client-1.0.jar')
compile files('libs/additionnal.jar')
compile files('libs/activation.jar')
}When im typing my code everything works just fine, everything is imported appropiately but when i run my app i get this result.
`What went wrong:
Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Users\Ricardo\AppData\Local\Android\sdk\build-tools\21.1.2\dx.bat --dex --output C:\Users\Ricardo\Google Drive\Development\Android\Updated\Private\Google_Fiasco\MyApplication\app\build\intermediates\dex\debug --input-list=C:\Users\Ricardo\Google Drive\Development\Android\Updated\Private\Google_Fiasco\MyApplication\app\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Ljavax/annotation/CheckForNull;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
at com.android.dx.command.dexer.Main.run(Main.java:246)
at com.android.dx.command.dexer.Main.main(Main.java:215)
at com.android.dx.command.Main.main(Main.java:106)
e`<br>
Now a couple things to note, when i make two separate projects ,one implementing the Spreadsheet API and the other the Drive API no errors occur. The error only occurs when i implement both API's in the same project i.e combining the jars from both libraries. After tinkering with the jars i found that the error only happens when both jsr305.jar & jsr305-1.3.9.jar are included. Looking at these two jars i see that both have CheckForNull.java files, the file referenced in the error message Multiple dex files define Ljavax/annotation/CheckForNull.
So my question: How do i resolve this error? I think i have to exclude it in my dependencies? Precise instructions would be appreciated, and perhaps an explanation on what the solution does. Also one last thing What difference does it make between simply putting jar files in my /lib folder and adding it to my dependencies in build.gradle? It doesn't seem to affect anything.
The result of deleting either of the jsr.jat files is the following: UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
at com.android.dx.merge.DexMerger$6.updateIndex(DexMerger.java:502)
at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:277)
at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:491)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:168)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
at com.android.dx.command.dexer.Main.run(Main.java:246)
at com.android.dx.command.dexer.Main.main(Main.java:215)
at com.android.dx.command.Main.main(Main.java:106)
Just clean the project and build again... I knew my code had nothing to do with the bug as soon as I saw the word 'dex' in the error text
This error is caused by having too many method referenced in your project. You can read more here.
Generally it means that you need to remove unused libraries or use proguard even on debug build to reduce the number of method referenced. You may also go with the multi-dex approached mentioned in the article but it makes your project more complicated.
I had react-native project with app's build.gradle:
def enableProguardInReleaseBuilds = false
...
buildTypes {
release {
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
Setting enableProguardInReleaseBuilds to true worked for me:
def enableProguardInReleaseBuilds = true

Android Studio: Multiple dex files define Lcom/sun/activation/registries/LineTokenizer;

Receiving this exception whilst trying to build my project. Have searched around for an answer but most cases seem to be different to mine.
Other solutions include clearing temporary files or doing a gradle clean. This does temporarily solve the issue but it reappears again after a few builds. Another way this issue can occur is if the project contains multiple copies of a library. I have searched through my project and only have one instance of activation.jar which is in my libs directory. Most other solutions seem to involve changing the build path in Eclipse, but my problem is occurring on Android Studio 0.5.8. I am using Java 1.7.
Gradle Console
UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException:
Multiple dex files define
Lcom/sun/activation/registries/LineTokenizer;
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)
Libraries in libs dir:
activation.jar
additionnal.jar
androidplot-core-0.5.1.jar
annotations.jar
DatawindAdsSdk-2.0.jar
jpct_ae.jar
jsr305-1.3.9.jar
libGoogleAnalyticsV2.jar
mail.jar
twitter4j-core-3.0.5.jar
Dependencies: Note - ":android-cropimage" does not have any dependencies in build.gradle
Main module:
dependencies {
//Library Projects
compile project(':android-cropimage')
compile project(':facebook')
//Android SDK Libraries
//This library requires "Google Play Services" and "Google Repository" to be downloaded via SDK Manager.
compile 'com.google.android.gms:play-services:4.4.52'
//Third Party
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abs:+'
compile 'com.jakewharton:butterknife:4.0.1'
compile 'com.j256.ormlite:ormlite-core:4.46'
compile 'com.j256.ormlite:ormlite-android:4.46'
compile 'net.hockeyapp.android:HockeySDK:3.0.1'
compile 'org.apache.httpcomponents:httpmime:4.2.5'
compile 'com.viewpagerindicator:library:2.4.1#aar'
compile 'com.squareup.picasso:picasso:2.2.0'
compile 'com.mcxiaoke.volley:library:1.0.+'
compile 'org.msgpack:msgpack:0.6.11'
}
Facebook module:
dependencies {
compile 'com.android.support:support-v4:19.1.0'
}
Turns out this is due to a bug with the Android Gradle plugin's incremental dex option on version 0.10.2 (https://groups.google.com/forum/#!topic/adt-dev/6KbhReCE_fo). Removing the following from my build.gradle file solved the issue:
android {
dexOptions {
incremental true
}
}
As #Marepork anwered there is a bug withing gradle that has not been fixed
If you still want to use incremental build you can always use build variants and fordebug use multidex and for release a proguard

Categories

Resources