android_plugin.rst 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. :article_outdated: True
  2. .. _doc_android_plugin:
  3. Creating Android plugins
  4. ========================
  5. Introduction
  6. ------------
  7. Android plugins are powerful tools to extend the capabilities of the Godot engine
  8. by tapping into the functionality provided by the Android platform and ecosystem.
  9. Mobile gaming monetization is one such example since it requires features
  10. and capabilities that don't belong to the core feature set of a game engine:
  11. - Analytics
  12. - In-app purchases
  13. - Receipt validation
  14. - Install tracking
  15. - Ads
  16. - Video ads
  17. - Cross-promotion
  18. - In-game soft & hard currencies
  19. - Promo codes
  20. - A/B testing
  21. - Login
  22. - Cloud saves
  23. - Leaderboards and scores
  24. - User support & feedback
  25. - Posting to Facebook, Twitter, etc.
  26. - Push notifications
  27. Android plugin
  28. --------------
  29. While introduced in Godot 3.2, the Android plugin system got a significant architecture update starting with Godot 3.2.2.
  30. The new plugin system is backward-incompatible with the previous one and in Godot 4.0, the previous system was fully deprecated and removed.
  31. Since we previously did not version the Android plugin systems, the new one is now labelled ``v1`` and is the starting point for the modern Godot Android ecosystem.
  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.<version>.<status>.aar``). A stable version is made available for each Godot release on the `Godot download page <https://godotengine.org/download>`_.
  36. - The library must include a specifically configured ``<meta-data>`` tag in its manifest file.
  37. Building an 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.<version>.<status>.aar``) from the `Godot download page <https://godotengine.org/download>`_ (e.g: ``godot-lib.4.0.stable.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.
  59. Depending on your need, pick only one version (usually the ``release`` one) which to provide your users with.
  60. It's recommended that the ``aar`` filename matches the following pattern: ``[PluginName]*.aar`` where ``PluginName`` is the name of the plugin in PascalCase (e.g.: ``GodotPayment.release.aar``).
  61. 6. Create a Godot Android Plugin configuration file to help the system detect and load your plugin:
  62. - The configuration file extension must be ``gdap`` (e.g.: ``MyPlugin.gdap``).
  63. - The configuration file format is as follow::
  64. [config]
  65. name="MyPlugin"
  66. binary_type="local"
  67. binary="MyPlugin.aar"
  68. [dependencies]
  69. local=["local_dep1.aar", "local_dep2.aar"]
  70. remote=["example.plugin.android:remote-dep1:0.0.1", "example.plugin.android:remote-dep2:0.0.1"]
  71. custom_maven_repos=["https://repo.mycompany.com/maven2"]
  72. The ``config`` section and fields are required and defined as follow:
  73. - **name**: name of the plugin.
  74. - **binary_type**: can be either ``local`` or ``remote``. The type affects the **binary** field.
  75. - **binary**:
  76. - If **binary_type** is ``local``, then this should be the filepath of the plugin ``aar`` file.
  77. - The filepath can be relative (e.g.: ``MyPlugin.aar``) in which case it's relative to the ``res://android/plugins`` directory.
  78. - The filepath can be absolute: ``res://some_path/MyPlugin.aar``.
  79. - If **binary_type** is ``remote``, then this should be a declaration for a `remote gradle binary <https://developer.android.com/studio/build/dependencies#dependency-types>`_ (e.g.: ``org.godot.example:my-plugin:0.0.0``).
  80. The ``dependencies`` section and fields are optional and defined as follow:
  81. - **local**: contains a list of filepaths to the local ``.aar`` binary files the plugin depends on. Similarly to the ``binary`` field (when the ``binary_type`` is ``local``), the local binaries' filepaths can be relative or absolute.
  82. - **remote**: contains a list of remote binary gradle dependencies for the plugin.
  83. - **custom_maven_repos**: contains a list of URLs specifying the custom maven repositories required for the plugin's dependencies.
  84. Loading and using an Android plugin
  85. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  86. Move the plugin configuration file (e.g: ``MyPlugin.gdap``) and, if any, its local binary (e.g: ``MyPlugin.aar``) and dependencies to the Godot project's ``res://android/plugins`` directory.
  87. The Godot editor will automatically parse all ``.gdap`` files in the ``res://android/plugins`` directory and show a list of detected and toggleable plugins in the Android export presets window under the **Plugins** section.
  88. In order to allow GDScript to communicate with your Java Singleton, you must annotate your function with ``@UsedByGodot``. The name called from GDScript must match the function name exactly. There is **no** coercing ``snake_case`` to ``camelCase``.
  89. .. image:: img/android_export_preset_plugins_section.png
  90. From your script::
  91. if Engine.has_singleton("MyPlugin"):
  92. var singleton = Engine.get_singleton("MyPlugin")
  93. print(singleton.myPluginFunction("World"))
  94. Bundling GDExtension resources
  95. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  96. An Android plugin can define and provide C/C++ GDExtension resources, either to provide and/or access functionality from the game logic.
  97. The GDExtension resources can be bundled within the plugin ``aar`` file which simplifies the distribution and deployment process:
  98. - The shared libraries (``.so``) for the defined GDExtension libraries will be automatically bundled by the ``aar`` build system.
  99. - Godot ``*.gdnlib`` and ``*.gdns`` resource files must be manually defined in the plugin ``assets`` directory.
  100. The recommended path for these resources relative to the ``assets`` directory should be: ``godot/plugin/v1/[PluginName]/``.
  101. For GDExtension libraries, the plugin singleton object must override the ``org.godotengine.godot.plugin.GodotPlugin::getPluginGDNativeLibrariesPaths()`` method,
  102. and return the paths to the bundled GDExtension libraries config files (``*.gdextension``). The paths must be relative to the ``assets`` directory.
  103. At runtime, the plugin will provide these paths to Godot core which will use them to load and initialize the bundled GDExtension libraries.
  104. Reference implementations
  105. ^^^^^^^^^^^^^^^^^^^^^^^^^
  106. - `Godot Oculus Mobile plugin <https://github.com/GodotVR/godot_oculus_mobile>`_
  107. - `Bundled gdnative resources <https://github.com/GodotVR/godot_oculus_mobile/tree/master/plugin/src/main/assets/addons/godot_ovrmobile>`_
  108. - `Godot Google Play Billing plugin <https://github.com/godotengine/godot-google-play-billing>`_
  109. Troubleshooting
  110. ---------------
  111. Godot crashes upon load
  112. ^^^^^^^^^^^^^^^^^^^^^^^
  113. Check ``adb logcat`` for possible problems, then:
  114. - Check that the methods exposed by the plugin used the following Java types: ``void``, ``boolean``, ``int``, ``float``, ``java.lang.String``, ``org.godotengine.godot.Dictionary``, ``int[]``, ``byte[]``, ``float[]``, ``java.lang.String[]``.
  115. - More complex datatypes are not supported for now.