using facebook sdk in Android studio
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:
- Download the Facebook SDK from https://developers.facebook.com/docs/android/
- Unzip the archive
- In Android Studio 0.5.5 or later, choose "Import Module" from the File menu.
- 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).
- 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.
- 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:
Create a
libraries
folder underneath your project's main directory. For example, if your project is HelloWorldProject, you would create aHelloWorldProject/libraries
folder.Now copy the entire
facebook
directory from the SDK installation into thelibraries
folder you just created.Delete the
libs
folder in thefacebook
directory. If you like, delete theproject.properties
,build.xml
,.classpath
, and.project
. files as well. You don't need them.-
Create a
build.gradle
file in thefacebook
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. Make sure the Android Support Library in your SDK manager is installed.
-
Edit your
settings.gradle
file in your application’s main directory and add this line:include ':libraries:facebook'
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.- 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.
- Choose “Module dependency”. It will bring up a dialog with a list of modules to choose from; select “:libraries:facebook”.
- 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:
-
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 }
-
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')
Related videos on Youtube
LoveMeSomeFood
Updated on March 07, 2020Comments
-
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 over 10 yearsIs your Android Support Repository installed in your SDK manager for the SDK you're using?
-
LoveMeSomeFood over 10 yearsNope. Is it mandatory?
-
Scott Barta over 10 yearsYes. 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 over 10 yearsI 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 over 10 yearsTry build through console: ./gradlew clean assembleDebug
-
swebal over 10 yearsI 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 over 10 years/libraries/facebook/build.gradle give me some errors. "Cannot resolve compileSdkVersion symbol". The same with compileSdkVersion symbol, manifest and others ...
-
sensorario over 10 yearsCan I add fb-sdk to an Android application or I can only add sdk to a new Android application?
-
Scott Barta over 10 yearsIf you're having problems, please post them as a separate question and include the details of your error messages and build files.
-
LoveMeSomeFood over 10 yearsHi, I tried this and got an error in gradle task. I have updated the question with error details.
-
Mark over 10 yearsThis 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 over 10 yearsVery good! This answer should be part of official Facebook SDK tutorial.
-
sadegh saati about 10 yearsFound no modules to depend on,Android Studio said.
-
Austyn Mahoney about 10 yearsI had to add
include ':libraries:facebook' to my
settings.gradle` and Sync Project with Gradle Files before I was able to getcompile project(':libraries:facebook')
to work right. -
EMBEDONIX about 10 yearsI 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 almost 10 yearsThis doesn't work anymore, your site is down. Killed my whole IDE.
-
Fattie almost 10 yearsthank you @Scott for this incredibly fresh information on 0.5.5 plus. thank you!
-
Mgamerz almost 10 yearsWeird, I used sites that check if a site is down and even your homepage would not load.
-
doubleA almost 10 yearsthe 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 almost 10 yearsThis 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 almost 10 yearsWhy can't they just make this thing straightforward for once?
-
Chris over 9 yearsThis 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 over 9 yearsFor 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 over 9 yearsIf 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 over 9 yearsIt is working for me. It is really the best way to add facebook api on Android Studio. Thanks
-
iqueqiorio over 9 yearsThe 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 over 9 yearsThis is the method on Facebook docs but it's not working. I have android studio 1.0 and sdk v 3.x.
-
nAkhmedov over 9 yearsIt gives to me 'Error:(26, 13) Gradle: error: package bolts does not exist'
-
Greg Ennis about 9 yearsNote 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 about 9 years@GregEnnis - good input. I updated the answer slightly and added a comment in the code as well.
-
Kaveesh Kanwal about 9 yearsSo, i do not have to import any external library and all? Just simple add the fb sdk in the gradle file??
-
Kaveesh Kanwal about 9 yearsYou 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 about 9 yearsdid all things ... no any error... still unable to import com.facebook.Session
-
Andre Figueiredo about 9 yearsIt gives me
Could not find property 'ANDROID_BUILD_SDK_VERSION' on project ':facebook'.
-
Jaimin Modi over 8 yearsError:(15, 0) Could not find property 'ANDROID_BUILD_SDK_VERSION' on project ':facebook'. <a href="openFile:/home/jaimin/AndroidStudioProjects/LatestFacebookIntegration/facebook/build.gradle">Open File</a>
-
Booger almost 8 yearsThis is not right, you need mavenCentral() in the repositories section.
-
Romain almost 8 yearsjcenter include mavenCentral
-
AAEM almost 5 yearswhen i click "Click on + button and select Module Dependency" --> there is no Module Dependency to select