Need help creating a Java plugin

I could use some beginner’s help how to create a Java plugin for Unity. Basically I just want to display some text from a Java method in the Unity scene to see if the plugin works.

I’ve followed the tutorial located at http://unity3d.com/support/documentation/Manual/Plugins.html but having problems with succeeding.

So far I’ve created a JAR file which contains a class with some very simple methods such as

public class ExampleLibrary {

	public String helloWorld() {
		return "Hello world!";
	}

}

Then I’ve added the JAR to the Assets->Plugins->Android path in Unity. The tutorial says next that I need to:

If I add the mentioned code to my MyJavaPlugin.cs file I get an error in Unity.

And here’s how MyJavaPlugin.cs looks like:

using UnityEngine;
using System.Collections;

public class MyJavaPlugin : MonoBehaviour {

	public ExampleLibrary() {
	}
	
	jint JNI_OnLoad(JavaVM* vm, void* reserved) {
  		JNIEnv* jni_env = 0;
  		vm->AttachCurrentThread(&jni_env, 0);
	} 
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

Do I need to import anything into the .cs script or how can I get rid of that error message?

Thanks for help.

From the doc :

The following code

jint JNI_OnLoad(JavaVM* vm, void* reserved) {
  		JNIEnv* jni_env = 0;
  		vm->AttachCurrentThread(&jni_env, 0);
	}

Supposedly only for c++ in android ndk, you need to setup your android ndk environment to build the native code from the cygwin, Installation video, although in german just follow the steps.

Based on the docs there is 2 ways to make android plugins, if you goes with java route you need also write the native code interface which bridge between java and c# (sample), if you using just plain native code you could use it directly on c# (sample).

Some other note

Might be useful if you wanted to hide your serial algorithm not for frequent used function calls.

Thanks for the information so far. I already got the Android NDK setup and can build native code with cygwin.

So basically I need to make an Android application with that? In a different forum post I was told that I can not use Android applications (APK files) in Unity.

It does not have to be an Android plugin. A simple Java plugin is enough for the time being. Do I still need to use the Android NDK for that?