Java Native Interface APIs in Unity
Learn about the Java Native Interface APIs in Unity that you can use to call Java/Kotlin code from C# scripts.
Read time 2 minutesLast updated 7 days ago
Unity provides low-level and high-level Java Native Interface (JNI) APIs that allow you to interact with Java code from C# scripts.
Low-level API
The low-level AndroidJNI class wraps JNI calls and provides static methods that directly map to JNI methods. The AndroidJNIHelper API provides helper functionality that's primarily used by the high-level API, but they can be useful in certain situations.
High-level API
The high-level AndroidJavaObject, AndroidJavaClass, and AndroidJavaProxy classes automate a lot of tasks required for JNI calls. They also use caching to make calls to Java faster. The combination of and is built on top of and , but they also contain additional functionality such as static methods that you can use to access static members of Java classes.
AndroidJavaObjectAndroidJavaClassAndroidJNIAndroidJNIHelperAdditionally, Unity provides the AndroidApplication class to simplify access to instances of , , and for your application. This class also allows you to delegate code execution on the UI or main thread based on your application's requirement.
currentActivitycurrentContextcurrentConfigurationInstances of and have a one-to-one mapping to an instance of java.lang.Object and java.lang.Class respectively. They provide three types of interactions with Java/Kotlin code:
AndroidJavaObjectAndroidJavaClassEach interaction also has a static version:
- CallStatic to call a static method.
- GetStatic to get the value of a static field.
- SetStatic to set the value of a static field.
When you get the value of a field or call a method that returns a value, you use generics to specify the return type. When you set the value of a field, you also use generics to specify the type of the field that you're setting. For methods that don't return a value, there's a regular, non-generic version of Call.