creating_android_modules.rst 9.3 KB

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