JNI Exception in the editor [Android] Unity 4.2

Hello,

So I just updated to Unity 4.2 and I now get this error in my console when running my game:

Exception: JNI: Init’d AndroidJavaClass with null ptr!

UnityEngine.AndroidJavaClass…ctor (IntPtr jclass) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/Export/AndroidJavaImpl.cs:533)

Now I ignored this error at first because I assumed it was because I’m not on an Android device. But because of this error my events don’t work so I can’t truely test my game without exporting it.

Let me note that this error did NOT happen on Unity 4.1, I did no change in my code, just updated Unity.

This is the code that is cuasing the error:

    #if UNITY_ANDROID
        AdClass = new AndroidJavaClass("com.package.name.Ads");	
    #endif

AdClass is declared as followed

    public static AndroidJavaClass AdClass;

When I build my APK and put it on my android device there are no problems. So this is just happening in the editor. Like stated before I can’t ignore this error because it stops all my events so my game doesn’t run properly.

Anyone have any suggestions on how I can get rid of this error?

I had the same problem and I used this:

 #if UNITY_ANDROID && !UNITY_EDITOR
      AdClass = new AndroidJavaClass("com.package.name.Ads"); 
 #endif

I hope it’s works for you.

So this isn’t a solution but it is a work around:

I added this around my JavaClass calls:

#if UNITY_EDITOR
	ignoreAdClass = true;
#endif
	
	
#if UNITY_ANDROID
	if(!ignoreAdClass){
		AdClass = new AndroidJavaClass("com.package.name.Ads");	
	}
#endif

And I declared ignoreAdClass as false initially.

(Any real solution rather than a work around would still be greatly appreciated.

How about using this code

if(Application.platform == RuntimePlatform.Android)
// do something in android platform

UNITY_ANDROID will be defined if the Android platform is selected in the Build Settings menu.

In order to ignore this code inside the editor, you can check use an extra #if:

#if UNITY_EDITOR
    // do nothing ?
#elif UNITY_ANDROID
        AdClass = new AndroidJavaClass("com.package.name.Ads");    
#endif