javaclasswrapper_and_androidruntimeplugin.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. .. _doc_javaclasswrapper_and_androidruntimeplugin:
  2. Integrating with Android APIs
  3. =============================
  4. The Android platform has numerous APIs as well as a rich ecosystem of third-party libraries with wide and
  5. diverse functionality, like push notifications, analytics, authentication, ads, etc...
  6. These don't make sense in Godot core itself so Godot has long provided an :ref:`Android plugin system <doc_android_plugin>`.
  7. The :ref:`Android plugin system <doc_android_plugin>` enables developers to create Godot Android plugins using Java or Kotlin code,
  8. which provides an interface to access and use Android APIs or third-party libraries in Godot projects from GDScript, C# or GDExtension.
  9. .. code-block:: kotlin
  10. class MyAndroidSingleton(godot: Godot?) : GodotPlugin(godot) {
  11. @UsedByGodot
  12. fun doSomething(value: String) {
  13. // ...
  14. }
  15. }
  16. Writing an Android plugin however requires knowledge of Java or Kotlin code, which most Godot developers do not have.
  17. As such there are many Android APIs and third-party libraries that don't have a Godot plugin that developers can interface with.
  18. In fact, this is one of the main reasons that developers cite for not being able to switch to Godot from other game engines.
  19. To address this, we've introduced a couple of tools in **Godot 4.4** to simplify the process for developers to access Android APIs and third-party libraries.
  20. JavaClassWrapper (Godot singleton)
  21. ----------------------------------
  22. ``JavaClassWrapper`` is a :ref:`Godot singleton <class_JavaClassWrapper>` which allows
  23. creating instances of Java / Kotlin classes and calling methods on them using only GDScript, C# or GDExtension.
  24. .. code-block:: gdscript
  25. var LocalDateTime = JavaClassWrapper.wrap("java.time.LocalDateTime")
  26. var DateTimeFormatter = JavaClassWrapper.wrap("java.time.format.DateTimeFormatter")
  27. var datetime = LocalDateTime.now()
  28. var formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss")
  29. print(datetime.format(formatter))
  30. In the code snippet above, ``JavaClassWrapper`` is used from GDScript to access the Java ``LocalDateTime`` and ``DateTimeFormatter`` classes.
  31. Through ``JavaClassWrapper``, we can call the Java classes methods directly from GDScript as if they were GDScript methods.
  32. AndroidRuntime plugin
  33. ---------------------
  34. ``JavaClassWrapper`` is great, but to do many things on Android, you need access to various Android lifecycle / runtime objects.
  35. ``AndroidRuntime`` plugin is a `built-in Godot Android plugin <https://javadoc.io/doc/org.godotengine/godot/latest/org/godotengine/godot/plugin/AndroidRuntimePlugin.html>`_ that allows you to do this.
  36. Combining ``JavaClassWrapper`` and ``AndroidRuntime`` plugin allows developers to access and use Android APIs without switching away from GDScript, or using any tools aside from Godot itself.
  37. This is **huge** for the adoption of Godot for Android development:
  38. - If you need to do something simple, or only use a small part of a third-party library, you don't have to make a plugin
  39. - It allows developers to quickly integrate Android functionality
  40. - It allows developers to create Godot addons using only GDScript and ``JavaClassWrapper`` (no Java or Kotlin needed)
  41. .. note::
  42. For exports using ``gradle``, Godot will automatically include ``.jar`` or ``.aar`` files it find in the project ``addons`` directory.
  43. So to use a third-party library, you can just drop its ``.jar`` or ``.aar`` file in the ``addons`` directory, and call its method directly from GDScript using ``JavaClassWrapper``.
  44. Example: Show an Android toast
  45. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  46. .. code-block:: gdscript
  47. # Retrieve the AndroidRuntime singleton.
  48. var android_runtime = Engine.get_singleton("AndroidRuntime")
  49. if android_runtime:
  50. # Retrieve the Android Activity instance.
  51. var activity = android_runtime.getActivity()
  52. # Create a Godot Callable to wrap the toast display logic.
  53. var toast_callable = func():
  54. # Use JavaClassWrapper to retrieve the android.widget.Toast class, then make and show a toast using the class APIs.
  55. var ToastClass = JavaClassWrapper.wrap("android.widget.Toast")
  56. ToastClass.makeText(activity, "This is a test", ToastClass.LENGTH_LONG).show()
  57. # Wrap the Callable in a Java Runnable and run it on the Android UI thread to show the toast.
  58. activity.runOnUiThread(android_runtime.createRunnableFromGodotCallable(toast_callable))
  59. Example: Vibrate the device
  60. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  61. .. code-block:: gdscript
  62. # Retrieve the AndroidRuntime singleton.
  63. var android_runtime = Engine.get_singleton("AndroidRuntime")
  64. if android_runtime:
  65. # Retrieve the Android Vibrator system service and check if the device supports it.
  66. var vibrator_service = android_runtime.getApplicationContext().getSystemService("vibrator")
  67. if vibrator_service and vibrator_service.hasVibrator():
  68. # Configure and run a VibrationEffect.
  69. var VibrationEffect = JavaClassWrapper.wrap("android.os.VibrationEffect")
  70. var effect = VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)
  71. vibrator_service.vibrate(effect)
  72. Example: Accessing inner classes
  73. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  74. Java inner classes can be accessed using the ``$`` sign:
  75. .. code-block:: gdscript
  76. # Accessing 'VERSION' class, which is an inner class from the 'android.os.Build' class.
  77. var version = JavaClassWrapper.wrap("android.os.Build$VERSION")
  78. var sdk_int = version.SDK_INT
  79. if sdk_int == 30:
  80. # Do something specific on android 11 devices.
  81. else:
  82. # All other devices