UNEXPECTED TOP-LEVEL EXCEPTION when testing using Gradle - android

I'm pretty new to Gradle. My project is compiling file when I do a
./gradlew assembleDebug
But it doesn't when I try to run the tests:
./gradlew connectedCheck
I get (a couple of them):
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/facebook/AccessToken$SerializationProxyV1;
at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
at com.android.dx.command.dexer.Main.processClass(Main.java:490)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
at com.android.dx.command.dexer.Main.access$400(Main.java:67)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:131)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
at com.android.dx.command.dexer.Main.processOne(Main.java:422)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:333)
at com.android.dx.command.dexer.Main.run(Main.java:209)
at com.android.dx.command.dexer.Main.main(Main.java:174)
at com.android.dx.command.Main.main(Main.java:91)
here is the structure of my project:
main app depends on:
compile 'com.android.support:support-v4:18.0.+'
compile 'com.android.support:gridlayout-v7:18.0.+'
compile 'com.google.code.gson:gson:1.7.2'
compile 'com.jakewharton:butterknife:2.0.1'
compile 'com.squareup.retrofit:retrofit:1.1.1'
compile 'com.squareup:otto:1.3.4'
compile 'com.squareup.okhttp:okhttp:1.2.0'
compile project(':Library:mylib')
mylib depends on:
compile 'com.android.support:support-v4:18.0.+'
compile files('libs/facebooksdk.jar')
compile files('libs/libGoogleAnalyticsV2.jar')
the exception is coming for each jars from mylib, after this line:
:Library:mylib:dexTest
any idea?

The already added error occurs when a library is referenced more than once. What I'd do is set the compile project as the first compile element in your app and delete the repeated support-library:
app depends on:
compile project(':Library:mylib')
compile 'com.google.code.gson:gson:1.7.2'
compile 'com.jakewharton:butterknife:2.0.1'
compile 'com.squareup.retrofit:retrofit:1.1.1'
compile 'com.squareup:otto:1.3.4'
compile 'com.squareup.okhttp:okhttp:1.2.0'
lib depends on:
compile 'com.android.support:support-v4:18.0.+'
compile files('libs/facebooksdk.jar')
compile files('libs/libGoogleAnalyticsV2.jar')

I encountered the same error but the message behind already added: is slightly different. It's about duplicate support-v4. What I did to tackle is:
compile('de.keyboardsurfer.android.widget:crouton:1.8.1') {
exclude(group: 'com.google.android', module: 'support-v4')
}
Hope it show you a new direction to fix your issue.
UPDATE:
Sorry, I didn't read your question carefully. Turns out you got problem with the connectedCheck task. Mine is when running gradle build. Right now, when I run gradle connectedCheck, I just faced the same error. I even tried with the IDE but none of them worked.
There are actually a discussion about it here https://code.google.com/p/android/issues/detail?id=61429 too. Due to the comment #8, if you move to the app folder and run above task, you may manage. Assume that the app folder is app-project/app, the libs are put in app-projecct/libraries/my-lib-1. Previously, I run in app-project and failed.
Hope it fix.

Related

duplicated class caused by mulitedex dependecy in Android Studio

thank you for your support.
My issue is : When I added the multidex dependecy and try to export the signed apk, I get this error :
Error:Execution failed for task
':androidKeyboardThemes:transformClassesWithJarMergingForRelease'.
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/google/android/gms/location/ActivityRecognition$1.class
My gradle dependecies are theses:
dependencies {
compile project(':cropper')
compile 'com.google.android.gms:play-services:+'
compile files('libs/StartAppInApp-2.4.7.jar')
compile files('libs/google-play-services.jar')
compile 'com.android.support:multidex:1.0.1'
}
I tried to delete one lib or jar at any time and build again but I couldn't get the job done.
Also I read all the unswer about duplicated class in stackoverflow but I failed again to solve my issue.
Any ideas?, thank you for your help.
dependencies {
compile project(':cropper')
compile 'com.google.android.gms:play-services:+' <-- choose either one
compile files('libs/StartAppInApp-2.4.7.jar')
compile files('libs/google-play-services.jar') <-- choose either one
compile 'com.android.support:multidex:1.0.1'
}
Whatever it is, please dont use play-services. inside compile 'com.google.android.gms:play-services:+' contains alot of dependencies.. see below.. using play-services may cause dex problem and heavy app. refer here

Android Unable to execute dex error after cloning correct project

This is a really strange problem and I've been days stuck with it: we've got a working project (no errors whatsoever) in a git repository. Everything is OK (it's even published in Google Play) when executing the project from the creator's equip, but, when we try to clone it in a different equip (we've tried Mac, Linux and Windows), this exception is thrown:
Unable to execute dex: method ID not in [0, 0xffff]: 65536
Hereafter, even if we apply the multidex solution as described in Android Developers or delete the unused dependencies, the app raises an exception when trying to load class android.support.design.widget.NavigationView (coming from a NoClassDefFound in runtime).
I suspect maybe it has something to do with the gradle plugin or appcompat libs version, but so far, nothing we've tried has worked.
Edit: This is not a common multiDex error, I'm pretty familiarized about how to solve them, it's about one computer executing the app without problems but when you clone the same project from git in other computer, it won't run and the MultiDex error is thrown.
EDIT 2: mi dependenvy list is as follows (I ommit the company libs but they're not the problem
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:support-v4:23.0.1'
compile 'com.android.support:design:23.0.1'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.mo2o.third:valnif:1.0.0'
compile 'com.android.support:recyclerview-v7:23.0.+'
compile 'de.hdodenhof:circleimageview:2.0.0'
compile('com.afollestad.material-dialogs:core:0.8.0.1#aar') {
transitive = true
}
compile 'com.android.support:percent:23.0.0'
compile 'se.emilsjolander:stickylistheaders:2.6.0'
compile 'me.grantland:autofittextview:0.2.+'
compile 'com.google.android.gms:play-services-auth:8.3.0'
compile "com.google.android.gms:play-services-gcm:8.3.0"
compile 'com.google.android.gms:play-services-plus:8.3.0'
compile 'com.squareup.picasso:picasso:2.5.2'

packageAllDebugClassesForMultiDex - duplicate entry "Escaper.class"

How does this happen when I try to deploy? In what library is this class ?
Note: It compiles and builds just fine, but this error shows up only when I try to deploy.
:myProject:packageAllDebugClassesForMultiDex FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':myProject:packageAllDebugClassesForMultiDex'.
> java.util.zip.ZipException: duplicate entry: com/google/gdata/util/common/base/Escaper.class
These are the dependencies in my project, required by 4 different modules:
compile files('libs/gson-1.7.1.jar')
compile files('libs/httpmime-4.1.1.jar')
compile files('libs/icu4j-4_8_1_1.jar')
compile files('libs/signpost-core-1.2.1.1.jar')
compile files('libs/twitter4j-core-3.0.5.jar')
compile files('libs/comscore.jar')
compile files('libs/javabase64-1.3.1.jar')
compile files('libs/libGoogleAnalytics.jar')
compile files('libs/urbanairship-lib-2.1.2.jar')
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:support-v4:23.0.1'
Just like Mamata Gelanee said in the comments, the solution is to use maven repos instead of .jar's in /libs.
I've gotten rid of all jar's in my /libs folder and replaced it with maven repos and now it all builds and deploys fine.

UNEXPECTED TOP-LEVEL EXCEPTION in Android Studio

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

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