Android

Android Package#

The Replay Android package is hosted on GitHub. Once added as a dependency using JitPack to your Android Studio project, you can replace the MainActivity:

import com.replay.android.ReplayActivity
class MainActivity : ReplayActivity() {
}

An assets folder also needs to be created for your project. See Replay Starter for an example setup. You can optionally add an index.html here to override the default file.

ReplayActivity()#

Overridable methods#

  • onJsCallback(id: String, message: String, message2: String, message3: String, message4: String, message5: String): (Optional) A callback for messages sent from your game. See Bridge.

Methods#

  • jsBridge(messageId: String, jsArg: String): Send a value from Android code to your JS game code. See Bridge.

Inputs#

Since Replay Android embeds your game as a web view, the device.inputs parameter of Sprite methods is an alias of the Web package's inputs:

type AndroidInputs = WebInputs;

The @replay/android package exports this type for TypeScript projects.

Bridge#

You can send asynchronous messages from your game's JS code to your Android code, and then respond back using a Promise. This allows you to use native features like in-app purchases.

JS side#

import { makeSprite } from "@replay/core";
import { androidBridge } from "@replay/android";
export const BridgeSprite = makeSprite({
init({ device }) {
androidBridge({
// This should be unique between parallel messages
id: "TestBridge",
// Strings to send
message: "Hello!",
message2: "Key",
message3: "Value",
// Can send up to 5 messages
}).then((message) => {
// message is a value sent back from Android code.
// This will log "Bridge response: Hi!"
device.log(`Bridge response: ${message.response}`);
});
},
render() {
return [];
},
});

Android side#

class MainActivity : ReplayActivity() {
override fun onJsCallback(
id: String,
message: String,
message2: String,
message3: String,
message4: String,
message5: String
) {
// Here you can call native APIs
val myApiVal = "Hi!"
jsBridge(id, "{ response: `${myApiVal}` }")
}
}