Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle build failure when trying to use Facebook SDK

I am trying to use the Facebook SDK in a project in Android Studio. I am following step 3 of this tutorial. When I try to Run the app, I get a "Gradle: Execution failed for task ':FacebookApp:dexDebug'." error. Below is the output if the error

Gradle: Execution failed for task ':FacebookApp:dexDebug'.
> Failed to run command:
C:\android-sdk\build-tools\18.0.0\dx.bat --dex --output C:\Users\Brandon\AndroidStudioProjects\FacebookAppProject\FacebookApp\build\libs\FacebookApp-debug.dex C:\Users\Brandon\AndroidStudioProjects\FacebookAppProject\FacebookApp\build\classes\debug C:\Users\Brandon\AndroidStudioProjects\FacebookAppProject\FacebookApp\build\dependency-cache\debug C:\Users\Brandon\AndroidStudioProjects\FacebookAppProject\FacebookApp\build\exploded-bundles\FacebookAppProjectLibrariesFacebookUnspecified.aar\classes.jar C:\Users\Brandon\AndroidStudioProjects\FacebookAppProject\FacebookApp\build\exploded-bundles\FacebookAppProjectLibrariesFacebookUnspecified.aar\libs\android-support-v4.jar C:\android-sdk\extras\android\m2repository\com\android\support\support-v4\13.0.0\support-v4-13.0.0.jar
    Error Code:
        1
    Output:
        UNEXPECTED TOP-LEVEL EXCEPTION:
        java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoIcsImpl;
            at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
            at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
            at com.android.dx.command.dexer.Main.processClass(Main.java:490)
            at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
            at com.android.dx.command.dexer.Main.access$400(Main.java:67)
            at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
            at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
            at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:131)
            at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
            at com.android.dx.command.dexer.Main.processOne(Main.java:422)
            at com.android.dx.command.dexer.Main.processAllFiles(Main.java:333)
            at com.android.dx.command.dexer.Main.run(Main.java:209)
            at com.android.dx.command.dexer.Main.main(Main.java:174)
            at com.android.dx.command.Main.main(Main.java:91)
        1 error; aborting

Here is the build.gradle for the facebook module:

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android-library'

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

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
        }
    }
}

And the build.gradle for the project:

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

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.android.support:support-v4:13.0.+'
    compile project(':libraries:facebook')
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

Am I doing something wrong?

like image 895
Sega dude Avatar asked Jul 31 '13 15:07

Sega dude


People also ask

Is Facebook SDK open source?

Overview of the Facebook SDK for AndroidThe SDK is open source, and it is hosted at GitHub's facebook / facebook-android-sdk repository.


2 Answers

You're adding the android support library twice, resulting in a dex merge conflict. Your main project refers to the maven library with 'com.android.support:support-v4:13.0.+' and your Facebook project refers to it with files('libs/android-support-v4.jar'). Gradle cannot resolve conflicts between local jar files, so you must refer to them through maven.

Modify the dependencies section of your Facebook build.gradle to:

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

and everything should work.

like image 69
Greg Avatar answered Sep 23 '22 07:09

Greg


In android studio, this is how I include a support library and facebook SDK. I'm supporting API 15+.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'fr.avianey:facebook-android-api:+@aar'
    compile 'com.android.support:support-v13:20.0.0'
    ...
}
like image 25
jyliang Avatar answered Sep 21 '22 07:09

jyliang