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!
What is the Facebook SDK? The Facebook SDK is what allows mobile app developers to integrate Facebook within a mobile app. SDK stands for software development kit, and it allows for a website or app to integrate with Facebook seamlessly.
Select the App you want to upgrade (you might have access to multiple apps) Click Settings > Advanced. Select the latest version under the Upgrade API Version section for both “Upgrade All Calls” and “Upgrade Calls for App Roles” Save.
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
Use v0.7.+ of the Gradle plugin, as you've already done, and you want to make sure you're using v1.9 of Gradle (not 1.10, which is incompatible). If your project is using the Gradle wrapper (which is what I'd recommend), you can check the Gradle version which is embedded in the distributionUrl
parameter in the gradle/wrapper/gradle-wrapper.properties file.
If you upgrade to Android Studio 0.4.2 it should check versions when you open the project and tell you how to correct problems if they exist.
EDIT
To fix your problem with
Build script error, unsupported Gradle DSL method found: 'include()'!
you need to correct your dependencies. To include another module as a dependency, use this:
compile project(':libraries:facebook')
If you go through the Project Structure dialog instead of editing the build.gradle files by hand, it can do it properly for you (though it won't fix the incorrect statement that uses include
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With