Java finished with non-zero exit value 2 - Android Gradle - android

I'm getting this error executing my Android app (I cleaned it and then built it, but the error is still present)
Sync: OK
Make Project: OK
Clean: OK
Run: 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_25\bin\java.exe'' finished with non-zero exit value 2
My gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.rzr.rzevallosr.miappdepruebas"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// This library handles authentication and authorization
compile 'com.spotify.sdk:spotify-auth:1.0.0-beta9#aar'
// This library handles music playback
compile 'com.spotify.sdk:spotify-player:1.0.0-beta9#aar'
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:recyclerview-v7:21.0.+'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
compile 'com.squareup.okhttp:okhttp:2.2.0'
compile files('libs/spotify-web-api-android-master-0.1.0.jar')
compile files('libs/okio-1.3.0.jar')
}
EDIT: I didn't see "compile fileTree(dir: 'libs', include: ['*.jar'])" it was compiling twice my libraries, so i just comment:
//compile 'com.squareup.retrofit:retrofit:1.9.0'
//compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
//compile 'com.squareup.okhttp:okhttp:2.2.0'
//compile files('libs/spotify-web-api-android-master-0.1.0.jar')
//compile files('libs/okio-1.3.0.jar')
and it works fine.

This issue is quite possibly due to exceeding the 65K methods dex limit imposed by Android. This problem can be solved either by cleaning the project, and removing some unused libraries and methods from dependencies in build.gradle, OR by adding multidex support.
So, If you have to keep libraries and methods, then you can enable multi dex support by declaring it in the gradle config.
defaultConfig {
// Enabling multidex support.
multiDexEnabled true
}
You can read more about multidex support and developing apps with more than 65K methods here.

For me the problem was, i had put a unnecessary complie library code in build.gradle
dependencies {
compile 'com.google.android.gms:play-services:7.5.0'
}
which was causing over 65k methods, so removed it,gradle sync, cleaned project, and then ran again and then this error stopped.
I needed just maps and gcm so i put these lines and synced project
compile 'com.google.android.gms:play-services-gcm:7.5.0'
compile 'com.google.android.gms:play-services-location:7.5.0'
Hi people i again encountered this problem and this time it was because of changing build tools version and it really required me to enable multidex..so i added these my app's build.gradle file..
defaultConfig {
applicationId "com.am.android"
minSdkVersion 13
targetSdkVersion 23
// Enabling multidex support.
multiDexEnabled true
}
dexOptions {
incremental true
javaMaxHeapSize "2048M"
jumboMode = true
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:multidex:1.0.1'
}
And create a class that extends Application class and include this method inside the class..
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
also include in OnCreate method too
#Override
public void onCreate() {
MultiDex.install(this);
super.onCreate();
}

Just in case if someone still struggling with this and have no clue why is this happening and how to fix. In fact this 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\jdkx.x.x_xx\bin\java.exe'' finished with non-zero exit value 2
can have many reasons to happen but certainly not something related to your JDK version so don't wast your time in wrong direction.
These are two main reasons for this to happen
You have same library or jar file included several places and some of them conflicting with each other.
You are about to or already exceeded 65k method limit
First case can be fixed as follows:
Find out which dependencies you have included multiple times. In order to do this run following command in android studio terminal
gradlew -q dependencies yourProjectName_usually_app:dependencies --configuration compile
this will return all the dependencies but jar files that you include from lib folder
try to get rid of duplication marked with asterisk (*), this is not always possible but in some cases you still can do it, after this try to exclude modules that are included many times, you can do it like this
compile ('com.facebook.android:facebook-android-sdk:4.0.1'){
exclude module: 'support-v4'
}
For the second case when you exceeding method limit suggestion is to try to minimize it by removing included libraries (note sounds like first solution) if no way to do it add multiDexEnabled true to your defaultConfig
defaultConfig {
...
...
multiDexEnabled true
}
this increases method limit but it is not the best thing to do because of possible performance issues
IMPORTANT adding only multiDexEnabled true to defaultConfig is not enough in fact on all devices running android <5 Lollipop it will result in unexpected behavior and NoClassDefFoundError. how to solve it is described here

I didn't know (by then) that "compile fileTree(dir: 'libs', include: ['*.jar'])" compile all that has jar extension on libs folder, so i just comment (or delete) this lines:
//compile 'com.squareup.retrofit:retrofit:1.9.0'
//compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
//compile 'com.squareup.okhttp:okhttp:2.2.0'
//compile files('libs/spotify-web-api-android-master-0.1.0.jar')
//compile files('libs/okio-1.3.0.jar')
and it works fine. Thanks anyway! My bad.

I had the same issue and I fixed removing the library that were unnecessary
compile fileTree(dir: 'libs', include: ['*.jar'])
I removed that library and I could run the project without any problem.

Mine got solved by enabling multiDex for debug builds.
defaultConfig {
multiDexEnabled true
}

