Table of Contents

Class JNIExtensions

QOL extensions for the JNI.

public static class JNIExtensions
Inheritance
object
JNIExtensions

Remarks

This class is public to allow package users to reuse these extensions if useful. However, it should not be considered stable and is not part of the supported public API. It exists solely as an internal utility and may change or be removed at any time.

Methods

JavaListAsManagedArray<T>(AndroidJavaObject)

Converts a java.util.List to a managed array.

public static T[] JavaListAsManagedArray<T>(this AndroidJavaObject current)

Parameters

current AndroidJavaObject

Returns

T[]

The converted array.

Type Parameters

T

The type of the elements.

Remarks

WARNING: This method DOES NOT verify if the native Java/Kotlin elements can actually be converted to the target type.

Supports converting elements to the types supported by ToManaged<T>(AndroidJavaObject) + AndroidJavaObject.

ToJava(object, Type)

Converts managed .NET/Unity object to a Java/Kotlin object represented by an AndroidJavaObject.

[SuppressMessage("Style", "IDE0046:Convert to conditional expression", Justification = "Code is neater without conditionals.")]
public static AndroidJavaObject ToJava(this object current, Type target)

Parameters

current object
target Type

The type of the managed object.

Returns

AndroidJavaObject

The converted Java object.

Remarks

Basic supported types:

  • bool
  • char
  • sbyte
  • short
  • int
  • long
  • float
  • double
  • string

Supported Android types:

Unity TypeAndroid Type
Resolutionandroid.util.Size
Rectandroid.graphics.RectF
RectIntandroid.graphics.Rect
CameraMetadata.IntRangeandroid.util.Range<Integer>
CameraMetadata.LongRangeandroid.util.Range<Long>
CameraMetadata.FloatRangeandroid.util.Range<Float>

Also supports converting arrays of all above types + generic AndroidJavaObject[] and AndroidJavaProxy[].

Exceptions

NotSupportedException

Thrown when the type is unsupported.

ToJava<T>(T)

Converts managed .NET/Unity object to a Java/Kotlin object represented by an AndroidJavaObject.

public static AndroidJavaObject ToJava<T>(this T current) where T : notnull

Parameters

current T

Returns

AndroidJavaObject

The converted Java object.

Type Parameters

T

The type of the managed object.

Remarks

Basic supported types:

  • bool
  • char
  • sbyte
  • short
  • int
  • long
  • float
  • double
  • string

Supported Android types:

Unity TypeAndroid Type
Resolutionandroid.util.Size
Rectandroid.graphics.RectF
RectIntandroid.graphics.Rect
CameraMetadata.IntRangeandroid.util.Range<Integer>
CameraMetadata.LongRangeandroid.util.Range<Long>
CameraMetadata.FloatRangeandroid.util.Range<Float>

Also supports converting arrays of all above types + generic AndroidJavaObject[] and AndroidJavaProxy[].

Exceptions

NotSupportedException

Thrown when the type is unsupported.

ToManaged(AndroidJavaObject, Type)

Converts a Java/Kotlin object represented by an AndroidJavaObject into a managed .NET/Unity type.

[SuppressMessage("Style", "IDE0046:Convert to conditional expression", Justification = "Code is neater without conditionals.")]
public static object ToManaged(this AndroidJavaObject current, Type target)

Parameters

current AndroidJavaObject
target Type

The managed target type.

Returns

object

The converted managed object.

Remarks

WARNING: This method DOES NOT verify if the native Java/Kotlin object can actually be converted to the target type.

Basic supported types:

  • bool
  • char
  • sbyte
  • short
  • int
  • long
  • float
  • double
  • string

Supported Android types:

Unity TypeAndroid Type
Resolutionandroid.util.Size
Rectandroid.graphics.RectF
RectIntandroid.graphics.Rect
CameraMetadata.IntRangeandroid.util.Range<Integer>
CameraMetadata.LongRangeandroid.util.Range<Long>
CameraMetadata.FloatRangeandroid.util.Range<Float>

Also supports converting arrays of all above types + generic AndroidJavaObject[].

Exceptions

NotSupportedException

Thrown when the requested target type is unsupported.

ToManaged<T>(AndroidJavaObject)

public static T ToManaged<T>(this AndroidJavaObject current)

Parameters

current AndroidJavaObject

Returns

T

Type Parameters

T

The managed target type.

TryConvertToManaged<T>(AndroidJavaObject, out T?)

public static bool TryConvertToManaged<T>(this AndroidJavaObject current, out T? value)

Parameters

current AndroidJavaObject
value T

Returns

bool

Type Parameters

T

UnboxBoolElement(nint, int)

Unboxes a boolean from a native Object array.

public static bool UnboxBoolElement(nint args, int index)

Parameters

args nint

The native array to take the boolean from.

index int

The index of the boolean object in the native array.

Returns

bool

The unboxed boolean.

UnboxByteBufferElement(nint, int)

Retrieves a ByteBuffer object from a native object array and obtains its direct memory address.

public static (nint obj, nint ptr) UnboxByteBufferElement(nint args, int index)

Parameters

args nint

JNI object array containing the ByteBuffer.

index int

Index of the ByteBuffer within the array.

Returns

(nint obj, nint ptr)

A tuple containing the local JNI reference to the ByteBuffer, and the pointer to its direct buffer memory.

UnboxIntElement(nint, int)

Unboxes an integer from a native Object array.

public static int UnboxIntElement(nint args, int index)

Parameters

args nint

The native array to take the integer from.

index int

The index of the integer object in the native array.

Returns

int

The unboxed integer.

UnboxLongElement(nint, int)

Unboxes a long from a native Object array.

public static long UnboxLongElement(nint args, int index)

Parameters

args nint

The native array to take the long from.

index int

The index of the long object in the native array.

Returns

long

The unboxed long.

UnboxObjectElement(nint, int)

Unboxes an AndroidJavaObject from a native Object array.

public static AndroidJavaObject UnboxObjectElement(nint args, int index)

Parameters

args nint

The native array to take the object from.

index int

The index of the object in the native array.

Returns

AndroidJavaObject

The unboxed object.