AndroidJavaClass(string)
Construct an AndroidJavaClass from the class name.
Read time 1 minuteLast updated 4 days ago
Definition
- Type: Constructor
- Namespace: UnityEngine
- Assembly: UnityEngine.AndroidJNIModule
public AndroidJavaClass(string className)
Parameters
Specifies the Java class name (e.g. \<tt\>java.lang.String\</tt\>).
Remarks
This essentially means locate the class type and allocate a \<tt\>java.lang.Class\</tt\> object of that particular type. It allows you to access static properties and methods of the Java/Kotlin classes without creating an instance or allocating additional memory.
The following code example demonstrates the use of constructor for interacting with Java code. It shows how to access a static property from a Java class to retrieve the maximum possible value of an integer.
AndroidJavaClassusing UnityEngine;public class AccessJavaLangIntegerStatic : MonoBehaviour{ void Start() { var integerClass = new AndroidJavaClass("java.lang.Integer"); int maxValue = integerClass.GetStatic<int>("MAX_VALUE"); Debug.Log("java.lang.Integer.MAX_VALUE: " + maxValue); }}
The following code examples demonstrate how to use the constructor to interact with Kotlin code. The following Kotlin example defines a static method to compute the hash code of a string.
AndroidJavaClassobject KotlinStringHelper { @JvmStatic fun getStringHashCode(inputString: String?): Int { return inputString?.hashCode() ?: 0 }}
The following C# example shows how to call the Kotlin method in Unity.
using UnityEngine;public class KotlinExamples{ public static int GetKotlinStringHashCode(string text) { //Use Kotlin object name using (AndroidJavaClass kotlinClass = new AndroidJavaClass("KotlinStringHelper")) { int hash = kotlinClass.CallStatic<int>("getStringHashCode", text); return hash; } }}