4

I recently added a new activity to my android studio project and now I am getting an error when I try and run it. It builds fine but I get the error below when I run it:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:preDexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    /Applications/Android Studio.app/sdk/build-tools/android-4.4W/dx --dex --output /Users/davidcavanagh/joshcpdandroid/app/build/intermediates/pre-dexed/debug/classes-22ecb8c50fefe43948d87c9fee8e36a6b7d1bb5a.jar /Users/davidcavanagh/joshcpdandroid/app/build/intermediates/exploded-aar/com.android.support/support-v4/20.0.0/classes.jar
  Error Code:
    1

Here is my build.gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion '20.0.0'

    defaultConfig {
        applicationId "com.joshcpd.android"
        minSdkVersion 15
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:5.2.08'
    compile 'com.android.support:appcompat-v7:20.0.0'
    compile project(':libraries:zbar')
}

I have tried removing the supportLappcompat dependency but then I get even more errors. Any help greatly appreciated.

DMC
  • 1,184
  • 5
  • 21
  • 50

4 Answers4

15

I finally solved this issue by downloading the latest version of gradle.

I still get this error every so often and I just fix it by going to File ----> Invalidate caches/restart

I find Android Studio to be very buggy

DMC
  • 1,184
  • 5
  • 21
  • 50
1

Based on where it's trying to find the dx command:

/Applications/Android Studio.app/sdk/build-tools/android-4.4W/dx

it looks like you've crossed a build tools version with something else. The build file you posted in your question looks okay, but look in the other build files in your project (there's at least going to be something in libraries/zbar) and make sure their buildToolsVersion statements are okay. I suspect somewhere you have:

buildToolsVersion 'android-4.4W'

where you should have:

buildToolsVersion '20.0.0'
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • No that didn't work. I think Im either missing a correct dependency or there is a conflict like you said. Thanks – DMC Aug 27 '14 at 17:00
1

Now i know the reason of the problem. It was causes by wrong code in \sdk\android-sdk\tools\lib\find_java.bat

Wrong code:

find /i "x86" > NUL && set arch_ext=32 || set arch_ext=64

Correct code:

find /i "x86" > NUL && set arch_ext=32||set arch_ext=64

The wrong code leads to the path of java_exe be

E:\Android\sdk\ANDROI~1\tools\lib\\find_java32 .exe

So, when dx.bat runs

call "%java_exe%" %javaOpts% -Djava.ext.dirs="%frameworkdir%" -jar "%jarpath%" %params% 

it will report the wrong status:

Error Code:
    1

My platform: win8-32 bit android studio 1.0 - 4GB RAM

Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
sridhar
  • 11
  • 1
0

I had a similar issue, so I uninstalled the java jdk and reinstalled the latest version from oracle and everything went OK. The link for installing the latest version of jdk is here

Note: you need to update the jdk directory in android studio after doing the above

Sami Kanafani
  • 14,244
  • 6
  • 45
  • 41