Android Studio 中的 Facebook SDK 错误

Facebook SDK Error in Android Studio

使用 Android Studio v0.8.14 和 Facebook SDK v3.22.0

在 Android Studio 中,添加 facebook-sdk/facebook 文件夹作为模块。并且模块已作为依赖项添加到 Project Structure 设置中。 Gradle 同步正常。尽管当我尝试 运行 应用程序时出现了那些奇怪的错误:

/Users/myuser/application-path/facebook/src/com/facebook/AuthorizationClient.java
Error:(29, 28) error: cannot find symbol class R
/Users/myuser/application-path/facebook/src/com/facebook/FacebookRequestError.java
Error:(19, 28) error: cannot find symbol class R
/Users/myuser/application-path/facebook/src/com/facebook/widget/WebDialog.java
Error:(40, 28) error: cannot find symbol class R
/Users/myuser/application-path/facebook/src/com/facebook/LoginActivity.java
Error:(24, 28) error: cannot find symbol class R
/Users/myuser/application-path/facebook/src/com/facebook/Settings.java
Error:(33, 28) error: cannot find symbol class BuildConfig
/Users/myuser/application-path/facebook/src/com/facebook/internal/ImageDownloader.java
Error:(25, 28) error: cannot find symbol class R
/Users/myuser/application-path/facebook/src/com/facebook/widget/FriendPickerFragment.java
Error:(29, 28) error: cannot find symbol class R
/Users/myuser/application-path/facebook/src/com/facebook/widget/GraphObjectAdapter.java
Error:(27, 28) error: cannot find symbol class R
/Users/myuser/application-path/facebook/src/com/facebook/widget/LikeView.java
Error:(35, 28) error: cannot find symbol class R
/Users/myuser/application-path/facebook/src/com/facebook/widget/PlacePickerFragment.java
Error:(36, 28) error: cannot find symbol class R
/Users/myuser/application-path/facebook/src/com/facebook/RequestBatch.java

这是脸书 build.gradle:

apply plugin: 'com.android.library'

repositories {
  mavenCentral()
}

project.group = 'com.facebook.android'

dependencies {
    compile 'com.android.support:support-v4:[21,22)'
    compile 'com.parse.bolts:bolts-android:1.1.4'
}

android {
    compileSdkVersion 14
    buildToolsVersion '19.1.0'

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 14
    }

    lintOptions {
        abortOnError false
    }

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

apply plugin: 'maven'
apply plugin: 'signing'

def isSnapshot = version.endsWith('-SNAPSHOT')
def ossrhUsername = hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
def ossrhPassword = hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""

task setVersion {
    // The version will be derived from source
    project.version = null
    def sdkVersionFile = file('src/com/facebook/FacebookSdkVersion.java')
    sdkVersionFile.eachLine{
        def matcher = (it =~ /(?:.*BUILD = \")(.*)(?:\".*)/)
        if (matcher.matches()) {
          project.version = matcher[0][1]
          return
        }
    }
    if (project.version.is('unspecified')) {
      throw new GradleScriptException('Version could not be found.', null)
    }
}

uploadArchives {
    repositories.mavenDeployer {
        beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

        repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
            authentication(userName: ossrhUsername, password: ossrhPassword)
        }

        snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
            authentication(userName: ossrhUsername, password: ossrhPassword)
        }

        pom.project {
            name 'Facebook-Android-SDK'
            artifactId = 'facebook-android-sdk'
            packaging 'aar'
            description 'Facebook Android SDK'
            url 'https://github.com/facebook/facebook-android-sdk'

            scm {
                connection 'scm:git@github.com:facebook/facebook-android-sdk.git'
                developerConnection 'scm:git@github.com:facebook/facebook-android-sdk.git'
                url 'https://github.com/facebook/facebook-android-sdk'
            }

            licenses {
                license {
                    name 'The Apache Software License, Version 2.0'
                    url 'https://github.com/facebook/facebook-android-sdk/blob/master/LICENSE.txt'
                    distribution 'repo'
                }
            }

            developers {
                developer {
                    id 'facebook'
                    name 'Facebook'
                }
            }
        }
    }
}

uploadArchives.dependsOn(setVersion)

signing {
    required { !isSnapshot && gradle.taskGraph.hasTask("uploadArchives") }
    sign configurations.archives
}

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
    classifier = 'javadoc'
    from androidJavadocs.destinationDir
}

task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.sourceFiles
}

artifacts {
    archives androidSourcesJar
    archives androidJavadocsJar
}

afterEvaluate {
    androidJavadocs.classpath += project.android.libraryVariants.toList().first().javaCompile.classpath
}

这也是 build.gradle 应用:

apply plugin: 'com.android.application'
apply plugin: 'android-apt'

def AAVersion = '3.2'

android {
    compileSdkVersion 14
    buildToolsVersion '19.1.0'
    defaultConfig {
        applicationId "com.general.application"
        minSdkVersion 14
        targetSdkVersion 14
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName 'com.general.application'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:19.0.0'
    compile 'com.android.support:appcompat-v7:19.0.0'
    compile 'com.parse.bolts:bolts-android:1.1.4'
    compile fileTree(dir: 'libs', include: 'Parse-*.jar')
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile project(':facebook')
}

我已经检查了所有文档,甚至是使用旧版 Android Studio(例如 v0.5.x)的 YouTube 教程。 Facebook 文档已弃用,示例不适合 Android Studio,因此我找不到正确的配置。

任何帮助都会很棒。

所以我自己找到了答案。

我的 Android Studio 已过时,因此已按照建议更新。比清除所有 Facebook SDK 内容包括 gradle 文件并重新添加。就这些了。