7 Lines of Android Code To Query ETH Balance on Your Own Private Blockchain

Hai Le Gia
5 min readMay 13, 2021

I spent my Saturday evening connecting an Android app to my local Go Ethereum instance. Because, why not?

Images by WorldSpectrum and ElisaRiva, edited by me.

It’s a lovely Saturday evening so I decided to write something. After a long time being a read-only Medium user, it’s time to contribute back (and hopefully to earn some cash as well 🎉).

In this article, I’m going to show you how to:

  1. Start your own local private blockchain using Geth (Go Ethereum)
  2. Expose your blockchain to the Internet.
  3. Setup a minimum Android project connect to your blockchain and query your own ETH balance.

If this is what you’re looking for, let’s begin!

Start your own Etherum private blockchain with Geth

What is Go Ethereum (Geth)?

With Ethereum, you can build your own blockchain node, as long as you implement the Ethereum protocol. That would be an interesting and ambitious project. However, we already had many stable projects doing this. Here are some of the most popular implementations that you can explore:

We’re going to use Geth for the rest of the article.

Install Geth

Geth is available to download from here. It’s a binary file and we can start using it immediately. However, we won’t run Geth as a full Ethereum client connecting the main net. Instead, we’ll start Geth as a single-node private blockchain (you own it) for our development purpose.

Configuration for our genesis block:

For any blockchain, the first block is called the “genesis block”. We’ll create our own by making a genesis.json file. You can use the following content:

Initialize our Geth node:

Let’s use the above file to initialize our blockchain:

You should see the output similar to this

Geth initialization result

Start Geth

Congratulation, you have your own private blockchain running now. Next step, we should create an account and then mine it some ETH. To do so, we need to connect (or attach) to the blockchain and issues some commands:

After this, you should have your Geth console ready.

Create a new account:

You will have to choose a password for your account. Once it’s created, note your wallet address, you’ll need it later to query the balance. In this case, my address is 0xe7fc9272efe566bee94c2f560124654b378c8547

As this is a new account, we won’t have any ETH, as you can check with:

Mine some ETH

Let’s give our account some ETH (the ETH is valid inside your blockchain only).

You should wait some seconds before stopping the miner. I have 400 ETH after this step.

You’re ready to move to the next step.

Expose your blockchain to the Internet.

You have your blockchain running. But how can we connect to it from our Android emulators or our phones? The easiest way is to expose it to the Internet and give us a proper URL. To make that happen, I use ngrok. You can download it from here.

Start ngrook to proxy our blockchain (listening on port 8545):

You should see some output similar to this:

ngrok proxy output

Here, my blockchain public address is https://8fec33672997.ngrok.io or http://8fec33672997.ngrok.io

It’s quite a lot of information, so let’s summarize what we have so far before moving to the final step. Here is the diagram for all we have set up:

ngrok proxies local blockchain

On your laptop/desktop, you have your own Geth node running a private blockchain, and a ngrok client. On ngrok backend, they created a custom domain (8fec33672997.ngrok.io) and proxied all HTTP requests to that domain to our ngrok client. This client, then proxied these requests to our Geth node as a middle man. When our blockchain responds, the flow is reverted. The response comes to the local ngrok client, then ngrok backend, and finally reaches the requester (our Android app).

Query your ETH balance from an Android application

Most of the articles usually will teach us how to build a web app to consume a blockchain. But in this article, I want to start with a mobile app instead. Let's give mobile developers some inspiration, right?

I’ll use Android Studio and create a new project with an empty Activity. After that, we need to import the Web3j library. This library will do all the heavy lifting and help us to talk with blockchain through nice APIs. Update your build.gradle to include this dependency:

At the time when I’m writing this article, I also have to enable the multiDex feature to be able to build the app.

And now, as promised, here are the 7 lines of code to query my balance:

You can put the code in the onCreate method of the MainActivity or anywhere you see fit. This is just a proof of concept of how you can build your dApp on Android instead of a traditional web app.

You’ll need to change the ngrok URL and your account respectively. After that, just run the app on an emulator and see your result.

Conclusion

I want to share a quick and short tutorial for any developers that are new to the blockchain and Etherum. Hopefully to get your feet wet. This could be the first step of a long journey to write a first DApp using a mobile-first approach. I hope you like the article and see you soon in my next articles 🕶.

--

--