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.

5 Comments

  1. Vik

    Hey Ondrej Bendo,

    Thanks for your tutorial. It was pretty insightful. Although when I run your code, I get an error when pressing the “Get Greeting”.
    It’s an IndexOutOfBounds. To be specific,

    java.lang.IndexOutOfBoundsException: Index: 0
    at java.util.Collections$EmptyList.get

    I looked it up here: https://github.com/web3j/web3j/issues/179
    In their case, they were using .deploy() and didn’t place their initial Wei value to 0.

    Just wondering if you came across it during your debugging.
    Cheers,

    • Ondrej Bendo

      Hi Vik,
      To run the code you have to modify it with your own information.
      Make sure to search for YOUR_PRIVATE_KEY and YOUR_API_KEY.
      Replace YOUR_PRIVATE_KEY with your ethereum account private key.
      Replace YOUR_API_KEY with the api key you get after registering on the Infura website.

  2. Amit

    Hi ,

    Nice article! one question though, how are you creating the wallet on the device( public+private key). Infura doesnt support personal apis.

    Thanks and Regards
    Amit

  3. Peter Babich

    Thanks for the artcile! Was very informative, and very good as a go-to guide. If you would like to know some more theory basis about Ether and smart contract building, I would suggest reading this article as well:
    https://vironit.com/how-to-%E2%80%8Abuild-smart-contract-for-ethereum/
    Hope this helps!

Leave a Reply