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).
Since we’ll be using the internet to interact with the Ethereum network, specify this permission in the android manifest file.
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:
If everything went well, you just connected to the Rinkeby Ethereum Testnet. Congratulations! (If not, make sure you are using .sendAync().get() instead of .send(), as .send() is broken at the time of the writing of this article.
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:
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.
5. Sending Transactions
After having loaded our wallet with some Rinkeby Ether, let’s try sending it back:
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!
If you liked this, give this story a couple of claps so more people can see it!