Getting the Geth Dependency in your app’s build.Gradle

You will first need to add Geth as a dependency to your app’s build.Gradle.

// Your App's build.gradle dependencies {

...



// Geth

compile 'org.ethereum:geth:1.7.3';

}

We’re also going to be connecting to the internet so make sure you give your app permission to use the internet in its AndroidManifest.xml file

// AndroidManifest.xml <uses-permission android:name="android.permission.INTERNET" />

Holding a Geth node in a singleton:

The first thing you need to do is start a Geth node on the “Main” Ethereum network. This node needs to be active and running throughout the life of the app, so we will hold the node instance in a singleton. Lets first work on crating our singleton before we work on starting the node itself. You’ll see in the project code I’ve linked to at the top of this article, that I have created a GethNodeHolder.java class which is our singleton.

// GethNodeHolder.java public class GethNodeHolder {



private Node node;

private Account account;

private static GethNodeHolder instance = null;



private GethNodeHolder() {

}



public static GethNodeHolder getInstance() {

if (instance == null) {

instance = new GethNodeHolder();

}

return instance;

}



public Node getNode() {

return node;

}



public void setNode(Node node) {

this.node = node;

}



public Account getAccount() {

return account;

}



public void setAccount(Account account) {

this.account = account;

}



}

This singleton not only has methods to set and get a Geth node, but also to setand get an Ethereum account that we will created after the node is up and running.

Starting a Geth node

Lets work on starting a Geth node in the MainActivity.java file’s onCreate() method.

// MainActivity.java @Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);



try {

NodeConfig nc = new NodeConfig();

Node node = Geth.newNode(getFilesDir() + "/.ethNode", nc);

node.start();



GethNodeHolder gethNode = GethNodeHolder.getInstance();

gethNode.setNode(node);



} catch (Exception e) {

Log.e("error: ", e.getMessage());

e.printStackTrace();

}

}

To create a Geth node you first need a NodeConfig to be passed to Geth’s newNode() method. The newNode() method creates a new Geth node, and stores its NodeConfig in the app’s default file system in a file named “ethNode”. We then start the node by calling its start() method. This starts the node and connects it to the “Main” Ethereum network

Once the node is started we get an instance of our GethNodeHolder singleton and call its setNode() method, passing in our newly started node. This insures that the node will be stored in our GethNodeHolder singleton and will be alive throughout the life of the app.

Create a new Ethereum Account on the “Main” Network

Now that we have a node started lets interact with it by simply creating a new Ethereum account on the “Main” Ethereum network. We will store this account’s information securely on the device using an encrypted Geth Keystore. We will also create a very simple user interface with a button to create a new account. Lets look at the UI part first. Its just a simple button that will call a createAccount() method that we will define in a moment. It also has a TextView that will later display the newly created account’s address.

// activity_main.xml <?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="software.toybox.derek.geth.MainActivity">



<Button

android:id="@+id/create_acc_btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="createAccount"

android:text="Create Account"

app:layout_constraintBottom_toTopOf="@+id/acc_display_textview"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.5"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />



<TextView

android:id="@+id/acc_display_textview"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Account Display"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.5"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/create_acc_btn" />



</android.support.constraint.ConstraintLayout>

Next lets look at the createAccount() method that is called when a user taps the “Create Account” button.

// MainActivity.java public void createAccount(View button) {



try {

GethNodeHolder gethNodeHolder = GethNodeHolder.getInstance();

Node gethNode = gethNodeHolder.getNode();



if (gethNode != null) {



KeyStore ks = new KeyStore(getFilesDir() + "/keystore", Geth.LightScryptN, Geth.LightScryptP);

Account newAccount = ks.newAccount("Password");



Log.d("account address: ", newAccount.getAddress().getHex());

gethNodeHolder.setAccount(newAccount);



Account account = gethNodeHolder.getAccount();

accDisplayTextView.setText("Here is your Account Address: " + account.getAddress().getHex());

}

} catch (Exception e) {

Log.d("error: ", e.getMessage());

e.printStackTrace();

}

}

This method first gets the GethNodeHolder singleton and then gets the node we set in it. Then it creates a new KeyStore() instance, which is a secure Geth Keystore to hold account information on the device. The first argument is the path where you want the keystore file created on the device. We are using the app’s default files directory, and creating a directory called “/keystore”. You will also see two other arguments passed to the new KeyStore() constructor, Geth.LightScryptN and Geth.LightScryptP. These arguments are saying we only want to download the headers from the Ethereum blockchain associated with any Ethereum accounts we decide to store in this keystore. Then we create a new account by calling the KeyStore’s newAccount() method and passing it a hardcoded password of, you guessed it, “password”. Now with our new account created we call our GethNodeHolder’s setAccount() method and pass it our newly created Ethereum account. Then for fun we get the account back out of the GethNodeHolder (for demonstration purposes mainly). And finally we display the account’s address to the user.

Conclusion

Writing this article has made me realize that there is really not much documentation out there on using Geth on mobile devices. The technology is so new and the biggest reason for docs not being out there is because the technology itself just isn’t there yet I’ve realized that the Ethereum technology and blockchain technology as a whole still has a lot to be figured out.

I did however find a framework being developed for Ethereum on Android:

Stay tuned to future updates to this post, and most likely a similar post on how to do this on iOS.