paint-brush
A Primer to Ethereum Development On Android using Web3j and Infuraby@nschapeler
1,803 reads
1,803 reads

A Primer to Ethereum Development On Android using Web3j and Infura

by Nicolas SchapelerJuly 19th, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A simple Android app can send and receive Ether to and from the Rinkeby Testnet. The code for all this can be found here. Using Web3j in Android is easy to set up and connect to the testnet using a node in the cloud. Creating a wallet so we can send Ether, loading the wallet and sending it back is easy. Sending Ether back is simple and easy to do with a simple app on an Android device with no need to be in sync with the network.

Company Mentioned

Mention Thumbnail

Coin Mentioned

Mention Thumbnail
featured image - A Primer to Ethereum Development On Android using Web3j and Infura
Nicolas Schapeler HackerNoon profile picture

Recently I decided I would like to get a bit more into Ethereum development, as blockchain technology had always interested me but I never got around to actually making a project with it. With Android being my favorite environment, I decided this would be my platform. However, upon starting I noticed it was quite difficult to find much information about Ethereum development in mobile, leading to the creation of this article, where I’ll show you how to make a simple Ethereum application using web3j on the Ethereum Testnet Rinkeby.

1. Setting up Web3j in your project

The first step of working with Web3j in Android is to add it to your project. Since Web3j has a maven plugin, this is very easy: Simply add mavencentral to your project build.gradle file and then add web3j as a dependency to your application build.gradle file (make sure you are using the Android version).

    repositories {
        mavenCentral()
        google()
        jcenter()
    }
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'org.web3j:core:4.1.0-android'
}

Since we’ll be using the internet to interact with the Ethereum network, specify this permission in the android manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.web3j_intro">
    <uses-permission android:name="android.permission.INTERNET"/>

2. Deciding on what type of node to use and connecting through it

When communicating with the Ethereum blockchain, one must do this through a node. Explaining what a node is exactly would be out of the scope of this article, however, what is important about these is that they are used to send and receive information to and from the Ethereum blockchain.
Now, to set one of these up on a mobile device, you have the choice of either running a private node on the device or through a node in the cloud, which in our case is supplied by Infura. The reason I decided to use Infura, is that running any node requires it to be in sync with the Ethereum network, meaning upon first initializing such a node it takes a bit of time and a lot of memory (for mobile device standards) to start up, which I was looking to avoid (if you are nevertheless interested in running a private node, I suggest taking a look into go-Ethereum mobile).

You can easily sign up for an API key at Infura. Once you have, use your endpoint link to create a new Web3j object and connect to the Rinkeby Testnet:

// FIXME: Add your own API key here
web3 = Web3j.build(new HttpService("https://rinkeby.infura.io/v3/YOURKEY"));
try {
      Web3ClientVersion clientVersion = web3.web3ClientVersion().sendAsync().get();
      if(!clientVersion.hasError()){
      //Connected
      }
       else {
         //Show Error
       }
}
catch (Exception e) {
  //Show Error
}

If everything went well, you just connected to the Rinkeby Ethereum Testnet. Congratulations!

3. Creating a wallet

Let’s create a wallet so we can send and receive testnet ether next. To do this, we need to create a wallet file inside the user's device:

//FIXME: Use your own password here
private final String password = "medium";
private String walletPath = getFilesDir().getAbsolutePath();
private File walletDir  = new File(walletPath);

try{
   WalletUtils.generateNewWalletFile(password, walletDir);
 }
catch (Exception e){
  //Display an Error
}

4. Getting the address & loading the wallet

Great, now that we have a wallet, let’s get its address so we can load it with a bit of Testnet ether from the Rinkeby faucet.

try {
  Credentials credentials = WalletUtils.loadCredentials(password, walletDir);
  Toast.makeText(this, "Your address is " + credentials.getAddress(), Toast.LENGTH_LONG).show();

}
catch (Exception e){ 
  //Show Error
}

5. Sending Transactions

After having loaded our wallet with some Rinkeby Ether, let’s try sending it back:

try{
  Credentials credentials = WalletUtils.loadCredentials(password, walletDir); TransactionReceipt receipt = Transfer.sendFunds(web3,credentials,"0x31B98D14007bDEe637298086988A0bBd31184523",new BigDecimal(1),Convert.Unit.ETHER).sendAsync().get(); 
  Toast.makeText(this, "Transaction complete: " +receipt.getTransactionHash(), Toast.LENGTH_LONG).show();
} 
catch (Exception e){ 
  //Show Error
}

Conclusion

And that’s it! You just created a simple Android app to handle the Ethereum Blockchain capable of sending and receiving Ether. The code for all this can be found here.

Thanks for following along!

Nicolas

If you liked this, give this story a couple of reactions so more people can see it!

If you LOVED this, buy me a coffee :)

Ethereum: 0x190d71ba3738f43dc6075f5561e58ac9d4e3dfc2

Bitcoin: 1BRucWzs2vnrkfmbfss14ZQErW74A1H1KE

Litecoin: LbNfoJfTFVCj7GFaAUtWqhitHJcJ3m8y3V