I Reject Embedded JDK ( in 32bit ) because embedded JDK is 64bit
Right click your Project -> Open Module Setting -> SDK Location -> Uncheck Use embedded JDk then set your JDK Path, eg in Ubuntu /usr/lib/jvm/java-8-openjdk-i386

Possible problem: You have exceeded dex 65k methods limit, may be you added some library or several methods before problem occurred?

If You have already updated your SDK and You also using google-play-services then you need to take care of the dependency because there are some kind of conflics with :
Follow the below : compile 'com.google.android.gms:play-services:+' Replace by compile 'com.google.android.gms:play-services:6.5.87'
Note: Here '6.5.87' is the google-play-service version. I hope it will help..

I think it's conflicts of .jar file in your project.
I have removed android-support-v4.jar from libs folder and its working !!!
if your project gives error, check in your build.gradle file
dependencies {
}

In my case, the problem was that the new library (gradle dependency) that I had added was relying on some other dependencies and two of those underlying dependencies were conflicting/clashing with some dependencies of other libraries/dependencies in my build script. Specifically, I added Apache Commons Validator (for email validation), and two of its dependencies (Apache Commons Logging and Apache Commons Collections) were conflicting with those used by Robolectric (because different versions of same libraries were present in my build path). So I excluded those conflicting versions of dependencies (modules) when adding the new dependency (the Validator):
compile ('commons-validator:commons-validator:1.4.1') {
exclude module: 'commons-logging'
exclude module: 'commons-collections'
}
You can see the dependencies of your gradle module(s) (you might have only one module in your project) using the following gradle command (I use the gradle wrapper that gets created for you if you have created your project in Android Studio/Intellij Idea). Run this command in your project's root directory:
./gradlew :YOUR-MODULE-NAME:dependencies
After adding those exclude directives and retrying to run, I got a duplicate file error for NOTICE.txtthat is used by some apache commons libraries like Logging and Collections. I had to exclude that text file when packaging. In my build.gradle, I added:
packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/notice.txt'
exclude 'META-INF/NOTICE.txt'
}
It turned out that the new library (the Validator) can work with slightly older versions of its dependencies/modules (which are already imported into my build path by another library (Robolectric)). This was the case with this specific library, but other libraries might be using the latest API of the underlying dependencies (in which case you have to try to see if the other libraries that rely on the conflicting module/dependency are able to work with the newer version (by excluding the older version of the module/dependecy under those libraries's entries)).

In my case, I got this error when there are 2 or more libraries conflict (same library but different versions). Check your app build.gradle in dependencies block.

There are two alternatives that'll work for sure:
Clean your project and then build.
If the above method didn't worked, try the next.
Add the following to build.gradle file at app level
defaultConfig {
multiDexEnabled true
}

Updating my Java SDK to the latest version 1.7.0_79 and updating the SDK Location under Project Structure solved the problem for me.

My problem was that apart from having
com.android.build.api.transform.TransformException:
com.android.ide.common.process.ProcessException:
org.gradle.process.internal.ExecException: Process 'command
'/Library/Java/JavaVirtualMachines/jdk1.X.X_XX.jdk/Contents/Home/bin/java''
finished with non-zero exit value 2
I had this trace as well:
Uncaught translation error: java.lang.IllegalArgumentException:
already added: Lcom/mypackage/ClassX;
The problem was that I was adding the same class in two differents libraries. Removing the class/jar file from one of the libraries, the project run properly

For me i was adding the whole playstore dependencies
compile 'com.google.android.gms:play-services:8.4.0'
But i needed google map only , so i made it more specific and the error was resolved.
compile 'com.google.android.gms:play-services-maps:8.4.0'

in my case problem was build tools version which was 23.0.0 rc3 and i changed to 22.0.1 and my problem fixed.

You may be using a low quality cable/defected cable to connect your device to the PC, I replaced the cable and it worked for me.

This error is because of using more number of libraries.in my case 'compile 'com.google.android.gms:play-services-9.4.0' caused this error.To avoid this error use necessary libraries.for example if you want to use Maps in your app.Then use
compile 'com.google.android.gms:play-services-maps:9.4.0'.while adding google play services dependency specify the needed libraries only.by default it includes all the libraries.

In Your gradle.build file, Use this
"compile fileTree(dir: 'libs', include: ['*.jar'])"
And it works fine.

If you want to see what exactly is causing the error, try building your project via gradle terminal, like this ./gradlew assembleDebug --stacktrace --debug . I've seen the following error in my case com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
This happened when I was trying to entegrate Google Maps to my project. To fix it, I simply checked libraries I was including
compile 'com.google.android.gms:play-services:9.8.0'
I simply changed it to
compile 'com.google.android.gms:play-services-maps:9.8.0'
and problem was gone

I had the same error after converting my project to Kotlin. My problem was that my jre location was changed to an invalid path during the process (I wonder why this could happen... made me waste time). Fixed it by doing this:
File > Project Structure > SDK Location
Unchecked the Use embedded JDK option, which was pointing to an old JDK installation, and selected the correct one:
/home/my_user/jdk1.8.0_101
After changing this, the error disappeared

WORKED FOR ME :)
i upgraded the java to the latest version 8 previously it was 7 and then go to OPEN MODULE SETTING right clicking on project and changed the jdk path to /usr/lib/jvm/java-8-oracle the new java 8 installed. And restart the studio
check in /usr/lib/jvm for java 8 folder name
I am using ubuntu

Related

Migrate Eclipse ADT project to Android Studio: Error Multiple dex files define Landroid/support/v4

I have an old Android project made with Eclipse ADT and I'm trying to migrate it to Android Studio.
I follow several guides and howtos and maybe I have almost migrated it.
When I try to build it I get the following error:
Error:Error converting bytecode to dex:Cause: com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
I have read a lot of solutions but none seems to suit to my problem.
But maybe this answer is for my case...
Here is the Project status in Eclipse project explorer:
and here is the migrated Project in Android Studio project view:
Maybe the problem is the dependencies property of every gradle script:
app-app:
dependencies {
compile project(':androidsupportv7appcompat')
compile project(':library')
compile files('libs/android-support-v4.jar')
}
androidsupportv7appcompat:
dependencies {
compile files('libs/android-support-v4.jar')
compile files('libs/android-support-v7-appcompat.jar')
}
googleplayservice_lib:
dependencies {
compile files('libs/google-play-services.jar')
}
library: (a Google Maps library for markers clustering)
dependencies {
compile project(':googleplayservices_lib')
compile files('libs/android-support-v4.jar')
}
Do you know if something could be wrong in these dependencies configurations?
Otherwise, what could be the cause?
First of all, you should remove the Support Library module from your Android Studio project. Instead add a dependency to your apps gradle build file. If your library uses components from the support library, its build file should have the proper dependencies also.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
}
Be sure to remove the Support Library JAR files from your lib's folder.
Similarly you should add a dependency for Google Play Services.
Dependency management with Gradle is one of the many advantages of moving to Android Studio. If the library module is not written by you, you should also find the correct take dependency to add to your build file instead of the module you have now.
in one of my old projects, I had to put this in the gradle file under app section
multiDexEnabled true in the default config and then add the dex options below... maybe this will help.
defaultConfig {
applicationId "xxx"
minSdkVersion 14
targetSdkVersion 24
versionCode 4
versionName "2.2"
multiDexEnabled true
}
dexOptions {
javaMaxHeapSize "2g"
}

