Android Studio and Gradle – build fails-ThrowExceptions

Exception or error:

I am building a small library project along wit a sample project to illustrate the use. I can’t manage to run the sample in Android Studio. I have created the project from scratch. I am experienced with Eclipse but it’s my first try at Android Studio & Gradle.

The error given:

Gradle: Execution failed for task ‘:demo:dexDebug’.

Running C:\DevTools\Android\android-studio\sdk\build-tools\android-4.2.2\dx.bat
failed. See output

I have the following folder structure:

- demo
  - build
  - libs
    - android-support-v4.jar
  - src
    - main
      - java
      - res
  - build.gradle
- library
  - build
  - libs
    - android-support-v4.jar
  - src
    - main
      - java
      - res
  - build.gradle
- build.gradle
- settings.gradle

Build.gradle at project root:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

Settings.gradle at project root:

include ':library', ':demo'

Build.gradle for the library module:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android-library'

dependencies {
    compile files('libs/android-support-v4.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

Build.gradle for the sample module:

buildscript {
    repositories {
        mavenCentral() 
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile project(':library')
    compile files('libs/android-support-v4.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}
How to solve:

Specifying compile files('libs/android-support-v4.jar') means that every library includes support v4. What you want to do is just specify that every library depends on it:

dependencies {
    compile 'com.android.support:support-v4:13.0.0'
}

This will allow gradle to detect all dependencies and include this only once.

Note: You have to first use the SDK Manager and download and install two Maven repositories: “Android Support Repository” and “Google Repository”.

###

I found the problem:

I removed that line from the sample gradle file.

compile files('libs/android-support-v4.jar')

However, I have no idea why this does not work (if I have 2 or 3 external libraries that all depend on the support library, how are we supposed to do, without touching their gradle files?

###

You should navigate to your libs folder in the IDE, right click on the jar and select to add the library to the project, it still needs to establish the dependency even though the jar appears to be there. Also look at your gradle built script to make sure the dependency appears there. If that still doesnt work just run a gradle clean on the project. Intellij documentation will give you more details on what clean does. see:

stackoverflow gradle build

Leave a Reply

Your email address will not be published. Required fields are marked *