IC4J Agent library can be also used for development of native Android application written in Java or Kotlin. Use Android Studio to start a new Android application or add support for the Internet Computer to your existing application.
To add required IC4J libraries to your Android project open gradle.build file and add dependencies:
MainActivity.kt has a simple code for QUERY and UPDATE invocation of the Canister code main.mo.
main.mo
actor {
stable var name = "Me";
public func greet(value : Text) : async Text {
name := value;
return "Hello, " # name # "!";
};
public shared query func peek() : async Text {
return name;
};
};
val url: String = getString(R.string.url)
val canister: String = getString(R.string.canister)
val identity: Identity = AnonymousIdentity()
val transport: ReplicaTransport =
ReplicaOkHttpTransport.create(url)
val agent: Agent = AgentBuilder().identity(identity).transport(transport).build()
val buf = IDLArgs.create(ArrayList<IDLValue>()).toBytes()
val principal = Principal.fromString(canister);
val response = QueryBuilder.create(
agent,
principal,
"peek"
).arg(buf).call()
val output = response.get()
val outArgs = IDLArgs.fromBytes(output)
name = outArgs.args[0].getValue<String>()