在 Android Studio 中使用 Facebook sdk [英] using facebook sdk in Android studio

查看:44
本文介绍了在 Android Studio 中使用 Facebook sdk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 使用 Android Studio 的 Android 版 Facebook SDK.当我运行我的应用程序时,我收到了下面提到的警告.

I'm following Facebook SDK for Android using Android Studio. When I run my application I'm getting the below mentioned warning.

Gradle: module 'facebook' won't be compiled. Unfortunately you can't have non-Gradle Java module and Android-Gradle module in one project.

我该如何解决这个问题?

How should I solve this?

我尝试了@Scott Barta 的回答并收到以下错误消息.

I tried @Scott Barta's answer and get the following error message.

    A problem occurred configuring project ':App'.
> Failed to notify project evaluation listener.
   > A problem occurred configuring project ':libraries:facebook'.
      > Failed to notify project evaluation listener.
         > Could not resolve all dependencies for configuration ':libraries:facebook:_DebugCompile'.
            > Could not find any version that matches com.android.support:support-v4:+.
              Required by:
                  MyApplication2.libraries:facebook:unspecified

推荐答案

注意

对于 Android Studio 0.5.5 及更高版本以及更高版本的 Facebook SDK,此过程比下面记录的内容(为两者的早期版本编写)要简单得多.如果您运行的是最新版本,那么您需要做的就是:

For Android Studio 0.5.5 and later, and with later versions of the Facebook SDK, this process is much simpler than what is documented below (which was written for earlier versions of both). If you're running the latest, all you need to do is this:

  1. https://developers.facebook.com/docs/android/下载 Facebook SDK一个>
  2. 解压存档
  3. 在 Android Studio 0.5.5 或更高版本中,从文件"菜单中选择导入模块".
  4. 在向导中,将要导入的模块的源路径设置为解压存档中的facebook"目录.(注意:如果您选择整个父文件夹,它不仅会引入库本身,还会引入所有示例应用程序,每个应用程序都是一个单独的模块.这可能有效,但可能不是您想要的).
  5. 通过Ctrl + Shift + Alt + S 打开项目结构,然后选择依赖项选项卡.单击 + 按钮并选择 Module Dependency.在弹出的新窗口中选择 :facebook.
  6. 你应该没事了.
  1. Download the Facebook SDK from https://developers.facebook.com/docs/android/
  2. Unzip the archive
  3. In Android Studio 0.5.5 or later, choose "Import Module" from the File menu.
  4. In the wizard, set the source path of the module to import as the "facebook" directory inside the unpacked archive. (Note: If you choose the entire parent folder, it will bring in not only the library itself, but also all of the sample apps, each as a separate module. This may work but probably isn't what you want).
  5. Open project structure by Ctrl + Shift + Alt + S and then select dependencies tab. Click on + button and select Module Dependency. In the new window pop up select :facebook.
  6. You should be good to go.

<小时>

旧版 Android Studio 和旧版 Facebook SDK 的说明

这适用于 Android Studio 0.5.4 及更早版本,并且对于 Facebook SDK 版本在 Facebook 为发行版提供 Gradle 构建文件之前的版本最有意义.我不知道他们在哪个版本的 SDK 中进行了更改.

This applies to Android Studio 0.5.4 and earlier, and makes the most sense for versions of the Facebook SDK before Facebook offered Gradle build files for the distribution. I don't know in which version of the SDK they made that change.

Facebook 在其 https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android-using-android-studio/3.0/ 页面是基于 Gradle 的项目是错误的(即您的项目是使用 Android Studio 的新建项目向导构建的和/或具有用于您的应用程序模块的 build.gradle 文件).请按照以下说明操作:

Facebook's instructions under "Import the SDK into an Android Studio Project" on their https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android-using-android-studio/3.0/ page are wrong for Gradle-based projects (i.e. your project was built using Android Studio's New Project wizard and/or has a build.gradle file for your application module). Follow these instructions instead:

  1. 在项目的主目录下创建一个 libraries 文件夹.例如,如果您的项目是 HelloWorldProject,您将创建HelloWorldProject/libraries 文件夹.

  1. Create a libraries folder underneath your project's main directory. For example, if your project is HelloWorldProject, you would create a HelloWorldProject/libraries folder.

现在从 SDK 中复制整个 facebook 目录安装到您刚刚创建的 libraries 文件夹中.

Now copy the entire facebook directory from the SDK installation into the libraries folder you just created.

删除 facebook 目录中的 libs 文件夹.如果你喜欢,删除 project.propertiesbuild.xml.classpath.project.文件也是如此.你不需要它们.

Delete the libs folder in the facebook directory. If you like, delete the project.properties, build.xml, .classpath, and .project. files as well. You don't need them.

facebook 目录中创建一个 build.gradle 文件以下内容:

Create a build.gradle file in the facebook directory with the following contents:

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

apply plugin: 'android-library'

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

android {
    compileSdkVersion 17
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }

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

请注意,根据您遵循这些说明的时间与编写此说明的时间相比,您可能需要调整 classpath 'com.android.tools.build:gradle:0.6.+'行以引用较新版本的 Gradle 插件.很快我们将需要 0.7 或更高版本.尝试一下,如果您收到提示需要更新版本的 Gradle 插件的错误,那么您必须编辑该行.

Note that depending on when you're following these instructions compared to when this is written, you may need to adjust the classpath 'com.android.tools.build:gradle:0.6.+' line to reference a newer version of the Gradle plugin. Soon we will require version 0.7 or later. Try it out, and if you get an error that a newer version of the Gradle plugin is required, that's the line you have to edit.

确保安装了 SDK 管理器中的 Android 支持库.

Make sure the Android Support Library in your SDK manager is installed.

编辑应用程序主目录中的 settings.gradle 文件并添加这一行:

Edit your settings.gradle file in your application’s main directory and add this line:

include ':libraries:facebook'

  • 如果您的项目已在 Android Studio 中打开,请单击工具栏中的将项目与 Gradle 文件同步"按钮.完成后,facebook 模块应该会出现.

    这篇关于在 Android Studio 中使用 Facebook sdk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