尝试使用Facebook SDK时Gradle构建失败

时间:2013-07-31 15:19:58

标签: android facebook gradle android-studio

我正在尝试在Android Studio的项目中使用Facebook SDK。我正在关注this教程的第3步。当我尝试运行应用程序时,我得到一个“Gradle:执行失败的任务':FacebookApp:dexDebug'。”错误。如果错误

,下面是输出
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

这是facebook模块的build.gradle:

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

项目的build.gradle:

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

我做错了吗?

3 个答案:

答案 0 :(得分:16)

你要两次添加android支持库,导致dex合并冲突。您的主项目是指带有'com.android.support:support-v4:13.0.+'的maven库,而您的Facebook项目使用files('libs/android-support-v4.jar')引用它。 Gradle无法解决本地jar文件之间的冲突,因此您必须通过maven引用它们。

将Facebook build.gradle的dependencies部分修改为:

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

一切都应该有效。

答案 1 :(得分:1)

在android studio中,这就是我包含支持库和facebook SDK的方式。我支持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'
    ...
}

答案 2 :(得分:0)

我有一个类似的问题,这只是我的疏忽。我在两个不同的gradle.build文件中有com.android.tools.build:gradle:1.1.2和1.1.1的冲突版本。

应用程序/ gradle.build

dependencies {
    ...
    compile 'com.android.tools.build:gradle:1.1.2'
    ...
}

顶级gradle.build

dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
    }

我在项目的gradle.build文件中注释了该行,从命令行运行了gradlew clean,重新启动了Android Studio,然后在Universe中恢复了订单。