using facebook sdk in Android studio

157,546

Solution 1

NOTE

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

Instructions for older Android Studio and older Facebook 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'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. Create a libraries folder underneath your project's main directory. For example, if your project is HelloWorldProject, you would create a HelloWorldProject/libraries folder.

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

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

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

    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.

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

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

    include ':libraries:facebook'
    
  7. If your project is already open in Android Studio, click the "Sync Project with Gradle Files" button in the toolbar. Once it's done, the facebook module should appear. enter image description here

  8. Open the Project Structure dialog. Choose Modules from the left-hand list, click on your application’s module, click on the Dependencies tab, and click on the + button to add a new dependency. enter image description here
  9. Choose “Module dependency”. It will bring up a dialog with a list of modules to choose from; select “:libraries:facebook”. enter image description here
  10. Click OK on all the dialogs. Android Studio will automatically resynchronize your project (making it unnecessary to click that "Sync Project with Gradle Files" button again) and pick up the new dependency. You should be good to go.

Solution 2

Facebook has indeed added the SDK to the Maven Central repositories. To configure your project using the maven repo's instance, you'll need to do 2 things:

  1. In your projects top-level build.gradle file, add the Maven Central repositories. Mine looks like this:

    repositories {
        jcenter()       // This is the default repo
        mavenCentral()  //  This is the Maven Central repo
    }
    
  2. In the app-level build.grade file, add the Facebook sdk dependency:

    dependencies {
    
        compile 'com.facebook.android:facebook-android-sdk:4.5.0' // Adjust the version accordingly
        // All your other dependencies.
    }
    

You can also adjust the specific Facebook SDK version as well. For a list of available versions in the maven repository click this link.

Solution 3

Facebook publishes the SDK on maven central :

Just add :

repositories {
    jcenter()       // IntelliJ main repo.
}

dependencies {
    compile 'com.facebook.android:facebook-android-sdk:+'
}

Solution 4

Scott Barta's solution worked for me, except I had to add these to the dependencies of my main project build.gradle file:

compile files('libs/android-support-v4.jar')
compile project(':libraries:facebook')

Also worth mentioning, you need to make sure:

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.1"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 18
    }

Are the same in both build.gradle files...Once i did this it ran like a charm.

Solution 5

When using git you can incorporate the newest facebook-android-sdk with ease.

  • Add facebook-android-sdk as submodule: git submodule add https://github.com/facebook/facebook-android-sdk.git
  • Add sdk as gradle project: edit settings.gradle and add line: include ':facebook-android-sdk:facebook'
  • Add sdk as dependency to module: edit build.gradle and add within dependencies block: compile project(':facebook-android-sdk:facebook')
Share:
157,546

Related videos on Youtube

LoveMeSomeFood
Author by

LoveMeSomeFood

Updated on March 07, 2020

