Interacting with Ethereum Smart Contracts from Android

Usually when you want to interact with smart contracts you need to download the whole ethereum blockchain and keep your local node synchronized. As of writing the blockchain takes up more than 80GB of space on my computer. This might make sense on a desktop computer but is less reasonable on a mobile device.

One way to go around this limitation is to use a service like Infura. Infura allows you to connect to a remote ethereum node and execute transactions without having to worry about maintaining and synchronizing your local node.

To be able to transact with the smart contract from native Java code I used a library called Web3j. Web3j provides you with utilities for generating smart contract java wrappers and a complete implementation of Ethereum’s JSON-RPC client API over HTTP and IPC. It provides more features but these were the important ones for this “Android Ethereum hello world” example.

The example smart contract I want to interact with is a Greeter. It stores a greeting message on the blockchain which can be read or updated. It looks like this:


To be able to create the wrappers we first have to compile this smart contract like so


To generate the wrappers you run


Java part

First we need to get a web3 instance for the ethereum blockchain interaction.
It will look like this


Now we can read from the contract which is almost instantaneous.


Transactions are not instantaneous and may take a few minutes to be validated.


Full working example can be found on github.

Leave a Reply