A Kotlin wrapper for the smoldot Rust-based Polkadot light client.
Add the following dependency to your build.gradle.kts
:
implementation("io.finsig:smoldotkotlin:0.1.1")
A Chain Specification file must be provided to initialize a chain. A Chain Specification is a JSON Object that describes a Polkadot-based blockchain network.
Example Chain Specification JSON files for Polkadot, Kusama, Rococo, and Westend can be copied for use from /smoldotkotlin/src/androidTest/assets.
Initialize a chain from a specification file:
val specification = ChainSpecification()
.readFromAsset("file name", LocalContext.current))
val chain = Chain(specification)
Add the chain to the client to connect to the network:
Client.instance().add(chain)
RPC requests must conform to the JSON-RPC 2.0 protocol.
val request = JSONRPC2Request.parse(\"{\"id\":1,\"jsonrpc\":\"2.0\",\"method\":\"chain_getHeader\",\"params\":[]}")
To send the request:
Client.instance().send(request,chain)
Collect from the responses flow to get the response value:
CoroutineScope().launch {
Client.instance().responses(chain).collect { response ->
// Do something with the response string
}
}
To disconnect the client from the network use:
Client.instance().remove(chain)
For additional information about usage see reference documentation.
You may enable logging of the smoldot Rust FFI library with a Gradle Build Configuration field (RUST_LOG
). The library uses the Rustenv_logger
framework and levels can be set accordingly.
android {
...
buildTypes {
debug {
buildConfigField("String", "RUST_LOG", "\"info\"")
}
}
}
The following Android Studio tools are required to build locally:
- SDK Tools v34.0.0
- NDK (Side by side) v26.1.10909125
- CMake v3.22.1
The NDK code is built using CMake and called from Kotlin using the Java Native Interface (JNI). The provided shell script can be used to build the static libary files for each Android ABI.
$ zsh build_staticlibs.sh
A Dockerfile is provided that can build (but not test) the AAR for the project.
Note: the Dockerfile is configured to run on 64-bit architecture. If using x86 architecture, remove the --platform
flag from the ubuntu build stage of the Dockerfile.
Build a Docker image from the Dockerfile:
docker build -t "finsig:Dockerfile" .
Gradle Wrapper can then be invoked as follows:
docker run --rm -v `pwd`:/project finsig:Dockerfile bash -c 'cd /project; \
./gradlew smoldotkotlin:bundleReleaseAar'
See GitHub issues for notes regarding warning messages.
Unit tests are provided in the form of instrumented tests. Regular unit tests will not work because they do not provide access to Android specific framework dependencies. These tests require an Android Virtual Device (AVD) or attached physical device, and can be run from Android Studio.
The Dockerfile is a fork of Docker Android Build Box.