Error:Execution failed for task ':app:transformClassesWithDexForDebug' in android studio

I am moving my projects from eclipse to Android Studio. While running one of my app I get the following error. Not able to find any solution. I have enabled multidex as well.
Error:Execution failed for task
':app:transformClassesWithDexForDebug'.
com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_79\bin\java.exe'' finished with non-zero exit value 1
also my gradle file is below
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.myapp"
minSdkVersion 9
targetSdkVersion 21
multiDexEnabled true
}buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard- android.txt'), 'proguard-project.txt', 'proguard-google-api-client.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v4:23.4.0'
compile 'com.google.android.gms:play-services:+'
compile 'com.google.http-client:google-http-client-gson:1.20.0'
compile 'com.google.code.gson:gson:2.1'
compile 'com.android.support:appcompat-v7:23.4.0'
compile files('libs/activation.jar')
compile files('libs/additionnal.jar')
compile files('libs/android-support-v7-cardview.jar')
compile files('libs/android-support-v7-recyclerview.jar')
compile files('libs/commons-logging-1.1.1.jar')
compile 'com.google.api-client:google-api-client:1.18.0-rc'
compile 'com.google.api-client:google-api-client-android:1.18.0-rc'
compile 'com.google.api-client:google-api-client-appengine:1.18.0-rc'
compile 'com.google.http-client:google-http-client:1.18.0-rc'
compile 'com.google.http-client:google-http-client-android:1.18.0-rc'
compile 'com.google.http-client:google-http-client-appengine:1.18.0-rc'
compile 'com.google.oauth-client:google-oauth-client:1.18.0-rc'
compile 'com.google.oauth-client:google-oauth-client-appengine:1.18.0-rc'
compile files('libs/google-api-client-gson-1.18.0-rc.jar')
compile files('libs/google-api-client-jackson2-1.18.0-rc.jar')
compile files('libs/google-api-client-java6-1.18.0-rc.jar')
compile files('libs/google-api-client-servlet-1.18.0-rc.jar')
compile files('libs/google-http-client-jackson2-1.18.0-rc.jar')
compile files('libs/google-http-client-jdo-1.18.0-rc.jar')
compile files('libs/google-oauth-client-java6-1.18.0-rc.jar')
compile files('libs/google-oauth-client-jetty-1.18.0-rc.jar')
compile files('libs/google-oauth-client-servlet-1.18.0-rc.jar')
compile files('libs/GraphView-4.0.1.jar')
compile files('libs/httpclient-4.0.1.jar')
compile files('libs/httpcore-4.0.1.jar')
compile files('libs/jackson-core-2.1.3.jar')
compile files('libs/jetty-6.1.26.jar')
compile files('libs/jetty-util-6.1.26.jar')
compile files('libs/jsr305-1.3.9.jar')
compile files('libs/jxl.jar')
compile files('libs/mail.jar')
compile files('libs/transaction-api-1.1.jar')
}
Error:Execution failed for task
':app:transformClassesWithDexForDebug'.
com.android.build.api.transform.TransformException:
java.lang.RuntimeException:
com.android.ide.common.process.ProcessException:
java.util.concurrent.ExecutionException:
com.android.ide.common.process.ProcessException:
org.gradle.process.internal.ExecException: Process 'command
'C:\Program Files\Java\jdk1.7.0_79\bin\java.exe'' finished with
non-zero exit value 1
The upper error occure due to lot of reason. So I can put why this error occure and how to solve it.
REASON 1 : Duplicate of class file name
SOLUTION :
when your refactoring of some of your class files to a library project. and that time you write name of class file So, double check that you do not have any duplicate names
REASON 2 : When you have lot of cache Memory
SOLUTION :
Sometime if you have lot of cache memory then this error occure so solve it.
go to File/Invalidate caches / Restart then select Invalidate and Restart it will clean your cache memory.
REASON 3 : When there is internal bug or used beta Version to Switch back to stable version.
SOLUTION :
Solution is just simple go to Build menu and click Clean Project and after cleaning click Rebuild Project.
REASON 4 : When you memory of the system Configuration is low.
SOLUTION :
open Task Manager and stop the other application which are not most used at that time so it will free the space and solve OutOfMemory.
REASON 5 : The problem is your method count has exceed from 65K.
SOLUTION :
open your Project build.gradle file add
defaultConfig {
...
multiDexEnabled true
}
and in dependencies add below line.
dependencies
{
compile 'com.android.support:multidex:1.0.0'
}
**In my case problem solved with Instant Run DISABLE **
Check Whether multidex enabled or not in your build.gradle(app level) under dependencies.if not place like below
dependecies{
multidexEnabled true
}
Check your gradle.properties(app level).if you see the below code
#org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
remove # before the line ,then it should be like this
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
Please
Add this into your gradle file
android {
...
defaultConfig {
...
multiDexEnabled true
}
}
AND also add the below dependency in your gradle
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
OR another option would be: In your manifest file add the MultiDexApplication package from the multidex support library in the application tag.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.multidex.myapplication">
<application
...
android:name="android.support.multidex.MultiDexApplication">
...
</application>
</manifest>
This can be because of following reason:
one of the jar files inside project was using an older version of google play services.
use multiDexEnabled true in defaultconfig
Be specific with classes you add in dependencies. like
compile 'com.google.android.gms:play-services-maps:8.4.0'
Not like compile 'com.google.android.gms:play-services:+'
I tried everything but still this error won't go, until I changed my JDK version.
My JDK version was 7, and after I changed it to 8, the error went away, you can try it if nothing else works.
i have tried this one and it worked for me.
hope it works for others too.
Open build.gradle module file
downgrade you sdkversion from 25 to 24 or 23
add 'multiDexEnabled true'
comment this line : compile 'com.android.support:appcompat-v7:25.1.0' (if it's in 'dependencies')
Sync your project
and ready to go.
Here i have attached screenshot to explain things.
Go through this steps
happy to help.
thanks.
Or you can try this -
Extend your Application class with MultiDexApplication instead of Application !
Duplicate name Classes
like
class BackGroundTask extends AsyncTask<String, Void, Void> {
and
class BackgroundTask extends AsyncTask<String, Void, Void> {
i have tried this one for more projects and it worked for me. note that solution can configure your app for multidex:
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
...
minSdkVersion 9
targetSdkVersion 25
}
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}}
dependencies {
compile 'com.android.support:support-v4:25.1.0'
compile 'com.google.android.gms:play-services:8.3.0'
...
}
Thank #Ironman for his complete answer, however I should add my solution according to what I've experienced facing this issue.
In build.gradle (Module: app):
compile 'com.android.support:multidex:1.0.1'
...
dexOptions {
javaMaxHeapSize "4g"
}
...
defaultConfig {
multiDexEnabled true
}
Also, put the following in gradle.properties file:
org.gradle.jvmargs=-Xmx4096m -XX\:MaxPermSize\=512m -XX\:+HeapDumpOnOutOfMemoryError -Dfile.encoding\=UTF-8
I should mention, these numbers are for my laptop config (MacBook Pro with 16 GB RAM) therefore please edit them as your config.
A temporary solution is to place the code below in the module build.gradle :
android {
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
}
And Sync the Project.
My Settings Screenshot:
Thanks to this post.
Just turn off instant run in your settings. You can easily search the keyword "instant" like I showed and it would switch to the window you want.
In my case I had 2 projects A and B. And I upgraded to gradle 4.5.
A was dependent on B but both had references of my 3rd party jar
I was getting this error
com.android.tools.r8.errors.CompilationError: Program type already
present: com.mnox.webservice.globals.WebServiceLightErrorHashCode
Program type already present: com.mnox.webservice.globals.WebServiceLightErrorHashCode
To fix it
I removed the duplicate jar's
I used api in the B build.gradle file so that it gets referred to in A.
The other root cause can be if you have upgraded to gradle 4.5 and used implementation instead of api in your commons build.gradle
Just worth mentioning that while others suggest tempering with files, I was able to resolve this issue by installing a missing plugin (ionic framework)
Hopefully it helps someone.
cordova plugin add cordova-support-google-services --save
For me the solution was to delete the gradle cache folder (.gradle/caches) in the home directory and build again. Possibly due to a duplicate class loading issue.
For Ionic 4 Just
$ cordova clean
Helped me then run
$ ionic cordova run android --device

