Version: 2017.4
AAR plug-ins and Android Libraries
Extendiendo el código Java UnityPlayerActivity

JAR plug-ins

JAR(Java Archive) plug-ins are primarily used to enable interaction with the Android OS or to call methods written in Java from within your C# scripts.

They can only contain Java code (for example, they can’t contain Android resources), which makes their use very limited. To add a JAR plug-in to your project, copy the .jar file into any of your project folders, then select it in Unity to open the Import Settings in the Inspector window. Tick the Android checkbox to mark this .jar file as compatible with Android:

JAR plug-in import settings as displayed in the Inspector window
JAR plug-in import settings as displayed in the Inspector window

Using Java plug-ins Unity uses the Java Native Interface (JNI) both when calling code from Java and when interacting with Java or the Java VM(Virtual Machine) from native code or C# scripts.

Using your Java plug-in from native (C/C++) code

Note: This information in this section requires advanced knowledge of the Android Java Native Interface (JNI).

To access your Java code from C or C++ plug-ins, you need access to the Java VM. Add the following method to your C/C++ code to access the Java VM.

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

It is beyond the scope of this document to explain JNI completely, but this method usually involves finding the class definition, resolving the constructor (<init>) method and creating a new object instance, as shown in this example:

jobject createJavaObject(JNIEnv* jni_env) {
  jclass cls_JavaClass = jni_env->FindClass("com/your/java/Class");         // find class definition
  jmethodID mid_JavaClass = jni_env->GetMethodID (cls_JavaClass, "<init>", "()V");      // find constructor method
  jobject obj_JavaClass = jni_env->NewObject(cls_JavaClass, mid_JavaClass);     // create object instance
  return jni_env->NewGlobalRef(obj_JavaClass);                      // return object with a global reference
}

For more information about JNI, see