How to implement Ethereum blockchain in Android using Web3j

Boemo Mmopelwa
7 min readOct 31, 2020

--

forbes

Blockchain technology is rapidly growing and spreading at a fast scale. The fact that blockchain developers are in demand, in sectors such as finance and insurance makes it lucrative for you to add the blockchain development skill to your developer portfolio.

Developing a blockchain app could be expensive and exhaustive. In this tutorial I will show you how to develop an Ethereum app painlessly and without spending a cent when testing the app using Web3j,Infura and Rinkeby test net.

Web3j is a java library that enables you to create decentralized java applications without having to write integration code for the blockchain platform. It also works with smart contracts. In this article we will be using the web3j library to create an Ethereum wallet, get wallet address and also retrieve balance and most of all send funds. We will use the Infura API to connect to a blockchain node and also use Rinkeby testnet to test our wallet as it gives us some free testing ethers.

Prerequisites and requirements

  • Infura account.
  • A Facebook or Twitter account for Rinkeby testnet validation.
  • Android Studio.
  • A background of how blockchain technology works.

Take Note: every credentials or keys I create of any website in this article are for teaching purposes and will be deleted .

Infura API

infura.io

Infura gives you the opportunity to connect to an Ethereum network and carry out transactions without maintaining your local node. Infura API Key is used to communicate with the Ethereum blockchain.

Creating an Infura API key.

Click on the Infura website link below to start creating an Infura API key.

  1. Create an account.

2.Create a project.

3.Get the API key.

After your project has been created, copy the endpoint URL. The endpoint is used to send API requests from your decentralized apps and it is the main connection point with the Ethereum blockchain.

You will need this URL later in the tutorial when creating an instance of the web3j class.

Implementing Web3j in Android.

web3j.com

Adding dependencies.

Add the third party web3j library dependency and synchronize the build.gradle file in app module to download the third party library. Do not add compile options if you do not intend to use java lambdas in your java files.

Add app permissions.

To carry out transactions and retrieve balance we will need to connect to the internet ,so we need to add the internet permission. If you create the Ethereum wallet directory on your SD card also add the read and write external storage permission.

Creating the user interface.

For this project we will not focus on attractive UI. The main goal is to focus on the beauty of functionality. Select empty activity when creating the project.

strings.xml

Add the following values to the strings file.

Creating the layout xml file.

Create the following parents and views that will receive users input and display the Ethereum account information in your layout file.

Web3Activity class

Create a java file and name it Web3jActivity and implement the following methods.First import the following classes.

Let’s Start the journey.

Declare a web3j class and Credentials class objects which we will use to invoke the class methods. When the app starts call the setupBouncyCastle method which we will implement later on.

The StrictMode class catches accidental disk or network access on the application’s main thread. The permitall() method disables the detection of everything. This allows you to access network on the main thread. We will create an instance of the web3j which connects to the infura API with the url in the onCreate() method.

Please use your own API key URL you got after creating an infura project.

Next create a file called file which will store the wallet details and check if the directory exists and if does not exist create it with the mkdirs() method.

BouncyCastle

The Bouncycastle crypto-package implements cryptographic algorithms needed for the blockchain security .The Security class centralizes security properties and also manages providers. A provider implements some parts of java Security Algorithms such as DSA, RSA, MD5 or SHA-1. But for web3j we need ECDSA algorithm which will be implemented by the security provider.

To get the provider pass the bouncycastleprovider to the getprovider() method.

ShowToast Method.

This method will take in a string which will be a toast message displayed when we show results or wallet address.

Connecting to an Ethereum Node.

To connect to an Ethereum network, invoke the build() method which takes in the httpservice and connects to the ethereum network using the URL. Check if the client version has an error, the user will only gain access to the ethereum network if the client version has no errors.

Creating an Ethereum wallet.

blockonomi

An Ethereum wallet requires a password and file path. The WalletUtils class provides utility functions for working with Wallet files. We will use its methods which are :

loadcredentials() this method takes in the password and file path .It is used to get credentials of the given wallet’s path and password.

generateLightNewWalletFile() this method takes in the password and file path and then generates the ethereum wallet and json file.

Invoke the getAddress() method which gives the wallet address using the credentials object.

Create two EditTexts for receiving the users wallet directory and also the password.

Sending Funds.

To send funds get the wallet credentials .Transfer funds by using the sendFunds() method which takes in the web3j object ,wallet credentials, destination address , value which is the amount and convert the unit.

The Edtvalue EditText gets the amount of Ether the user wants to send.

When the transaction is successful get the receipt by invoking getTransactionHash() method with TransactionReceipt class object.

As you make a transaction you will be charged with a gas value. Gas refers to the fee required to successfully conduct a transaction on the Ethereum blockchain platform.

Retrieving wallet balance

To get the wallet’s balance pass the address and lastest DefaultBlockParameterName to the ethGetBalance method .Use the getBalance() method of the EthGetBalance class to get the balance.

Note:1 ether (ETH) is equivalent to 1x1018 wei.

How To Use Rinkeby Ethereum Test Network

Rinkeby provides free testing Ether .To test if our app can successfully send funds we will get test ethers from the Rinkeby website.

Getting free test Ether.

  1. Go to rinkeby website to start the journey.

2.Create a twitter post containing an Ethereum wallet address.

3.Paste the link of the Twitter post you posted containing the Ethereum wallet .

4.Click on the give me ether drop down and select how many ethers you want and you will be credited with Rinkeby testnet ethers.

Now we have been credited with Rinkeby test net ether and ready to start testing.

The transaction has been successful using Rinkeby testnet ethers. You can use the etherscan website to check the check full transaction details .

Important links

In Conclusion

In this article we learned how to create a wallet, connect to an Ethereum node, send funds and also retrieve funds. Please leave a comment if you are having any errors.

The full source code of the app we where building is also available on Github.

--

--

Boemo Mmopelwa
Boemo Mmopelwa

Written by Boemo Mmopelwa

Android developer| Tech Enthusiast|Innovative thinking is my passion | Learn and experience more to discover more

Responses (4)