Build Failed when i try to run the app

I have the error like
"Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/google/android/gms/internal/zzpm$zza$zza.class"
What should i do to remove this error
gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.user.merchant"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
compile 'com.android.support:support-v4:23.2.0'
compile 'com.android.volley:volley:1.0.0'
compile 'com.google.android.gms:play-services-appindexing:8.4.0'
compile 'com.google.android.gms:play-services-gcm:8.4.0'
//compile files('libs/google-play-services.jar')
}
As #Vaiden says the issue was:
"It seems that you have at least one library already bundled with GMS"
Every time that you face a duplicate entry error run ./gradlew app:dependencies and make sure that there is no duplicated versions of a same module. For example: in my case I had:
compile 'com.google.android.gms:play-services-gcm:9.0.0'
When I tried to use firebase in my project I add it like:
compile 'com.google.firebase:firebase-core:9.+'
compile 'com.google.firebase:firebase-messaging:9.+'
That makes my app crash cause when I navigate to the: app > build > intermediates > exploded-aar > com.google.android.gms one of the folders (play-services-basement) pull a version 9.4.0 instead 9.0.0 so what I did was to unificate the versions by adding firebase like:
compile 'com.google.firebase:firebase-core:9.0.0'
compile 'com.google.firebase:firebase-messaging:9.0.0'
In theory you can solve this too by pushing the firebase to 9.4.0 instead.
The bottom line is to try to know which things are your dependencies introducing to the app and check "WHO" is introducing the same thing to the app, once you get there try to set a specific version of that duplicated dependency.
Based from this forum, try to change the gradle inside your C:\Program Files\Android\Android Studio\gradle\ to gradle-2.10. Also, it seems unlikely that a program that is designed to run on an Android device doesn't need all of the jars. Try removing some of them.
Check this related SO questions:
TransformException duplicate entry for common.annotations.Beta
Android dependency issue with gms play services
It seems that you have at least one library already bundled with GMS.
None of the dependencies stated in your gradle build file should have it, so the culprit might be a library jar file in your libs dir. So either you have something in your libs dir you shouldn't have put there, or a library that has GMS bundled inside of it.
Did you remember to delete libs/google-play-services.jar?

