Creating Android modules

    This area is complex, usually a mobile game that monetizes needs special connections to a server for things like:

    • Analytics
    • In-app purchases
    • Receipt validation
    • Install tracking
    • Ads
    • Video ads
    • Cross-promotion
    • In-game soft & hard currencies
    • Promo codes
    • A/B testing
    • Login
    • Cloud saves
    • Leaderboards and scores
    • User support & feedback
    • Posting to Facebook, Twitter, etc.
    • Push notifications

    On iOS, you can write a C++ module and take advantage of the C++/ObjC intercommunication.

    On Android, interfacing with C++ through JNI (Java Native Interface) isn’t as convenient.

    Maybe REST?

    Most of these APIs allow communication via REST/JSON APIs. Godot has great support for HTTP, HTTPS and JSON, so consider this as an option that works on every platform. Only write the code once and you are set to go.

    Android module

    Writing an Android module is similar to , but needs a few more steps.

    Make sure you are familiar with building your own Android export templates, as well as creating .

    In the config.py for the module, some extra functions are provided for convenience. First, it’s often wise to detect if Android is the target platform being built for and only enable building in this case:

    If more than one platform can be built (typical if implementing the module also for iOS), check manually for Android in the configure functions for Android (or other platform-specific) code:

    1. return plat=="android" or plat=="iphone"
    2. def configure(env):
    3. if env['platform'] == 'android':
    4. # android specific code

    Java singleton

    An Android module will usually have a singleton class that will load it, this class inherits from Godot.SingletonBase. Resource identifiers for any additional resources you have provided for the module will be in the com.godot.game.R class, so you’ll likely want to import it.

    A singleton object template follows:

    1. package org.godotengine.godot;
    2. import android.app.Activity;
    3. import android.content.Intent;
    4. import com.godot.game.R;
    5. import javax.microedition.khronos.opengles.GL10;
    6. public class MySingleton extends Godot.SingletonBase {
    7. protected Context appContext;
    8. private int instanceId = 0;
    9. public int myFunction(String p_str) {
    10. // a function to bind
    11. return 1;
    12. }
    13. public void getInstanceId(int pInstanceId) {
    14. // You will need to call this method from Godot and pass in the get_instance_id().
    15. instanceId = pInstanceId;
    16. }
    17. static public Godot.SingletonBase initialize(Activity p_activity) {
    18. return new MySingleton(p_activity);
    19. }
    20. public MySingleton(Activity p_activity) {
    21. //register class name and functions to bind
    22. registerClass("MySingleton", new String[]
    23. {
    24. "getInstanceId"
    25. });
    26. this.appActivity = p_activity;
    27. this.appContext = appActivity.getApplicationContext();
    28. // you might want to try initializing your singleton here, but android
    29. // threads are weird and this runs in another thread, so to interact with Godot you usually have to do
    30. activity.runOnUiThread(new Runnable() {
    31. public void run() {
    32. //useful way to get config info from project.godot
    33. String key = GodotLib.getGlobal("plugin/api_key");
    34. //SDK.initializeHere();
    35. }
    36. });
    37. }
    38. // forwarded callbacks you can reimplement, as SDKs often need them
    39. protected void onMainActivityResult(int requestCode, int resultCode, Intent data) {}
    40. protected void onMainRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {}
    41. protected void onMainPause() {}
    42. protected void onMainResume() {}
    43. protected void onMainDestroy() {}
    44. protected void onGLDrawFrame(GL10 gl) {}
    45. protected void onGLSurfaceChanged(GL10 gl, int width, int height) {} // singletons will always miss first onGLSurfaceChanged call
    46. }

    From Java, use the calldeferred function to communicate back with Godot. Java will most likely run in a separate thread, so calls are deferred:

    1. GodotLib.calldeferred(<instanceid>, "<function>", new Object[]{param1,param2,etc});

    Add this singleton to the build of the project by adding the following to config.py:

    1. def can_build(plat):
    2. return plat=="android" or plat=="iphone"
    3. def configure(env):
    4. if env['platform'] == 'android':
    5. # will copy this to the java folder

    Some SDKs need custom values in AndroidManifest.xml. Permissions can be edited from the Godot exporter so there is no need to add those, but maybe other functionalities are needed.

    Create the custom chunk of android manifest and put it inside the module, add it like this:

    Resources

    In order to provide additional resources with your module you have to add something like this:

    1. def configure(env):
    2. if env['platform'] == 'android':
    3. # [...]
    4. env.android_add_res_dir("Directory that contains resource subdirectories (values, drawable, etc.)")

    Now you can refer to those resources by their id (R.string.my_string, and the like) by importing the com.godot.game.R class in your Java code.

    Assets

    Similarly, you can add any type of raw asset files to your app’s asset directory like this:

    1. def configure(env):
    2. if env['platform'] == 'android':
    3. # [...]
    4. env.android_add_asset_dir("Directory that contains asset files for your app")

    Assets don’t have resource ids, but can be read with their file name as streams of bytes with the help of the Android AssetManager class.

    SDK library

    So, finally it’s time to add the SDK library. The library can come in two flavors, a JAR file or an Android project for ant. JAR is the easiest to integrate, put it in the module directory and add it:

    1. def can_build(plat):
    2. return plat=="android" or plat=="iphone"
    3. def configure(env):
    4. if env['platform'] == 'android':
    5. # will copy this to the java folder
    6. env.android_add_java_dir("Directory that contains MySingleton.java")
    7. env.android_add_to_manifest("AndroidManifestChunk.xml")
    8. 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/
    9. env.android_add_maven_repository("maven url") #add a maven url
    10. env.android_add_dependency("compile 'com.google.android.gms:play-services-ads:8'") #get dependency from maven repository

    When this is an Android project, things usually get more complex. Copy the project folder inside the module directory and configure it:

    1. c:\godot\modules\mymodule\sdk-1.2> android -p . -t 15

    Then, add the module folder to the project:

    Building

    As you probably modify the contents of the module, and modify your .java inside the module, you need the module to be built with the rest of Godot, so compile android normally.

    1. c:\godot> scons p=android

    This will cause your module to be included, the .jar will be copied to the java folder, the .java will be copied to the sources folder, etc. Each time you modify the .java, scons must be called.

    Afterwards, continue the steps for compiling android Compiling for Android.

    To use the module from GDScript, first enable the singleton by adding the following line to project.godot:

    1. [android]
    2. modules="org/godotengine/godot/MySingleton"

    More than one singleton module can be enabled by separating with commas:

    1. [android]
    2. modules="org/godotengine/godot/MySingleton,org/godotengine/godot/MyOtherSingleton"

    Then request the singleton Java object from Globals like this:

    1. # in any file
    2. var singleton = null
    3. func _init():
    4. singleton = Globals.get_singleton("MySingleton")
    5. print(singleton.myFunction("Hello"))

    Troubleshooting

    Check adb logcat for possible problems, then:

    • Check that the methods used in the Java singleton only use simple Java datatypes, more complex ones are not supported.

    Future

    Godot has an experimental Java API Wrapper that allows to use the entire Java API from GDScript.

    It’s simple to use and it’s used like this: