Using Facebook SDK in Android Studio 0.4.2
android-studio, facebook-android-sdk
Solution
To add to the other answer, add the line `include ':libraries:facebook'` to your settings.gradle and you'll get a new error
Gradle 'Android' project refresh failed: Configuration with name 'default' not found.
But hey this is what we call progress.
EDI:
Okay now it's working, here is exactly what I did. - I put the FB SDK at the wrong path since the beginning, I was creating a libraries folder at the same level as my app, you should not, it'll not works. - Since Android Studio 0.4.+ the folder name should be libs and not libraries
Create a folder libs at the root of your project (same place where you find the settings.gradle)
Copy the Facebook folder from the Facebook SDK in the libs folder
In your `settings.gradle` add `include ':libs:Facebook'` before the include of your main app
In your build.gradle of your main app add this line in your dependencies
compile project(':libs:facebook')
Be sure that Android Support V4 is also one of your dependencies i.e:
compile 'com.android.support:support-v4:18.0.0'
Also here is the build.gradle of my Facebook libs
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.1'
}
}
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:+'
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 15
targetSdkVersion 19
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
Now sync gradle and everything should works fine, I'm on Android Studio 0.4.2 + Facebook SDK 3.6.0
Problem
I'm trying to create a new Android application that uses the Facebook SDK, I'm using latests versions for everything, so I'm using Android Studio 0.4.0 with the new Gradle compilation system and the latests version of the SDK downloaded from Facebook. I have tried to follow the instructions in the Facebook Developers page: https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android-using-android-studio/3.0/ with no luck, because the instructions are not for a Gradle based Android Studio. I have also tried to follow the instructions from Scott Barta in using facebook sdk in android studio but no luck, the step 7 when "Sync Project with Gradle Files" is not working, I'm getting this error: Failed to refresh Gradle project. You are using an old, unsupported version of Gradle. Please use version 1.9 or greater. Please point to a supported Gradle version in the project's Gradle settings or in the project's Gradle wrapper (if applicable.) I have tried to modify the build.gradle file and change the classpath line from: ``` classpath 'com.android.tools.build:gradle:0.6.+' ``` to: ``` classpath 'com.android.tools.build:gradle:0.7.+' ``` And also change the compileSdkVersion, buildToolsVersion, minSdkVersion and targetSdkVersion to the values I have in my project, but is not working. Can anyone help me? Thanks! EDIT: I have updated today to Android 0.4.2 but no luck with the new version. My gradle-wrapper.properties file is using Gradle 1.9: ``` #Wed Apr 10 15:27:10 PDT 2013 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=http\://services.gradle.org/distributions/gradle-1.9-all.zip ``` This is the build.gradle file I use to compile the Facebook library: ``` buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.7.+' } } apply plugin: 'android-library' dependencies { compile 'com.android.support:support-v4:+' } android { compileSdkVersion 19 buildToolsVersion "19.0.1" defaultConfig { minSdkVersion 7 targetSdkVersion 19 } sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] res.srcDirs = ['res'] } } } ``` And this is the complete build.gradle I use for my application: ``` apply plugin: 'android' android { compileSdkVersion 19 buildToolsVersion "19.0.1" defaultConfig { minSdkVersion 7 targetSdkVersion 19 versionCode 1 versionName "1.0" } buildTypes { release { runProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } } dependencies { compile 'com.android.support:appcompat-v7:+' compile project(':libraries:facebook') } ``` Now, when I click on Sync Project with Gradle Files I get the following error: ``` Gradle 'Testing' project refresh failed: Project with path ':libraries:facebook' could not be found in project ':app'. ``` I have tried to change the path using: "libraries:facebook", "app:libraries:facebook", ":app:libraries:facebook"... but I'm always having the same error. EDIT WITH SOLUTION: With Android 0.4.2 and latest version of the Facebook SDK is really easy, Facebook SDK is including a build.gradle file that works, just only follow these steps: Create a folder called "libs" (important, don't use other name!!! If you use "lib" may not work), at the top of your project (important too, don't create in a subfolder!!!). Copy the facebook folder from the downloaded SDK into the libs folder you just created. Include this line at the top of your settings.gradle: include ':libs:facebook' Include this line at the bottom of your build.gradle file, in the dependencies group: compile project(':libs:facebook') Just click on "Sync Project with Gradle files", rebuild project and it should work! EDIT FOR ANDROID STUDIO > 0.5.2: Well, from Android Studio version 0.5.2, when a new project is created, a "libs" folder is created inside your project, so I think is a better idea to use that folder, so these are the steps: Copy the facebook folder from the downloaded SDK into the libs folder: YourProjectName/yourProjectName/libs Include this line at the top of your settings.gradle: include ':libs:facebook' Include this line at the bottom of your build.gradle file, in the dependencies group: compile project(':yourProjectName:libs:facebook') Just click on "Sync Project with Gradle files", rebuild project and it should work!