Android Studio - UNEXPECTED TOP-LEVEL EXCEPTION:

I have built a new project in Android Studio using the new project templates provided as part of the tool. All of the code has been generated by Studio I have not made any amendments yet.
I am attempting to run the code but the app fails with the following errors, not sure what the problem is so any help appreciated.
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v7/app/ActionBar$Callback;
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)
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Users\RichardKavanagh\AppData\Local\Android\android-sdk\build-tools\19.0.1\dx.bat --dex --output D:\Android\Projects\MyHealthRecord\app\build\libs\app-debug.dex D:\Android\Projects\MyHealthRecord\app\build\classes\debug D:\Android\Projects\MyHealthRecord\app\build\dependency-cache\debug D:\Android\Projects\MyHealthRecord\app\build\pre-dexed\debug\android-support-v7-appcompat-5a78dab7e2789bbe64f4bc80d106ca75c04dcf6f.jar D:\Android\Projects\MyHealthRecord\app\build\pre-dexed\debug\classes-f9b947272e9f33ba50355b52d82755584f9c0c58.jar D:\Android\Projects\MyHealthRecord\app\build\pre-dexed\debug\support-v4-19.0.0-31a2c13df80d37d62ca50fec3bde6da0ca706223.jar
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v7/app/ActionBar$Callback;
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)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 12.948 secs
Like everyone else said here, the support library (com.android.support) is being included more than once in your project. Try adding this in your build.gradle at the root level and it should exclude the support library from being exported via other project dependencies.
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
If you have more then one support libs included in the dependencies like this, you may want to remove one of them:
dependencies {
compile 'com.android.support:support-v4:19.1.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
This gets conflicted if you have the support jar in your libs folder.
If you have the support jar in your project libs folder and you have the module dependency added to compile 'com.android.support:support-v4:13.0.+' the UNEXPECTED_TOPLEVEL_DEPENDANCY exception will be thrown.
Because you may include two same libs in your project.
check your build.gradle file.
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile files('libs/android-support-v4.jar')
}
if your file includes compile 'com.android.support:appcompat-v7:+' and compile files('libs/android-support-v4.jar'), it will have this problems.
delete this sentence: compile files('libs/android-support-v4.jar')
That's how I fix this problem.
The error occurs when you have the same library/directory included more than once in your build.gradle's dependencies. Ok, let’s say you have an App structure that looks like this:
So you have the main “app” and then you have a bunch of sub-apps/modules/libraries. The libraries are: 1) gene_test_library, 2) genes_nine_old_androids_library, & 3) swipe_list_view_library.
My name is Gene, so that’s why there are all these “gene” libraries.
Inside the build.gradle for “app”, I have:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.0'
compile project(':libraries:gene_test_library')
//compile project(':libraries:genes_nine_old_androids_library')
compile project(':libraries:swipe_list_view_library')
}
Inside the build.gradle for gene_test_library, I have nothing:
dependencies {
}
Inside build.gradle for gene_nine_old_androids_library, I have:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.0'
}
Inside build.gradle for swipe_list_view_library, I have:
dependencies {
compile 'com.nineoldandroids:library:2.4.0+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.0'
}
This line of code “compile fileTree(dir: 'libs', include: ['*.jar'])” just means “hey, look inside the ‘libs’ folder inside this module for any jar files. I do not have anything in the libs folder of any of the modules so you can ignore that line of code.
So let’s say I uncomment out //compile project(':libraries:genes_nine_old_androids_library')
In the build.gradle for the “app” module. Then I would get the “UNEXPECTED TOP-LEVEL EXCEPTION:” error. Why is that?
Well, writing //compile project(':libraries:genes_nine_old_androids_library') inside the build.gradle for “app”, is the same as taking the build dependencies of “genes_nine_old_androids_library” module and putting it there. So uncommenting the //compile project(':libraries:genes_nine_old_androids_library') statement, the build.gradle for “app” module becomes:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.0'
compile project(':libraries:gene_test_library')
***compile fileTree(dir: 'libs', include: ['*.jar'])***
***compile 'com.android.support:appcompat-v7:21.0.0'***
compile project(':libraries:swipe_list_view_library')
}
Notice how now “compile 'com.android.support:appcompat-v7:21.0.0'” shows up 2x. That’s where the error is coming from.
I found 2 reason for this issue:
Sometimes its because of multiple included libraries. For example you add
compile 'com.nineoldandroids:library:2.4.0'
in your gradle and add another library that it also use "nineoldandroids" in it's gradle!
As Android Developer Official website said:
If you have built an Android app and received this error, then congratulations, you have a lot of code!
So, why?
The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536, including Android framework methods, library methods, and methods in your own code. Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration.
Then what should you do?
Avoiding the 65K Limit - How?
Review your app's direct and transitive dependencies - Ensure any large library dependency you include in your app is used in a manner that outweighs the amount of code being added to the application. A common anti-pattern is to include a very large library because a few utility methods were useful. Reducing your app code dependencies can often help you avoid the dex reference limit.
Remove unused code with ProGuard - Configure the ProGuard settings for your app to run ProGuard and ensure you have shrinking enabled for release builds. Enabling shrinking ensures you are not shipping unused code with your APKs.
Configuring Your App for Multidex with Gradle - How?
1.Change your Gradle build configuration to enable multidex.
put
multiDexEnabled true
in the defaultConfig, buildType, or productFlavor sections of your Gradle build file.
2.In your manifest add the MultiDexApplication class from the multidex support library to the application element.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.multidex.myapplication">
<application
...
android:name="android.support.multidex.MultiDexApplication">
...
</application>
</manifest>
Note: If your app uses extends the Application class, you can override the attachBaseContext() method and call MultiDex.install(this) to enable multidex. For more information, see the MultiDexApplication reference documentation.
Also this code may help you:
dexOptions {
javaMaxHeapSize "4g"
}
Put in your gradle(android{ ... } ).
Hi all I had the same issue that was being caused by a duplicate support version 4 file that I had included while trying to get parse integrated. Deleted the extra inclusion from the libs directory and it works fine now!
This happens when a library is getting compiled twice (i.e it is added two time). It can be support library or any other, it doesn't matter.
The common case is that you have added a compile statement of a library which is already in your libs/ directory. All the *.jar files are compiled automatically. Thus, adding a compile statement is causing the error. Removing that statement might fix this issue. If this is not applicable then we already have some awesome answers.
This might be the dumbest answer, but this worked for me :
I removed and added all the libraries on my project manually.(One after the other) And voila, it worked.
Build -> Rebuild project
Note: No library of mine was compiled twice.
Make sure you have downloaded Support Repository to use support library dependency in build.gradle.
If these all are there already installed sync your project with gradle once using the button available.
In my case TOP LEVEL EXCEPTION was throw because of a special char in project path. Just closed the project, changed "á" to "a" and reopened the project. Works!
Suddenly, without any major change in my project, I too got this error.
All the above did not work for me, since I needed both the support libs V4 and V7.
At the end, because 2 hours ago the project compiled with no problems, I simply told Android Studio to REBUILD the project, and the error was gone.
I had similar problem when I tried to build a signed apk for my app.
Strange, it happened only when I wanted to build a release apk, while on debug apk everything worked OK.
Finally, looking on this thread, I checked for support library duplications in build.gradle and removed any duplications but this wasn't enough..
I had to do clean project and only then finally I've got it to work.
I know that the problem was answered, but this could happen again and my solution was a little different from the ones that I found. In my case the solution wasn't related to include two different libraries in my project. See code below:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
This code was giving that error "Unexpected Top-Level Exception".
I fix the code making the following changes:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
I solved my problem with adding these in build gradle:
defaultConfig {
multiDexEnabled true
dependencies {
compile 'com.android.support:multidex:1.0.0'
another solution can be removing unnecessary libraries

Android Studio: Gradle - build fails -- Execution failed for task ':dexDebug'

Error:
Gradle: Execution failed for task ':vertretungsplan:dexDebug'.
> Failed to run command:
P:\Android-Studio\sdk\build-tools\18.0.1\dx.bat --dex --output P:\Projekte\VertretungsplanProject\vertretungsplan\build\libs\vertretungsplan-debug.dex P:\Projekte\VertretungsplanProject\vertretungsplan\build\classes\debug P:\Projekte\VertretungsplanProject\vertretungsplan\build\dependency-cache\debug P:\Android-Studio\sdk\extras\android\m2repository\com\android\support\support-v4\18.0.0\support-v4-18.0.0.jar P:\Projekte\VertretungsplanProject\vertretungsplan\libs\commons-io-2.4.jar P:\Projekte\VertretungsplanProject\vertretungsplan\build\exploded-bundles\VertretungsplanProjectLibrariesActionbarsherlockUnspecified.aar\classes.jar
Error Code:
2
Output:
trouble processing:
bad class file magic (cafebabe) or version (0033.0000)
...while parsing de/MayerhoferSimon/Vertretungsplan/LoginActivity$2.class
...while processing de/MayerhoferSimon/Vertretungsplan/LoginActivity$2.class
trouble processing:
bad class file magic (cafebabe) or version (0033.0000)
...while parsing de/MayerhoferSimon/Vertretungsplan/MainActivity$1.class
...while processing de/MayerhoferSimon/Vertretungsplan/MainActivity$1.class
trouble processing:
bad class file magic (cafebabe) or version (0033.0000)
...while parsing de/MayerhoferSimon/Vertretungsplan/YQL/YqlVplanParser.class
...while processing de/MayerhoferSimon/Vertretungsplan/YQL/YqlVplanParser.class
3 warnings
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dx.util.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:592)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:550)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:531)
at com.android.dx.merge.DexMerger.mergeDexBuffers(DexMerger.java:168)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:186)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:300)
at com.android.dx.command.dexer.Main.run(Main.java:232)
at com.android.dx.command.dexer.Main.main(Main.java:174)
at com.android.dx.command.Main.main(Main.java:91)
Project structure:
build.gradle (actionbarsherlock)
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:18.0.0'
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 11
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
build.gradle (vertretungsplan)
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/commons-io-2.4.jar')
compile project(':libraries:actionbarsherlock')
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 11
}
}
settings.gradle
include ':vertretungsplan', ':libraries:actionbarsherlock'
How can I fix this error?
The right answer is, that some of your jar files does not compile.
You should go into your build.gradle file in your project, and look in your dependencies.
If you're just importing some jar files, you could try to remove them and add them one at a time. This will help you determine which one of them causes the error.
In my case, I did just that, and when I was importing the last one, the app compiled. So I think the real problem was that I was importing too many at once. But now it all works.
I suddenly had the same problem, after no noteworthy changes.
I solved it by deleting the app/build directory and let gradle build the whole project new.
You must check if the same JAR is being imported again. In my case there was a class inside a jar which was getting imported in another jar. So just check if the any lib / class file is being included twice in the whole project!
I got the same sort of error when I tried to compile a utils library jar in eclipse using Java JRE 1.8, and use it in my /libs/ in Android Studio 1.1.0.
I had my Android Studio set to use JDK1.8.0.
I switched my Eclipse to work with JRE 1.7, and the error was fixed.
Eclipse: Window->Preferences->Java tab->Compiler -> Compliance level 1.7. It will most likely prompt you to switch your JRE System Library to jdk1.7.x_x.
You may need to make sure to uncheck 'compress jar' when you export. I haven't tested whether it had an effect or not. I doubt it was related.
I had the same problem too.
In my case the problem started after a reboot.
I closed my App, then I closed the Android Studio (In my case V1.1.0), and finally a normal shutdown. After that, I modified one java file to add a RadioGroup object and then the problem appeared.
I solved my problem only changing a simple '0' for a '1' in my Gradle configuration file, because the root cause of the problem was generated at the Gradle execution process. Previously I used to have version '1.0.0' then i changed it to '1.1.0', as stated in the pictures.
Location of the Gradle configuration a changed
Location where I took the right version from (File -> Settings -> Gradle -> Experimental
The problem is NOT about Execution failed for task ':dexDebug'
if you look above the error showed in red you are going to see this
To solve this problem permanently just add these lines in your build.gradle file
android {
dexOptions {
jumboMode = true
}
}
For further details check this question: here
Make sure your AndroidManifest file contains a package name in the manifest node.
Setting a package name fixed this problem for me.
ANDROID STUDIO Users try this:-
You need to add the following to your gradle file dependencies:
compile 'com.android.support:multidex:1.0.0'
And then add below line ( multidex support application ) to your manifest's application tag:
android:name="android.support.multidex.MultiDexApplication"
Could fix this by adding
compile 'com.android.support:support-v4:18.0.0'
to the dependencies in the vertretungsplan build.gradle, compile and then remove this line and compile again.
now it works
I had the same problem, you should do:
File -> Invalidate Caches / Restart
I had this problem because I tried to use both support library and appcompat:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:support-v4:23.1.0'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.google.android.gms:play-services:8.3.0'
}
After I deleted support library and changed to older version, it compiled:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
/*compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'*/
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'com.google.android.gms:play-services:8.3.0'
}
I had two incompatible dependencies.
The below dependencies caused the error.
compile 'com.google.android.gms:play-services-fitness:8.3.0'
compile 'com.google.android.gms:play-services-wearable:8.4.0'
By changing the fitness dependency to version 8.4.0 I was able to run the app.
compile 'com.google.android.gms:play-services-fitness:8.4.0'
compile 'com.google.android.gms:play-services-wearable:8.4.0'
I found a very interesting issue with Android Studio and the mircrosoft upgrade to the web browser. I upgraded "stupidly" to the latest version of ie. of course Microsoft in their infinite wisdom knows exactly what to do with security. When I tried to compile my app I kept getting the error Gradle - build fails -- Execution failed for task. looking in the stack I saw that it did not recognize the path to java.exe. I found that odd as I was just able to compile the day before. I added JAVA_HOME to the env vars for the system, closed Android Studio and reopened it. Low and behold if the fire wall nag screen did not pop asking if I wanted to all jave.exe through.
What a cluster!
(This might be the wrong thread, as your problem seems more specific, but it's the thread that I found when searching for the issue's keywords)
Despite all good hints, the only thing that helped me, and that I'd like to share just in case, if everything else does not work :
Remove your .gradle directory in your home directory and have it re-build/re-downloaded for you by Android Studio.
Fixed all kinds of weird errors for me that neither were fixable by re-installing Android Studio itself nor the SDK.
A reason can be duplicated libraries after importing from Eclipse IDE.
dependencies {
compile 'com.github.japgolly.android:svg-android:2.0.5'
compile 'com.google.android.gms:play-services:+'
compile 'com.android.support:appcompat-v7:21.0.3'
compile files('libs/androidannotations-api-2.7.1.jar')
compile files('libs/androidasync-2.1.2.jar')
//compile files('libs/google-play-services.jar')
compile files('libs/universal-image-loader-1.8.2.jar')}
I had the same problem, after comment:
//compile files('libs/google-play-services.jar')
The app get no errors.
I faced the same issue .Resolved by doing this .
Go to actionbarsherlock -> module settings ->dependencies .Remove the support v4 library .In bottom left there is a plus button , from there add 1 Library Dependency (Select support-v4) .
Let the gradle resync and clean project once done .
Many of the answers here are trial and error to find duplicate dependencies but if you scroll up just a little bit from the Execution failed for task ':app:dexDebug'. line it will give you a hint at the duplications
.
In my case I had the following error:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define L/com/parse/AbstractQueryController$1;
...
...
...
Execution failed for task ':app:dexDebug'.
So I knew that in order to fix this bug I needed to find the duplicate dependencies that define parse.AbstractQueryController
In my case I had two imported modules that were loading in two different Parse libraries. Making my project only load one fixed my issue.
I have also ran into this error when the package in one of my class files was incorrectly spelled. Many of these answers immediately jump to the Jar files but I would also check to make sure your packages are spelled correctly.
just add in build.gradle
compile 'com.parse.bolts:bolts-android:1.+'
compile 'com.parse:parse-android:1.11.0'
and sync Project with Gradle Files
But Don't Add The parse Jar in libs :) OKK
If you are also using Dagger or Butterknife you should to add guava as a dependency to your build.gradle main file like classpath :
com.google.guava:guava:20.0
In other hand, if you are having problems with larger heap for the Gradle daemon you can increase adding to your radle file:
dexOptions {
javaMaxHeapSize "4g"
}
In my case, I did Build > Clean Project and it worked!
Cleaning the Project using Build in Menu Bar work for many error scenarios in Android Studio and so it does in this case.

Categories

Resources