Build your first Android Dapp using Web3j and Infura

Madhurakunjir
3 min readAug 20, 2020

--

Want to learn how to write a Decentralized Android application(Dapp) to interact with Ethereum Blockchain? Well, you are in a right place then. In this article lets learn to build a simple Android dapp that transfers ether from one address to another.

Web3j: A java library to interact with smart contracts and integrate applications with Ethereum blockchain.

Infura: Infura lets us connect our dapp to the Ethereum blockchain without requiring us to download the entire blockchain or operate as a node

We will divide this process into the following subparts:

  1. Add Web3j dependency
  2. Connect to Ethereum network
  3. Create an offline wallet
  4. Load an existing offline wallet
  5. Load an existing wallet from mnemonic
  6. Transfer ether to given address

Add Web3j dependency to your project

First, add mavenCentral()to the build.gradle file(Project).

build.gradle file (Project)

Then add web3j dependency implementation 'org.web3j:core:4.6.0-android' to the build.gradle file(Module).

build.gradle file (Modules)

Also don’t forget to add the internet permission in AndroidManifest.xml file.

AndroidManifest.xml file

Connect your Dapp to Ethereum network

Here we use Infura which connects our application to the Ethereum blockchain in just one line of code. You just need to create your project on infura and it will assign you a unique API Id. Further, Web3j sends an asynchronous request to check if our app is connected.

Connect to Ethereum Function

Replace yourAPIid with the Id you get from infura.

Create a new offline wallet

Offline wallets are less vulnerable to potential hacking as compared to online wallets. Let us see how we can create one on our mobile.

Web3j’s WalletUtilsprovides a method to create a JSON encrypted offline wallet. This method takes two parameters first is the wallet password and next is a File object with a wallet path. This path is the one where our wallet will be located.

Wallet Creation Function

Remember the wallet name and password to load it in the future.

Load the existing offline wallet into your app

Once the wallet is created or if there is an existing offline wallet, it can be loaded into your app using Web3j’s WalletUtils class. The wallet is loaded into a Credentials object with the help of a password and wallet name.

Credentials object help us know the address, balance, etc of a particular account.

Obtain Address and Balance

Load a wallet from mnemonic

Just like a password, you can access a wallet through a mnemonic (12-word phrase). This mnemonic is supported by the HD wallets and control all the private keys of our accounts.

Here, let us see how we can load a Metamask account into our app from the mnemonic. The default derivation path used by Web3j is m/44'/60'/0'/1 . This path will create a new account on our Metamask wallet but we want our existing account to load, so we will have to create our derivation path.

Bip32ECKeyPairallows us to do so using the mnemonic and then load the account into the Credentials object.

Import from Mnemonic Function

Transfer ether to a given address

Now as the wallet is loaded and the account is set up, let us learn to transfer some ether to a given address.

Transfer ether

The Transfer class provides a method that takes care of nonce, gas price, gas limit, signature, etc. All these things are included in the Web3j object and Credentials object.

Hurray! You have developed your first Android Dapp to transfer funds. The entire code can be found here.

Also if you want to know more about Blockchain Technology and its Android implementation I have recorded some notes here, do visit and let me know your views 😄

--

--