Index · Object → JavaClassWrapper

JavaClassWrapper

Provides access to the Java Native Interface.

Class Description

The JavaClassWrapper singleton provides a way for the Godot application to send and receive data through the Java Native Interface (JNI).

Note: This singleton is only available in Android builds.

var LocalDateTime = JavaClassWrapper.wrap("java.time.LocalDateTime")
var DateTimeFormatter = JavaClassWrapper.wrap("java.time.format.DateTimeFormatter")

var datetime = LocalDateTime.now()
var formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss")

print(datetime.format(formatter))

Warning: When calling Java methods, be sure to check get_exception() to check if the method threw an exception.

Methods

func create_proxy(object: Object, interfaces: PackedStringArray) -> JavaObject

Creates a JavaObject implementing the given Java interfaces using the given Object as the implementation.

The object must contain methods signatures matching the methods signatures from the passed Java interfaces. Invoking methods from the Java interfaces will route to the matching object method.

class PrintProxy:
	func println(content: String) -> void:
		print(content)

var print_proxy = PrintProxy.new()
var printer_object = JavaClassWrapper.create_proxy(print_proxy, ["android.util.Printer"])
printer_object.println("Hello Godot World!")

Note: This method only works on Android. On every other platform, this method will always return null.

func create_sam_callback(sam_interface: String, callable: Callable) -> JavaObject

Creates a JavaObject implementing the Java Single Abstract Method (SAM) interface using the Godot Callable as the implementation.

The sam_interface must be a Java SAM interface, meaning it must only have a single abstract method to implement.

The callable must be able to handle the same parameter types as the SAM interface method, and must provide the same return type. The callable will be invoked as a callback, passing the arguments from the Java SAM interface method.

var cb = func (content: String) -> void:
	print(content)
var callback = JavaClassWrapper.create_sam_callback("android.util.Printer", cb)
callback.println("Hello Godot World!")

Note: This method only works on Android. On every other platform, this method will always return null.

func get_exception() -> JavaObject

Returns the Java exception from the last call into a Java class. If there was no exception, it will return null.

Note: This method only works on Android. On every other platform, this method will always return null.

func wrap(name: String) -> JavaClass

Wraps a class defined in Java, and returns it as a JavaClass Object type that Godot can interact with.

When wrapping inner (nested) classes, use $ instead of . to separate them. For example, JavaClassWrapper.wrap("android.view.WindowManager$LayoutParams") wraps the WindowManager.LayoutParams class.

Note: To invoke a constructor, call a method with the same name as the class. For example:

var Intent = JavaClassWrapper.wrap("android.content.Intent")
var intent = Intent.Intent()

Note: This method only works on Android. On every other platform, this method does nothing and returns an empty JavaClass.

Inherited from Object (62)
func _get(property: StringName) -> Variant
func _get_property_list() -> Dictionary[]
func _init() -> void
func _iter_get(iter: Variant) -> Variant
func _iter_init(iter: Array) -> bool
func _iter_next(iter: Array) -> bool
func _notification(what: int) -> void
func _set(property: StringName, value: Variant) -> bool
func _to_string() -> String
func _validate_property(property: Dictionary) -> void
func add_user_signal(signal: String, arguments: Array = []) -> void
func call(method: StringName) -> Variant
func callv(method: StringName, arg_array: Array) -> Variant
func cancel_free() -> void
func connect(signal: StringName, callable: Callable, flags: int = 0) -> Error
func disconnect(signal: StringName, callable: Callable) -> void
func emit_signal(signal: StringName) -> Error
func free() -> void
func get(property: StringName) -> Variant
func get_class() -> String
func get_incoming_connections() -> Dictionary[]
func get_indexed(property_path: NodePath) -> Variant
func get_meta(name: StringName, default: Variant = null) -> Variant
func get_meta_list() -> StringName[]
func get_method_list() -> Dictionary[]
func get_property_list() -> Dictionary[]
func get_signal_connection_list(signal: StringName) -> Dictionary[]
func get_signal_list() -> Dictionary[]
func has_meta(name: StringName) -> bool
func has_method(method: StringName) -> bool
func has_signal(signal: StringName) -> bool
func is_class(class: StringName) -> bool
func is_connected(signal: StringName, callable: Callable) -> bool
func notification(what: int, reversed: bool = false) -> void
func remove_meta(name: StringName) -> void
func remove_user_signal(signal: StringName) -> void
func set(property: StringName, value: Variant) -> void
func set_block_signals(enable: bool) -> void
func set_deferred(property: StringName, value: Variant) -> void
func set_indexed(property_path: NodePath, value: Variant) -> void
func set_message_translation(enable: bool) -> void
func set_meta(name: StringName, value: Variant) -> void
func set_script(script: Variant) -> void
func set_translation_domain(domain: StringName) -> void
func to_string() -> String
func tr(message: StringName, context: StringName = &"") -> String
func tr_n(message: StringName, plural_message: StringName, n: int, context: StringName = &"") -> String

Signals

Inherited from Object (2)