Export Cardboard app from Unity and change android side code

Hi all,
I’m trying to modify a sample app from the Google Cardboard SDK (for VR development)
Using Unity 4.6 and Android Studio / Eclipse

My goal is to make modifications, or for this matter a simple log on different events of the app life cycle (i.e OnCreate(), OnPause() ect.).

In the exported project from Unity I find in the src directory the following file com.company.app.UnityPlayerNativeActivity
This file has all the application lifecycle methods and the basic calls which load the Unity project.
This is part of the code, as you can see, this is where I want to make the log statements:

public class UnityPlayerNativeActivity extends NativeActivity
{
    protected UnityPlayer mUnityPlayer;        // don't change the name of this variable; referenced from native code

    // Setup activity layout
    @Override protected void onCreate (Bundle savedInstanceState)
    {
        Log.d("Hello","World"); // my try to log something
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        getWindow().takeSurface(null);
        setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
        getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy

        mUnityPlayer = new UnityPlayer(this);
        if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
            getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
                                   WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(mUnityPlayer);
        mUnityPlayer.requestFocus();
    }

    // Quit Unity
    @Override protected void onDestroy ()
    {
        mUnityPlayer.quit();
        super.onDestroy();
    }

    // Pause Unity
    @Override protected void onPause()
    {
        super.onPause();
        mUnityPlayer.pause();
    }

The thing is that the project manifest is pointing to a different location,

<activity android:name="com.google.vrtoolkit.cardboard.plugins.unity.UnityCardboardActivity" >

I have noticed that in the Unity project, the Cardboard SDK has a Assets/Plugins/Android
UnityCardboardActivity jar file. I believe this is the file which is called by the Manifest. As it’s the only file in the project with this name.

If I change the manifest to point to the src file com.company.app.UnityPlayerNativeActivity,

<activity android:name="com.company.app.UnityPlayerNativeActivity" >

the code will run and I will be able to log, but the Android app will crash once trying to load the Unity project with the following error:

5828/com.company.app I/Unity﹕ Exception: JNI: Init’d AndroidJavaObject with null ptr!
at UnityEngine.AndroidJavaObject…ctor (IntPtr jobject) [0x00000] in :0
at UnityEngine.AndroidJavaObject.AndroidJavaObjectDeleteLocalRef (IntPtr jobject) [0x00000] in :0
at UnityEngine.AndroidJavaObject._CallStatic[AndroidJavaObject] (System.String methodName, System.Object[ ] args) [0x00000] in :0
at UnityEngine.AndroidJavaObject.CallStatic[AndroidJavaObject] (System.String methodName, System.Object[ ] args) [0x00000] in :0
at Cardboard.ConnectToActivity () [0x00000] in :0
at Cardboard.Awake () [0x00000] in :0
(Filename: Line: -1)

Any help would be appreciated,
Thanks in advance.

This error is coming from the script Cardboard.cs, because it is trying to get a reference to the UnityCardboardActivity class, which is a wrapper around the Cardboard SDK for Java as well as the UnityEngine. This class already does most of the logic you included in your code snippet above. Try changing your class to extend from UnityCardboardActivity rather than NativeActivity, and just override methods where you want to log something. Keep your modified manifest, but there is also a constant string in Cardboard.cs which tells it what class to look for. Change that to your class as well.

I’ve made the change you offered,
Changing the hardcoded part in the cardboard.cs script (in Unity) and then re-exported the project.
Made the change to the manifest so I can see my logs,
Whereas in the Java part (in Android) I’ve changed the activity to extend CardboardNativeActivity instead of UnityCardboardActivity (unrecognised) .

But the crash remains. The sequence of OnCreate() finish without a problem, it’s the Unity player that crushes.

Here’s the print from logcat:

com.company.test E/Unity﹕ Cannot access UnityCardboardActivity. Verify that the jar is in Assets/Plugins/Android. UnityEngine.AndroidJavaException: java.lang.NoSuchMethodError: no static method “Lcom/company/test/UnityPlayerNativeActivity;.getActivity()Ljava/lang/Object;”
at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in :0
at UnityEngine.AndroidJNISafe.GetStaticMethodID (IntPtr clazz, System.String name, System.String sig) [0x00000] in :0
at UnityEngine._AndroidJNIHelper.GetMethodID (IntPtr jclass, System.String methodName, System.String signature, Boolean isStatic) [0x00000] in :0
at UnityEngine.AndroidJNIHelper.GetMethodID (IntPtr javaClass, System.String methodName, System.String signature, Boolean isStatic) [0x00000] in :0
at UnityEngine._AndroidJNIHelper.GetMethodID[AndroidJavaObject] (IntPtr jclass, System.String methodName, System.Object[ ] args, Boolean isStatic) [0x00000] in :0
at UnityEngine.AndroidJNIHelper.Ge
/com.company.test A/libc﹕ Fatal signal 11 (SIGSEGV) in tid 6255 (UnityMain)

The message "Cannot access UnityCardboardActivity. Verify that the jar is in Assets/Plugins/Android." comes from Cardboard.ConnectToActivity(), so it is still saying that it can’t find the class. You do need to extend UnityCardboardActivity to get all the functions that it provides to Cardboard.cs, or none of the head tracking or distortion correction functions will work. You said it was unrecognized? Make sure to include the unitycardboardactivity.jar file in your Java build’s classpath or library list, and you should be able to extend it. Same with vrtoolkit.jar, which has classes used by UnityCardboardActivity.