Comments

  • LoveMeSomeFood
    LoveMeSomeFood about 4 years

    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?

    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
    
    • Scott Barta
      Scott Barta over 10 years
      Is your Android Support Repository installed in your SDK manager for the SDK you're using?
    • LoveMeSomeFood
      LoveMeSomeFood over 10 years
      Nope. Is it mandatory?
    • Scott Barta
      Scott Barta over 10 years
      Yes. That's the error message you're seeing. I know it's unintuitive; improving it is on our list. I'll update my answer to reflect the need for this.
  • LoveMeSomeFood
    LoveMeSomeFood over 10 years
    I already did this. Still its showing the error. I think the error says facebook is non-gradle and so it cant be commpiled. But Im not sure
  • Volodymyr
    Volodymyr over 10 years
    Try build through console: ./gradlew clean assembleDebug
  • swebal
    swebal over 10 years
    I get an error when building: Project with path 'libraries:facebook' could not be found in root project ... Any ideas? (I have of course added the folder according to Scott Bs solution, even though I do NOT have a settings.gradle file?! (Project created in Android Studio)
  • sensorario
    sensorario over 10 years
    /libraries/facebook/build.gradle give me some errors. "Cannot resolve compileSdkVersion symbol". The same with compileSdkVersion symbol, manifest and others ...
  • sensorario
    sensorario over 10 years
    Can I add fb-sdk to an Android application or I can only add sdk to a new Android application?
  • Scott Barta
    Scott Barta over 10 years
    If you're having problems, please post them as a separate question and include the details of your error messages and build files.
  • LoveMeSomeFood
    LoveMeSomeFood over 10 years
    Hi, I tried this and got an error in gradle task. I have updated the question with error details.
  • Mark
    Mark over 10 years
    This worked for me, but I had to change the path to the facebook libraries grade project to be the absolute path, i.e. ":MyApp:libraries:facebook" in both the settings file and the build.grade file for my main app
  • Samuel Barbosa
    Samuel Barbosa over 10 years
    Very good! This answer should be part of official Facebook SDK tutorial.
  • sadegh saati
    sadegh saati about 10 years
    Found no modules to depend on,Android Studio said.
  • Austyn Mahoney
    Austyn Mahoney about 10 years
    I had to add include ':libraries:facebook' to my settings.gradle` and Sync Project with Gradle Files before I was able to get compile project(':libraries:facebook') to work right.
  • EMBEDONIX
    EMBEDONIX about 10 years
    I get an error that says \libraries\facebook\AndroidManifest.xml can not be found. I get this error after syncying with gradle. That file actually is not present in the folder
  • Mgamerz
    Mgamerz almost 10 years
    This doesn't work anymore, your site is down. Killed my whole IDE.
  • Fattie
    Fattie almost 10 years
    thank you @Scott for this incredibly fresh information on 0.5.5 plus. thank you!
  • Mgamerz
    Mgamerz almost 10 years
    Weird, I used sites that check if a site is down and even your homepage would not load.
  • doubleA
    doubleA almost 10 years
    the compile project line needs to match your project directory structure. :libraries:facebook implies that your facebook library is in a folder called libraries within your root directory
  • Diego Palomar
    Diego Palomar almost 10 years
    This solution does not work for me. After selecting the facebook directory inside the facebook-android-sdk-3.16 directory, the wizard still says 'Select modules to import' :$. I use Android Studio (beta) 0.8.2. Any idea?
  • Ojonugwa Jude Ochalifu
    Ojonugwa Jude Ochalifu almost 10 years
    Why can't they just make this thing straightforward for once?
  • Chris
    Chris over 9 years
    This works for me with a few additional steps: 1. Set facebook module as an explicit dependency of your main module in the module settings 2. Ensure the version of the bolts jar is the same for both the facebook and your main modules (usually mobile)
  • BonanzaDriver
    BonanzaDriver over 9 years
    For the latest versions what you posted is essentially correct - you need to have the latest Android Support Library and Android Support Repository installed as well. Thanks for a great answer.
  • Till
    Till over 9 years
    If you choose the 'facebook' folder for the module import, then you need to copy the gradle.properties file from the parent folder into this 'facebook' folder
  • Olkunmustafa
    Olkunmustafa over 9 years
    It is working for me. It is really the best way to add facebook api on Android Studio. Thanks
  • iqueqiorio
    iqueqiorio over 9 years
    The NOTE does not appear to be working for android studio version 0.8.14 and Facebook sdk v3.20. I get the error Gradle: module 'facebook' won't be compiled. Unfortunately you can't have non-Gradle Java module and Android-Gradle module in one project
  • Abhishek Balani
    Abhishek Balani over 9 years
    This is the method on Facebook docs but it's not working. I have android studio 1.0 and sdk v 3.x.
  • nAkhmedov
    nAkhmedov over 9 years
    It gives to me 'Error:(26, 13) Gradle: error: package bolts does not exist'
  • Greg Ennis
    Greg Ennis about 9 years
    Note you should specify an exact version number or at least a major, like 3.23.+. Facebook is not afraid to version the API rapidly and make breaking changes, which will cause build failures and non-repeatable builds.
  • SBerg413
    SBerg413 about 9 years
    @GregEnnis - good input. I updated the answer slightly and added a comment in the code as well.
  • Kaveesh Kanwal
    Kaveesh Kanwal about 9 years
    So, i do not have to import any external library and all? Just simple add the fb sdk in the gradle file??
  • Kaveesh Kanwal
    Kaveesh Kanwal about 9 years
    You don't need to do all this. Simply, just add the facebook dependency in your app's gradel build. Adjust the version accordingly. dependencies { compile 'com.facebook.android:facebook-android-sdk:3.23.+' }
  • Shaktisinh Jadeja
    Shaktisinh Jadeja about 9 years
    did all things ... no any error... still unable to import com.facebook.Session
  • Andre Figueiredo
    Andre Figueiredo about 9 years
    It gives me Could not find property 'ANDROID_BUILD_SDK_VERSION' on project ':facebook'.
  • Jaimin Modi
    Jaimin Modi over 8 years
    Error:(15, 0) Could not find property 'ANDROID_BUILD_SDK_VERSION' on project ':facebook'. <a href="openFile:/home/jaimin/AndroidStudioProjects/LatestFace‌​bookIntegration/face‌​book/build.gradle">O‌​pen File</a>
  • Booger
    Booger almost 8 years
    This is not right, you need mavenCentral() in the repositories section.
  • Romain
    Romain almost 8 years
    jcenter include mavenCentral
  • AAEM
    AAEM almost 5 years
    when i click "Click on + button and select Module Dependency" --> there is no Module Dependency to select