0.2.0-dev Download ZIP
82 lines ·4.5 KB Raw
| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="JavaClassWrapper" inherits="Object" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 3 | <brief_description> |
| 4 | Provides access to the Java Native Interface. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | The JavaClassWrapper singleton provides a way for the Godot application to send and receive data through the [url=https://developer.android.com/training/articles/perf-jni]Java Native Interface[/url] (JNI). |
| 8 | [b]Note:[/b] This singleton is only available in Android builds. |
| 9 | [codeblock] |
| 10 | var LocalDateTime = JavaClassWrapper.wrap("java.time.LocalDateTime") |
| 11 | var DateTimeFormatter = JavaClassWrapper.wrap("java.time.format.DateTimeFormatter") |
| 12 | |
| 13 | var datetime = LocalDateTime.now() |
| 14 | var formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss") |
| 15 | |
| 16 | print(datetime.format(formatter)) |
| 17 | [/codeblock] |
| 18 | [b]Warning:[/b] When calling Java methods, be sure to check [method JavaClassWrapper.get_exception] to check if the method threw an exception. |
| 19 | </description> |
| 20 | <tutorials> |
| 21 | <link title="Integrating with Android APIs">$DOCS_URL/tutorials/platform/android/javaclasswrapper_and_androidruntimeplugin.html</link> |
| 22 | </tutorials> |
| 23 | <methods> |
| 24 | <method name="create_proxy"> |
| 25 | <return type="JavaObject" /> |
| 26 | <param index="0" name="object" type="Object" /> |
| 27 | <param index="1" name="interfaces" type="PackedStringArray" /> |
| 28 | <description> |
| 29 | Creates a [JavaObject] implementing the given Java interfaces using the given [Object] as the implementation. |
| 30 | The [param object] must contain methods signatures matching the methods signatures from the passed Java [param interfaces]. Invoking methods from the Java [param interfaces] will route to the matching [param object] method. |
| 31 | [codeblock] |
| 32 | class PrintProxy: |
| 33 | func println(content: String) -> void: |
| 34 | print(content) |
| 35 | |
| 36 | var print_proxy = PrintProxy.new() |
| 37 | var printer_object = JavaClassWrapper.create_proxy(print_proxy, ["android.util.Printer"]) |
| 38 | printer_object.println("Hello Godot World!") |
| 39 | [/codeblock] |
| 40 | [b]Note:[/b] This method only works on Android. On every other platform, this method will always return [code]null[/code]. |
| 41 | </description> |
| 42 | </method> |
| 43 | <method name="create_sam_callback"> |
| 44 | <return type="JavaObject" /> |
| 45 | <param index="0" name="sam_interface" type="String" /> |
| 46 | <param index="1" name="callable" type="Callable" /> |
| 47 | <description> |
| 48 | Creates a [JavaObject] implementing the Java Single Abstract Method (SAM) interface using the Godot [Callable] as the implementation. |
| 49 | The [param sam_interface] [b]must be[/b] a Java SAM interface, meaning it must only have a single abstract method to implement. |
| 50 | The [param callable] must be able to handle the same parameter types as the SAM interface method, and must provide the same return type. The [param callable] will be invoked as a callback, passing the arguments from the Java SAM interface method. |
| 51 | [codeblock] |
| 52 | var cb = func (content: String) -> void: |
| 53 | print(content) |
| 54 | var callback = JavaClassWrapper.create_sam_callback("android.util.Printer", cb) |
| 55 | callback.println("Hello Godot World!") |
| 56 | [/codeblock] |
| 57 | [b]Note:[/b] This method only works on Android. On every other platform, this method will always return [code]null[/code]. |
| 58 | </description> |
| 59 | </method> |
| 60 | <method name="get_exception"> |
| 61 | <return type="JavaObject" /> |
| 62 | <description> |
| 63 | Returns the Java exception from the last call into a Java class. If there was no exception, it will return [code]null[/code]. |
| 64 | [b]Note:[/b] This method only works on Android. On every other platform, this method will always return [code]null[/code]. |
| 65 | </description> |
| 66 | </method> |
| 67 | <method name="wrap"> |
| 68 | <return type="JavaClass" /> |
| 69 | <param index="0" name="name" type="String" /> |
| 70 | <description> |
| 71 | Wraps a class defined in Java, and returns it as a [JavaClass] [Object] type that Godot can interact with. |
| 72 | When wrapping inner (nested) classes, use [code]$[/code] instead of [code].[/code] to separate them. For example, [code]JavaClassWrapper.wrap("android.view.WindowManager$LayoutParams")[/code] wraps the [b]WindowManager.LayoutParams[/b] class. |
| 73 | [b]Note:[/b] To invoke a constructor, call a method with the same name as the class. For example: |
| 74 | [codeblock] |
| 75 | var Intent = JavaClassWrapper.wrap("android.content.Intent") |
| 76 | var intent = Intent.Intent() |
| 77 | [/codeblock] |
| 78 | [b]Note:[/b] This method only works on Android. On every other platform, this method does nothing and returns an empty [JavaClass]. |
| 79 | </description> |
| 80 | </method> |
| 81 | </methods> |
| 82 | </class> |