android_plugin.rst 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. .. _doc_android_plugin:
  2. Creating Android plugins (Godot 4.0+)
  3. =====================================
  4. Introduction
  5. ------------
  6. Android plugins are powerful tools to extend the capabilities of the Godot engine
  7. by tapping into the functionality provided by the Android platform and ecosystem.
  8. Mobile gaming monetization is one such example since it requires features
  9. and capabilities that don't belong to the core feature set of a game engine:
  10. - Analytics
  11. - In-app purchases
  12. - Receipt validation
  13. - Install tracking
  14. - Ads
  15. - Video ads
  16. - Cross-promotion
  17. - In-game soft & hard currencies
  18. - Promo codes
  19. - A/B testing
  20. - Login
  21. - Cloud saves
  22. - Leaderboards and scores
  23. - User support & feedback
  24. - Posting to Facebook, Twitter, etc.
  25. - Push notifications
  26. Making modifications to the Android export template is another use-case since using a plugin for that task allows the project
  27. to remain compatible with newer Godot versions.
  28. Android plugin
  29. --------------
  30. While introduced in Godot 3.2.0, the Android plugin system got a significant architecture update starting with Godot 3.2.2. In Godot 4.0, the new architecture became
  31. the default, rendering plugins for Godot 3.2.0 incompatible with Godot 4.0.
  32. As a prerequisite, make sure you understand how to set up a :ref:`custom build environment<doc_android_custom_build>` for Android.
  33. At its core, a Godot Android plugin is a `Android archive library <https://developer.android.com/studio/projects/android-library#aar-contents>`_ (*aar* archive file)
  34. with the following caveats:
  35. - The library must have a dependency on the Godot engine library (``godot-lib.x.y.aar``). A stable version is made available for each Godot release.
  36. - The library must include a specifically configured ``<meta-data>`` tag in its manifest file.
  37. Building a Android plugin
  38. ^^^^^^^^^^^^^^^^^^^^^^^^^
  39. **Prerequisite:** `Android Studio <https://developer.android.com/studio>`_ is strongly recommended as the IDE to use to create Android plugins.
  40. The instructions below assumes that you're using Android Studio.
  41. 1. Follow `these instructions <https://developer.android.com/studio/projects/android-library>`__ to create an Android library module for your plugin.
  42. 2. Add the Godot engine library as a dependency to your plugin module:
  43. - Download the Godot engine library (godot-lib.x.y.aar)
  44. - Follow `these instructions <https://developer.android.com/studio/projects/android-library#AddDependency>`__ to add
  45. the Godot engine library as a dependency for your plugin.
  46. - In the plugin module's ``build.gradle`` file, replace ``implementation`` with ``compileOnly`` for the dependency line for the Godot engine library.
  47. 3. Create a new class in the plugin module and make sure it extends ``org.godotengine.godot.plugin.GodotPlugin``.
  48. At runtime, it will be used to instantiate a singleton object that will be used by the Godot engine to load, initialize and run the plugin.
  49. 4. Update the plugin ``AndroidManifest.xml`` file:
  50. - Open the plugin ``AndroidManifest.xml`` file.
  51. - Add the ``<application></application>`` tag if it's missing.
  52. - In the ``<application>`` tag, add a ``<meta-data>`` tag setup as follow::
  53. <meta-data
  54. android:name="org.godotengine.plugin.v1.[PluginName]"
  55. android:value="[plugin.init.ClassFullName]" />
  56. Where ``PluginName`` is the name of the plugin, and ``plugin.init.ClassFullName`` is the full name (package + class name) of the plugin loading class.
  57. 5. Add the remaining logic for your plugin and run the ``gradlew build`` command to generate the plugin's ``aar`` file.
  58. The build will likely generate both a ``debug`` and ``release`` ``aar`` files. Depending on your need, pick only one version (usually the ``release`` one) which to provide your users with.
  59. **Note:** The plugin's ``aar`` filename must match the following pattern: ``[PluginName]*.aar``
  60. where ``PluginName`` is the name of the plugin in camel case (e.g: ``GodotPayment.release.aar``).
  61. Loading and using a Android plugin
  62. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  63. Once you have access to the plugin ``aar`` file, move it to the Godot project ``res://android/plugins`` directory.
  64. From your script:
  65. .. code-block::
  66. if Engine.has_singleton("MyPlugin"):
  67. var singleton = Engine.get_singleton("MyPlugin")
  68. print(singleton.myPluginFunction("World"))
  69. **When exporting the project**, you need to add the plugin's name to the ``Custom Template`` -> ``Plugins`` section.
  70. If trying to add multiple plugins, separate their names by a comma (``,``).
  71. Bundling GDNative resources
  72. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  73. A Android plugin can define and provide C/C++ GDNative resources, either to provide and/or access functionality from the game logic.
  74. The GDNative resources can be bundled within the plugin ``aar`` file which simplifies the distribution and deployment process:
  75. - The shared libraries (``.so``) for the defined GDNative libraries will be automatically bundled by the ``aar`` build system.
  76. - Godot ``*.gdnlib`` and ``*.gdns`` resource files must be manually defined in the plugin ``assets`` directory.
  77. The recommended path for these resources relative to the ``assets`` directory should be: ``godot/plugin/v1/[PluginName]/``.
  78. For GDNative libraries, the plugin singleton object must override the ``org.godotengine.godot.plugin.GodotPlugin::getPluginGDNativeLibrariesPaths()`` method,
  79. and return the paths to the bundled GDNative libraries config files (``*.gdnlib``). The paths must be relative to the ``assets`` directory.
  80. At runtime, the plugin will provide these paths to Godot core which will use them to load and initialize the bundled GDNative libraries.
  81. Reference implementations
  82. ^^^^^^^^^^^^^^^^^^^^^^^^^
  83. - `Godot Oculus Mobile plugin <https://github.com/m4gr3d/godot_oculus_mobile/tree/2.0>`_
  84. - `Bundled gdnative resources <https://github.com/m4gr3d/godot_oculus_mobile/tree/2.0/plugin/src/main/assets/addons/godot_ovrmobile>`_
  85. - `Godot Payment plugin <https://github.com/m4gr3d/godot/tree/rearch_godot_android_plugin/platform/android/java/plugins/godotpayment>`_
  86. Troubleshooting
  87. ---------------
  88. Godot crashes upon load
  89. ^^^^^^^^^^^^^^^^^^^^^^^
  90. Check ``adb logcat`` for possible problems, then:
  91. - Check that the methods used in the Java singleton only use simple
  92. Java datatypes. More complex datatypes are not supported.