4

I've pretty much followed this guide to the letter, however, when the application is launched and the Facebook session should be created the app crashes. I'm thinking the libraries aren't being included in the build...

package com.firstandroidapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;
import com.facebook.*;
import com.facebook.model.*;

public class MainActivity extends Activity {

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

    // 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.executeMeRequestAsync(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() + "!");
              }
            }
          });
        }
      }
    });
  }

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

}

Here's the exception stack:

10-16 14:13:30.627: E/AndroidRuntime(28793): FATAL EXCEPTION: main
10-16 14:13:30.627: E/AndroidRuntime(28793): java.lang.NoClassDefFoundError: android.support.v4.content.LocalBroadcastManager
10-16 14:13:30.627: E/AndroidRuntime(28793):    at com.facebook.Session.postActiveSessionAction(Session.java:1327)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at com.facebook.Session.setActiveSession(Session.java:790)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at com.facebook.Session.openActiveSession(Session.java:890)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at com.facebook.Session.openActiveSession(Session.java:830)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at com.mumin.facebookconnect.FBAuth.onCreate(FBAuth.java:21)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at android.app.Activity.performCreate(Activity.java:5104)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at android.os.Looper.loop(Looper.java:137)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at android.app.ActivityThread.main(ActivityThread.java:5195)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at java.lang.reflect.Method.invokeNative(Native Method)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at java.lang.reflect.Method.invoke(Method.java:511)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
10-16 14:13:30.627: E/AndroidRuntime(28793):    at dalvik.system.NativeStart.main(Native Method)
10-16 14:13:38.175: I/Process(28793): Sending signal. PID: 28793 SIG: 9
MK3GTX
  • 241
  • 4
  • 13

2 Answers2

14

Just replace

     YourProject\lib\android-support-v4.jar 

with

     facebook-android-sdk-x.xx.x\libs\android-support-v4.jar 

where x.xx.x is the facebook sdk version, e.g. 3.21.0

wwkudu
  • 2,778
  • 3
  • 28
  • 41
user2968230
  • 174
  • 2
  • 7
0

I encountered the same issue when trying to run my app on Android versions before lollipop. After hours of searching and playing with libraries, I figured it was just because of the 65k function limit on versions before lollipop.

So before trying the above solution or any other solution, I recommend moving the facebook sdk initialize call later in the code and not in the onCreate method. In this way, if the issue was because of the 65k limit, you might get a NoClassDef found exception for another class.

To fix the 65k limit issue, just go through your libraries in build.gradle and remove any unused libraries. Also use just what is needes from the Google apis, especially Google play services. It will also help cleanup your app and reduce file size.