creating_android_modules.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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``. Resource identifiers for
  69. any additional resources you have provided for the module will be in the
  70. ``com.godot.game.R`` class, so you'll likely want to import it.
  71. A singleton object template follows:
  72. .. code:: java
  73. // package com.android.godot; // for 1.1
  74. package org.godotengine.godot; // for 2.0
  75. import com.godot.game.R;
  76. public class MySingleton extends Godot.SingletonBase {
  77. public int myFunction(String p_str) {
  78. // a function to bind
  79. }
  80. static public Godot.SingletonBase initialize(Activity p_activity) {
  81. return new MySingleton(p_activity);
  82. }
  83. public MySingleton(Activity p_activity) {
  84. //register class name and functions to bind
  85. registerClass("MySingleton", new String[]{"myFunction"});
  86. // you might want to try initializing your singleton here, but android
  87. // threads are weird and this runs in another thread, so you usually have to do
  88. activity.runOnUiThread(new Runnable() {
  89. public void run() {
  90. //useful way to get config info from engine.cfg
  91. String key = GodotLib.getGlobal("plugin/api_key");
  92. SDK.initializeHere();
  93. }
  94. });
  95. }
  96. // forwarded callbacks you can reimplement, as SDKs often need them
  97. protected void onMainActivityResult(int requestCode, int resultCode, Intent data) {}
  98. protected void onMainPause() {}
  99. protected void onMainResume() {}
  100. protected void onMainDestroy() {}
  101. protected void onGLDrawFrame(GL10 gl) {}
  102. protected void onGLSurfaceChanged(GL10 gl, int width, int height) {} // singletons will always miss first onGLSurfaceChanged call
  103. }
  104. Calling back to Godot from Java is a little more difficult. The instance
  105. ID of the script must be known first, this is obtained by calling
  106. ``get_instance_ID()`` on the script. This returns an integer that can be
  107. passed to Java.
  108. From Java, use the ``calldeferred`` function to communicate back with Godot.
  109. Java will most likely run in a separate thread, so calls are deferred:
  110. .. code:: java
  111. GodotLib.calldeferred(<instanceid>, "<function>", new Object[]{param1,param2,etc});
  112. Add this singleton to the build of the project by adding the following
  113. to config.py:
  114. (Before Version 2.0)
  115. .. code:: python
  116. def can_build(plat):
  117. return plat=="android" or plat=="iphone"
  118. def configure(env):
  119. if env['platform'] == 'android':
  120. # will copy this to the java folder
  121. env.android_module_file("MySingleton.java")
  122. #env.android_module_file("MySingleton2.java") call again for more files
  123. (After Version 2.0)
  124. .. code:: python
  125. def can_build(plat):
  126. return plat=="android" or plat=="iphone"
  127. def configure(env):
  128. if env['platform'] == 'android':
  129. # will copy this to the java folder
  130. env.android_add_java_dir("Directory that contain MySingleton.java")
  131. AndroidManifest
  132. ---------------
  133. Some SDKs need custom values in AndroidManifest.xml. Permissions can be
  134. edited from the godot exporter so there is no need to add those, but
  135. maybe other functionalities are needed.
  136. Create the custom chunk of android manifest and put it inside the
  137. module, add it like this:
  138. (Before Version 2.0)
  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. (After Version 2.0)
  148. .. code:: python
  149. def can_build(plat):
  150. return plat=="android" or plat=="iphone"
  151. def configure(env):
  152. if env['platform'] == 'android':
  153. # will copy this to the java folder
  154. env.android_add_java_dir("Directory that contain MySingelton.java")
  155. env.android_add_to_manifest("AndroidManifestChunk.xml")
  156. Resources
  157. ---------
  158. In order to provide additional resources with your module you have to
  159. add something like this:
  160. .. code:: python
  161. def configure(env):
  162. if env['platform'] == 'android':
  163. # [...]
  164. env.android_add_res_dir("Directory that contains resource subdirectories (values, drawable, etc.)")
  165. Now you can refer to those resources by their id (``R.string.my_string``, and the like)
  166. by importing the ``com.godot.game.R`` class in your Java code.
  167. SDK library
  168. -----------
  169. So, finally it's time to add the SDK library. The library can come in
  170. two flavors, a JAR file or an Android project for ant. JAR is the
  171. easiest to integrate, just put it in the module directory and add it:
  172. (Before Version 2.0)
  173. .. code:: python
  174. def can_build(plat):
  175. return plat=="android" or plat=="iphone"
  176. def configure(env):
  177. if env['platform'] == 'android':
  178. # will copy this to the java folder
  179. env.android_module_file("MySingleton.java")
  180. env.android_module_manifest("AndroidManifestChunk.xml")
  181. env.android_module_library("MyLibrary-3.1.jar")
  182. (After Version 2.0)
  183. .. code:: python
  184. def can_build(plat):
  185. return plat=="android" or plat=="iphone"
  186. def configure(env):
  187. if env['platform'] == 'android':
  188. # will copy this to the java folder
  189. env.android_add_java_dir("Directory that contain MySingelton.java")
  190. env.android_add_to_manifest("AndroidManifestChunk.xml")
  191. env.android_add_dependency("compile files('something_local.jar')") # if you have a jar, the path is relative to platform/android/java/gradlew, so it will start with ../../../modules/module_name/
  192. env.android_add_maven_repository("maven url") #add a maven url
  193. env.android_add_dependency("compile 'com.google.android.gms:play-services-ads:8'") #get dependency from maven repository
  194. SDK project
  195. -----------
  196. When this is an Android project, things usually get more complex. Copy
  197. the project folder inside the module directory and configure it:
  198. ::
  199. c:\godot\modules\mymodule\sdk-1.2> android -p . -t 15
  200. As of this writing, Godot uses minsdk 10 and target sdk 15. If this ever
  201. changes, it should be reflected in the manifest template:
  202. `AndroidManifest.xml.template <https://github.com/godotengine/godot/blob/master/platform/android/AndroidManifest.xml.template>`
  203. Then, add the module folder to the project:
  204. (Before Version 2.0)
  205. .. code:: python
  206. def can_build(plat):
  207. return plat=="android" or plat=="iphone"
  208. def configure(env):
  209. if env['platform'] == 'android':
  210. # will copy this to the java folder
  211. env.android_module_file("MySingleton.java")
  212. env.android_module_manifest("AndroidManifestChunk.xml")
  213. env.android_module_source("sdk-1.2","")
  214. (After Version 2.0)
  215. Building
  216. --------
  217. As you probably modify the contents of the module, and modify your .java
  218. inside the module, you need the module to be built with the rest of
  219. Godot, so compile android normally.
  220. ::
  221. c:\godot> scons p=android
  222. This will cause your module to be included, the .jar will be copied to
  223. the java folder, the .java will be copied to the sources folder, etc.
  224. Each time you modify the .java, scons must be called.
  225. Afterwards, just continue the steps for compiling android :ref:`doc_compiling_for_android`.
  226. Using the module
  227. ~~~~~~~~~~~~~~~~
  228. To use the module from GDScript, first enable the singleton by adding
  229. the following line to engine.cfg (Godot Engine 2.0 and greater):
  230. ::
  231. [android]
  232. modules="org/godotengine/godot/MySingleton"
  233. For Godot Engine 1.1 is
  234. ::
  235. [android]
  236. modules="com/android/godot/MySingleton"
  237. More than one singleton module can be enabled by separating with commas:
  238. ::
  239. [android]
  240. modules="com/android/godot/MySingleton,com/android/godot/MyOtherSingleton"
  241. Then just request the singleton Java object from Globals like this:
  242. ::
  243. # in any file
  244. var singleton = null
  245. func _init():
  246. singleton = Globals.get_singleton("MySingleton")
  247. print(singleton.myFunction("Hello"))
  248. Troubleshooting
  249. ---------------
  250. (This section is a work in progress, report your problems here!)
  251. Godot crashes upon load
  252. ~~~~~~~~~~~~~~~~~~~~~~~
  253. Check ``adb logcat`` for possible problems, then:
  254. - Make sure libgodot_android.so is in the ``libs/armeabi`` folder
  255. - Check that the methods used in the Java singleton only use simple
  256. Java datatypes, more complex ones are not supported.
  257. Future
  258. ------
  259. Godot has an experimental Java API Wrapper that allows to use the
  260. entire Java API from GDScript.
  261. It's simple to use and it's used like this:
  262. ::
  263. class = JavaClassWrapper.wrap(<javaclass as text>)
  264. This is most likely not functional yet, if you want to test it and help
  265. us make it work, contact us through the `developer mailing
  266. list <https://groups.google.com/forum/#!forum/godot-engine>`__.