using facebook sdk in Android studio – Android

Photo of author
Written By M Ibrahim
android android-gradle-plugin android-studio

Quick Fix: In Android studio, create a ‘libraries’ folder under the project’s main directory and copy Facebook SDK’s ‘facebook’ directory into it. Delete the ‘libs’ folder and create a ‘build.gradle’ file in the ‘facebook’ directory with specified contents. Edit ‘settings.gradle’ and add ‘:libraries:facebook’ in it. Sync the project with Gradle files, open Project Structure dialog, and add ‘:libraries:facebook’ as a dependency.

The Problem:

"You’re getting a warning message "Gradle: module ‘facebook’ won’t be compiled. Unfortunately you can’t have non-Gradle Java module and Android-Gradle module in one project." when integrating the Facebook SDK with Android Studio. Additionally, when following a suggested solution, you’re encountering an error message "Failed to notify project evaluation listener. > 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:+."
}

The Solutions:

Solution 2: Using Maven Central Repositories for Facebook SDK

To resolve the issue and successfully use the Facebook SDK in your Android Studio project, follow these steps:

  1. Update the Top-Level build.gradle File:

    • Open the build.gradle file located in the root directory of your project.

    • Inside the repositories block, add the following line:

      mavenCentral()
      

    This line adds the Maven Central repositories to your project, allowing you to access the Facebook SDK.

  2. Add the Facebook SDK Dependency:

    • Open the app-level build.gradle file located in the app directory of your project.

    • Inside the dependencies block, add the following line:

      compile 'com.facebook.android:facebook-android-sdk:4.5.0'
      

    Replace "4.5.0" with the desired version of the Facebook SDK. You can find a list of available versions here.

  3. Adjust the Facebook SDK Version (Optional):

    • If you want to use a different version of the Facebook SDK, you can adjust the version number in the compile line. Make sure to use a compatible version with your project’s requirements.
  4. Rebuild the Project:

    • After making these changes, rebuild your project by clicking on the "Build" menu and selecting "Rebuild Project".

By following these steps, you should be able to successfully integrate the Facebook SDK into your Android Studio project using the Maven Central repositories.

Solution 3: Add facebook SDK library to app’s build.gradle

To add the Facebook SDK to your Android Studio project, do the following steps:

  1. Add the Facebook SDK library to your app’s build.gradle file.
    dependencies {
         compile 'com.facebook.android:facebook-android-sdk:+'
    }
    
  2. Implement the Facebook SDK.
    // Add the Facebook SDK to your AndroidManifest.xml.
    <manifest ... >
        <application ... >
            <meta-data
                android:name="com.facebook.sdk.ApplicationId"
                android:value="@string/facebook_app_id" />
            ...
        </application>
    </manifest>
    

    // Initialize the Facebook SDK. FacebookSdk.sdkInitialize(getApplicationContext());

    // Create a callback manager. CallbackManager callbackManager = CallbackManager.Factory.create();

    // Create a login button. LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);

    // Set the login permissions. loginButton.setReadPermissions("email", "public_profile");

    // Register a callback for the login button. loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { // Handle the login success. }

    @Override
    public void onCancel() {
        // Handle the login cancellation.
    }
    
    @Override
    public void onError(FacebookException error) {
        // Handle the login error.
    }
    

    });

  3. Test the Facebook SDK.
    // Run the app.
    // Click the login button.
    // The app will prompt you to log in with your Facebook account.
    // After you log in, the app will display your profile information.
    

Solution 4: Using Android Studio libraries

To solve the issue of non-Gradle Java module and Android-Gradle module in one project, follow these steps:

  1. In the main project’s build.gradle file, add the following dependencies:

    compile files('libs/android-support-v4.jar')
    compile project(':libraries:facebook')
    
  2. Ensure that the following lines in both build.gradle files are identical:

    android {
        compileSdkVersion 18
        buildToolsVersion "18.1.1"
    
        defaultConfig {
            minSdkVersion 7
            targetSdkVersion 18
        }
    }
    

By implementing these steps, you can resolve the conflict between non-Gradle Java and Android-Gradle modules and successfully run your application.

\n

Solution 5: Using Git to Include Newest SDK

\n

To resolve the issue and incorporate the latest facebook-android-sdk, you can follow these steps:

1. Add facebook-android-sdk as a Submodule:

From your Android Studio project directory, open the Terminal window.
Run the following command to add facebook-android-sdk as a submodule:
git submodule add https://github.com/facebook/facebook-android-sdk.git

2. Add SDK as a Gradle Project:

Open the settings.gradle file located in the root directory of your Android Studio project.
Add the following line to include the facebook-android-sdk as a Gradle project:
include ':facebook-android-sdk:facebook'

3. Add SDK as a Dependency to Module:

Open the build.gradle file for your app module.
Within the dependencies block, add the following line to specify facebook-android-sdk as a dependency:
compile project(':facebook-android-sdk:facebook')

By following these steps, you can incorporate the latest facebook-android-sdk into your Android Studio project using Git. This should resolve the warning related to 'facebook' not being compiled and allow you to use the SDK in your application.