简体   繁体   English

Android Studio:无法使用Facebook SDK

[英]Android Studio: Cannot get Facebook SDK working

I have been trying to figure this out for a couple of days but I give up. 我已经尝试了几天,但我放弃了。

Here are the errors I am getting: 这是我遇到的错误:

Error:(9, 1) error: package com.facebook does not exist
Error:(10, 1) error: package com.facebook.model does not exist
Error:(11, 20) error: package com.facebook does not exist
Error:(21, 58) error: package Session does not exist
Error:(21, 9) error: cannot find symbol variable Session
Error:(48, 9) error: cannot find symbol variable Session

I am using Android Studio 0.58 and Facebook SDK 3.14 我正在使用Android Studio 0.58和Facebook SDK 3.14

I downloaded the facebook SDK, extracted it, then went to File > Import Module and selected the "Facebook" module. 我下载了facebook SDK,将其解压缩,然后转到“文件”>“导入模块”并选择“ Facebook”模块。 Did not help. 没有帮助。

I took the jar file and put it in my libs folder. 我拿了jar文件,并将其放在我的libs文件夹中。 Clean. 清洁。 Sync. 同步。 No help. 没有帮助

I added to my build.gradle (as suggested by many searches). 我添加到了build.gradle(许多搜索都建议)。 Did a sync. 进行了同步。 I checked and the "android-support-v4.jar" is in my dependencies in the Project Structure. 我检查了一下,“ android-support-v4.jar”位于我在项目结构中的依赖项中。 Still no help. 仍然没有帮助。

Here is my build.gradle: 这是我的build.gradle:

apply plugin: 'android'

android {
compileSdkVersion 19
buildToolsVersion '19.0.3'

defaultConfig {
    minSdkVersion 9
    targetSdkVersion 19
    versionCode 6
    versionName "0.3.0"
}
signingConfigs{
    release {
        storeFile file("path")
        storePassword "password"
        keyAlias "alias"
        keyPassword "password"
    }
buildTypes {
    release {
        runProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        debuggable false
        signingConfig signingConfigs.release
        zipAlign true
        }
    }
}
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:19.+'
    compile 'com.google.android.gms:play-services:+'
    compile files('libs/libGoogleAnalyticsServices.jar')
    compile files('libs/android-support-v4.jar')
}

Here is my Android Manifest.xml 这是我的Android Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.understandingyourbody.uyb" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
     >
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"
         />
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>


    <activity
        android:name="com.understandingyourbody.uyb.MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.understandingyourbody.uyb.WordPress"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.WORDPRESS" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.understandingyourbody.uyb.Facebook"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.FACEBOOK" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

</application>

And here is my Java 这是我的Java

package com.understandingyourbody.uyb;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;

import com.facebook.*;
import com.facebook.model.*;
import com.facebook.Session;

public class Facebook extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.facebook);

    // start Facebook Login
    Session.openActiveSession(this, true, new Session.StatusCallback() {

        // callback when session changes state
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if (session.isOpened()) {

                // make request to the /me API
                Request.newMeRequest(session, new Request.GraphUserCallback() {

                    // callback after Graph API response with user object
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            TextView welcome = (TextView) findViewById(R.id.welcome);
                            welcome.setText("Hello " + user.getName() + "!");
                        }
                    }
                }).executeAsync();
            }
        }
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}

}

I found the answer here: https://stackoverflow.com/a/23577782/3554758 我在这里找到了答案: https : //stackoverflow.com/a/23577782/3554758

I was adding an "Android Library" instead of choosing "Import Existing Prohect" It works great now 我添加的是“ Android库”,而不是选择“导入现有产品”,现在效果很好 在此处输入图片说明

When importing the module, don't forget to add it as a dependency: 导入模块时,请不要忘记将其添加为依赖项:

dependencies {
    compile project(':facebooksdk') // The name of the module.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM