Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio and Gradle - build fails

Tags:

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
    }
}
like image 937
Vincent Mimoun-Prat Avatar asked Jun 17 '13 08:06

Vincent Mimoun-Prat


People also ask

What went wrong Gradle could not start your build?

Your issue is some files are missing, don't delete the Gradle file and any inbuild files and classes. If you need this project try to undo what you did, or try to recover revert it back if your project is connected with the version controller.

How do I sync Gradle with Android Studio?

Open your gradle. properties file in Android Studio. Restart Android Studio for your changes to take effect. Click Sync Project with Gradle Files to sync your project.


2 Answers

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".

like image 192
Siva Velusamy Avatar answered Oct 01 '22 01:10

Siva Velusamy


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?

like image 22
Vincent Mimoun-Prat Avatar answered Oct 01 '22 01:10

Vincent Mimoun-Prat