creating_android_modules.rst 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. .. _doc_creating_android_modules:
  2. Creating Android modules
  3. ========================
  4. Introduction
  5. ------------
  6. Making video games portable is all fine and dandy, until mobile
  7. gaming monetization shows up.
  8. This area is complex, usually a mobile game that monetizes needs
  9. special connections to a server for stuff such as:
  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. Oh yeah, developing for mobile is a lot of work. On iOS, you can just
  27. write a C++ module and take advantage of the C++/ObjC
  28. intercommunication, so this is rather easy.
  29. For C++ developers Java is a pain, the build system is severely bloated
  30. and interfacing it with C++ through JNI (Java Native Interface) is more
  31. pain that you don't want even for your worst enemy.
  32. Maybe REST?
  33. -----------
  34. Most of these APIs allow communication via REST+JSON APIs. Godot has
  35. great support for HTTP, HTTPS and JSON, so consider this as an option
  36. that works in every platform. Only write the code once and you are set
  37. to go.
  38. Popular engines that have half the share of apps published on mobile get
  39. special plugins written just for them. Godot does not have that luxury
  40. yet. So, if you write a REST implementation of a SDK for Godot, please
  41. share it with the community.
  42. Android module
  43. --------------
  44. Writing an Android module is similar to :ref:`doc_custom_modules_in_c++`, but
  45. needs a few more steps.
  46. Make sure you are familiar with building your own :ref:`Android export templates <doc_compiling_for_android>`,
  47. as well as creating :ref:`doc_custom_modules_in_c++`.
  48. config.py
  49. ~~~~~~~~~
  50. In the config.py for the module, some extra functions are provided for
  51. convenience. First, it's often wise to detect if android is being built
  52. and only enable building in this case:
  53. .. code:: python
  54. def can_build(plat):
  55. return plat=="android"
  56. If more than one platform can be built (typical if implementing the
  57. module also for iOS), check manually for Android in the configure
  58. functions:
  59. .. code:: python
  60. def can_build(plat):
  61. return plat=="android" or plat=="iphone"
  62. def configure(env):
  63. if env['platform'] == 'android':
  64. # android specific code
  65. Java singleton
  66. --------------
  67. An android module will usually have a singleton class that will load it,
  68. this class inherits from ``Godot.SingletonBase``. A singleton object
  69. template follows:
  70. .. code:: java
  71. // namespace is wrong, will eventually change
  72. package com.android.godot;
  73. public class MySingleton extends Godot.SingletonBase {
  74. public int myFunction(String p_str) {
  75. // a function to bind
  76. }
  77. static public Godot.SingletonBase initialize(Activity p_activity) {
  78. return new MySingleton(p_activity);
  79. }
  80. public MySingleton(Activity p_activity) {
  81. //register class name and functions to bind
  82. registerClass("MySingleton", new String[]{"myFunction"});
  83. // you might want to try initializing your singleton here, but android
  84. // threads are weird and this runs in another thread, so you usually have to do
  85. activity.runOnUiThread(new Runnable() {
  86. public void run() {
  87. //useful way to get config info from engine.cfg
  88. String key = GodotLib.getGlobal("plugin/api_key");
  89. SDK.initializeHere();
  90. }
  91. });
  92. }
  93. // forwarded callbacks you can reimplement, as SDKs often need them
  94. protected void onMainActivityResult(int requestCode, int resultCode, Intent data) {}
  95. protected void onMainPause() {}
  96. protected void onMainResume() {}
  97. protected void onMainDestroy() {}
  98. protected void onGLDrawFrame(GL10 gl) {}
  99. protected void onGLSurfaceChanged(GL10 gl, int width, int height) {} // singletons will always miss first onGLSurfaceChanged call
  100. }
  101. Calling back to Godot from Java is a little more difficult. The instance
  102. ID of the script must be known first, this is obtained by calling
  103. ``get_instance_ID()`` on the script. This returns an integer that can be
  104. passed to Java.
  105. From Java, use the ``calldeferred`` function to communicate back with Godot.
  106. Java will most likely run in a separate thread, so calls are deferred:
  107. .. code:: java
  108. GodotLib.calldeferred(<instanceid>, "<function>", new Object[]{param1,param2,etc});
  109. Add this singleton to the build of the project by adding the following
  110. to config.py:
  111. .. code:: python
  112. def can_build(plat):
  113. return plat=="android" or plat=="iphone"
  114. def configure(env):
  115. if env['platform'] == 'android':
  116. # will copy this to the java folder
  117. env.android_module_file("MySingleton.java")
  118. #env.android_module_file("MySingleton2.java") call again for more files
  119. AndroidManifest
  120. ---------------
  121. Some SDKs need custom values in AndroidManifest.xml. Permissions can be
  122. edited from the godot exporter so there is no need to add those, but
  123. maybe other functionalities are needed.
  124. Create the custom chunk of android manifest and put it inside the
  125. module, add it like this:
  126. .. code:: python
  127. def can_build(plat):
  128. return plat=="android" or plat=="iphone"
  129. def configure(env):
  130. if env['platform'] == 'android':
  131. # will copy this to the java folder
  132. env.android_module_file("MySingleton.java")
  133. env.android_module_manifest("AndroidManifestChunk.xml")
  134. SDK library
  135. -----------
  136. So, finally it's time to add the SDK library. The library can come in
  137. two flavors, a JAR file or an Android project for ant. JAR is the
  138. easiest to integrate, just put it in the module directory and add it:
  139. .. code:: python
  140. def can_build(plat):
  141. return plat=="android" or plat=="iphone"
  142. def configure(env):
  143. if env['platform'] == 'android':
  144. # will copy this to the java folder
  145. env.android_module_file("MySingleton.java")
  146. env.android_module_manifest("AndroidManifestChunk.xml")
  147. env.android_module_library("MyLibrary-3.1.jar")
  148. SDK project
  149. -----------
  150. When this is an Android project, things usually get more complex. Copy
  151. the project folder inside the module directory and configure it:
  152. ::
  153. c:\godot\modules\mymodule\sdk-1.2> android -p . -t 15
  154. As of this writing, Godot uses minsdk 10 and target sdk 15. If this ever
  155. changes, it should be reflected in the manifest template:
  156. `AndroidManifest.xml.template <https://github.com/godotengine/godot/blob/master/platform/android/AndroidManifest.xml.template>`
  157. Then, add the module folder to the project:
  158. .. code:: python
  159. def can_build(plat):
  160. return plat=="android" or plat=="iphone"
  161. def configure(env):
  162. if env['platform'] == 'android':
  163. # will copy this to the java folder
  164. env.android_module_file("MySingleton.java")
  165. env.android_module_manifest("AndroidManifestChunk.xml")
  166. env.android_module_source("sdk-1.2","")
  167. Building
  168. --------
  169. As you probably modify the contents of the module, and modify your .java
  170. inside the module, you need the module to be built with the rest of
  171. Godot, so compile android normally.
  172. ::
  173. c:\godot> scons p=android
  174. This will cause your module to be included, the .jar will be copied to
  175. the java folder, the .java will be copied to the sources folder, etc.
  176. Each time you modify the .java, scons must be called.
  177. Afterwards, just build the ant project normally:
  178. ::
  179. c:\godot\platform\android\java> ant release
  180. This should generate the apk used as export template properly, as
  181. defined in :ref:`doc_compiling_for_android`.
  182. Usually to generate the apk, again both commands must be run in
  183. sequence:
  184. ::
  185. c:\godot> scons p=android
  186. c:\godot\platform\android\java> ant release
  187. Using the Module
  188. ~~~~~~~~~~~~~~~~
  189. To use the Module from GDScript, first enable the singleton by adding
  190. the following line to engine.cfg:
  191. ::
  192. [android]
  193. modules="com/android/godot/MySingleton"
  194. More than one singleton module can be enabled by separating with commas:
  195. ::
  196. [android]
  197. modules="com/android/godot/MySingleton,com/android/godot/MyOtherSingleton"
  198. Then just request the singleton Java object from Globals like this:
  199. ::
  200. # in any file
  201. var singleton = null
  202. func _init():
  203. singleton = Globals.get_singleton("MySingleton")
  204. print(singleton.myFunction("Hello"))
  205. Troubleshooting
  206. ---------------
  207. (This section is a work in progress, report your problems here!)
  208. Godot crashes upon load
  209. ~~~~~~~~~~~~~~~~~~~~~~~
  210. Check ``adb logcat`` for possible problems, then:
  211. - Make sure libgodot_android.so is in the ``libs/armeabi`` folder
  212. - Check that the methods used in the Java singleton only use simple
  213. Java datatypes, more complex ones are not supported.
  214. Future
  215. ------
  216. Godot has an experimental Java API Wrapper that allows to use the
  217. entire Java API from GDScript.
  218. It's simple to use and it's used like this:
  219. ::
  220. class = JavaClassWrapper.wrap(<javaclass as text>)
  221. This is most likely not functional yet, if you want to test it and help
  222. us make it work, contact us through the `developer mailing
  223. list <https://groups.google.com/forum/#!forum/godot-engine>`__.