Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

Android Studio and Gradle - build fails

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
    }
}

Upvotes: 21

Views: 38678

Answers (5)

Aqif
Aqif

Reputation: 326

In my Case replace this line classpath "com.android.tools.build:gradle:7.0.2"

Upvotes: 0

Sahil Khan
Sahil Khan

Reputation: 41

This error could be encountered while migrating from Groovy to kotlin DSL as well and here are the steps to get rid of it:

  1. If you are still in the process of migrating please complete the migration of gradle files first, use kts syntax and then sync gradle files.

  2. Use this dependency inside your build.gradle(app level):

    implementation("androidx.legacy:legacy-support-v4:1.0.0")

  3. Remove id("kotlin-android-extensions") from plugins block inside build.gradle.kts (app level).

That's it! 3rd Point solved the issue for me but trying all the points should definitely fix the issue.

Upvotes: 0

Chris
Chris

Reputation: 4593

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

Upvotes: 0

Siva Velusamy
Siva Velusamy

Reputation: 2489

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

Upvotes: 25

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

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?

Upvotes: 1

Related Questions