Custom plugin for facebook: app crash, warning about portrait mode?

Hi,

I would like to connect my facebook project to Unity via a plugin. The project in Eclipse works fine, without Unity, there are no issues. But in Unity, there is a warning and the app crashes :

In Player Settings in Unity, the orientation is in “portrait”. In the manifest also :

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.firstandroidapp"> 
  <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
		android:screenOrientation="portrait" >
        <activity
            android:name="com.firstandroidapp.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>
        <meta-data android:value="@string/app_id" android:name="com.facebook.sdk.ApplicationId"/>
        <activity android:name="com.facebook.LoginActivity"></activity>
    </application>
</manifest>

And the code from Eclipse : I pasted the “res” folder under Plugins > Android to avoid any errors with the R class, and added getIdentifier in the mainActivity : (but here, I only call testFunc() from unity ) :

public class MainActivity extends UnityPlayerActivity {
	
	public static Context ctx;
	private LoginButton loginButton;
    private ProfilePictureView profilePictureView;
    private TextView greeting;
    private GraphUser user;
	
	private UiLifecycleHelper uiHelper;
    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };
    
    public void testFunc(){
    	Log.i("test func","test func");
    }
    
    public String getStr(String n) {
        return getString(getResources().getIdentifier( n, "string", getPackageName()));
    }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ctx = this;
    uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(savedInstanceState);
	//setContentView(R.layout.activity_main);
	int login_int = getResources().getIdentifier("login", "id", getPackageName());
	loginButton = (LoginButton) findViewById(login_int);
	loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
        @Override
        public void onUserInfoFetched(GraphUser user) {
            MainActivity.this.user = user;
            updateUI();
        }
    });
    int greeting_int = getResources().getIdentifier("greeting", "id", getPackageName());
    greeting = (TextView) findViewById(greeting_int);
  }
  
  private void onSessionStateChange(Session session, SessionState state, Exception exception) {
      updateUI();
  }
  
  private void updateUI() {
      Session session = Session.getActiveSession();
      boolean enableButtons = (session != null  session.isOpened());

      if (enableButtons  user != null) {
    	  greeting.setText("hello : "+user.getFirstName());
      } else {
          greeting.setText(null);
      }
  }
  
  @Override
  protected void onResume() {
      super.onResume();
      uiHelper.onResume();
      updateUI();
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      uiHelper.onActivityResult(requestCode, resultCode, data, dialogCallback);
  }
  private FacebookDialog.Callback dialogCallback = new FacebookDialog.Callback() {
      @Override
      public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
          Log.d("HelloFacebook", String.format("Error: %s", error.toString()));
      }

      @Override
      public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
          Log.d("HelloFacebook", "Success!");
      }
  };

  @Override
  public void onPause() {
      super.onPause();
      uiHelper.onPause();
  }

  @Override
  public void onDestroy() {
      super.onDestroy();
      uiHelper.onDestroy();
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      uiHelper.onSaveInstanceState(outState);
  }

}

and the code in Unity :

if(GUI.Button(new Rect(20,250,150,150), "TEST")) {
	Debug.Log("hello facebook");
	using (AndroidJavaClass javaClass = new AndroidJavaClass("com.firstandroidapp.MainActivity"))
	{
		using (AndroidJavaObject activity = javaClass.GetStatic<AndroidJavaObject>("ctx"))
		{
			activity.Call("testFunc");
		}
	}
}

I know a plugin for facebook exists, but I had too many errors I could not fix, so I would like to try to create my own plugin (I only look for simple actions, such as connect and post a status).

Thanks for your help

It’s possible something you copied from the “res” folder overwrite specific settings when compiling on Unity. For example drawable icons from “res” folder can overwrite default icons on Unity. Not sure with the behaviour from layouts and similar files. What happens on the Android Plugins folder when compiling on Unity is poorly documented.

Double check the content of the res folder you copied on Android/Plugins folder. Delete the res folder and try if you don’t receive this warning.

In case of doubt your Android Manifest is modified by Unity when compiling you need to decompile your apk with apktool to view your final Android Manifest in a readable format.

Thanks, the manifest file decoded by apktool looks the same : (only the value of the meta data has changed, but maybe it is written in a different format (it is shorter than the one I entered) :

<?xml version="1.0" encoding="utf-8"?>
<manifest android:versionCode="1" android:versionName="1.0" android:installLocation="preferExternal" package="com.firstandroidapp"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:debuggable="false" android:screenOrientation="portrait" android:configChanges="locale|mcc|mnc|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale" android:allowBackup="true">
        <activity android:label="MyApp" android:name="com.firstandroidapp.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="theNumber" />
        <activity android:name="com.facebook.LoginActivity" />
    </application>
    <uses-feature android:glEsVersion="0x20000" />
</manifest>

I removed the “res” folder from Plugins/Android folder and I entered the value directly in the manifest file in Unity, instead of @string/… . In Unity, the warning is the same as before, with the message about “portrait” mode. In Eclipse, I get more errors :

01-03 23:37:21.025: E/dalvikvm(14429): Could not find class 'android.os.UserManager', referenced from method aod.b
01-03 23:37:21.026: E/dalvikvm(14429): Could not find class 'android.os.UserManager', referenced from method aod.c
01-03 23:37:21.045: E/dalvikvm(14429): Could not find class 'android.os.UserManager', referenced from method aod.d
01-03 23:37:21.339: E/dalvikvm(14429): Could not find class 'android.app.AppOpsManager', referenced from method axp.a
01-03 23:37:22.157: E/dalvikvm(14501): Could not find class 'com.firstandroidapp.MainActivity$1', referenced from method com.firstandroidapp.MainActivity.<init>
01-03 23:37:22.162: E/dalvikvm(14501): Could not find class 'com.facebook.UiLifecycleHelper', referenced from method com.firstandroidapp.MainActivity.onCreate
01-03 23:37:22.171: E/AndroidRuntime(14501): FATAL EXCEPTION: main
01-03 23:37:22.171: E/AndroidRuntime(14501): java.lang.NoClassDefFoundError: com.firstandroidapp.MainActivity$1
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at com.firstandroidapp.MainActivity.<init>(MainActivity.java:27)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at java.lang.Class.newInstanceImpl(Native Method)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at java.lang.Class.newInstance(Class.java:1319)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2011)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2124)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at android.app.ActivityThread.access$600(ActivityThread.java:135)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1248)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at android.os.Looper.loop(Looper.java:137)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at android.app.ActivityThread.main(ActivityThread.java:4645)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at java.lang.reflect.Method.invokeNative(Native Method)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at java.lang.reflect.Method.invoke(Method.java:511)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
01-03 23:37:22.171: E/AndroidRuntime(14501): 	at dalvik.system.NativeStart.main(Native Method)
01-03 23:37:50.792: E/dalvik-internals(14568): Failed to look up ladDumpProfiles

Would you know how to debug this?

Your package name will not be allowed to upload on Google Play, you must follow the naming convention.

Also, check your plugins and libraries are linked correctly on Eclipse and Unity.

OK for the package name.
About the library, maybe I am doing something wrong with Unity. The libraries work fine without Unity. Here are some images :

1472135--80990--$a.png

1472135--80991--$aa.png

1472135--80992--$aaa.png

When I click on the checkbox in Order and Export next to classes.jar, it adds more errors.

In Unity, I only copy the .jar from the bin folder under Plugins/Android, and change the Manifest file accordingly. Is there anything else I could check?