Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android docs added #96

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions basic_usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## Basic Usage

### Initialize Web3j

To interact with an Ethereum node, initialize the `Web3j` instance:

```java
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;

Web3j web3j = Web3j.build(new HttpService("https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"));
System.out.println("Connected to Ethereum: " + web3j.web3ClientVersion().send().getWeb3ClientVersion());
```

### Fetch Account Balance

Retrieve the Ether balance of an account:

```java
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.utils.Convert;
import java.math.BigDecimal;

String address = "0xYourEthereumAddress";
EthGetBalance ethGetBalance = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).send();
BigDecimal balance = Convert.fromWei(ethGetBalance.getBalance().toString(), Convert.Unit.ETHER);
System.out.println("Balance: " + balance + " ETH");
```

### Send a Transaction

Send Ether to another account:

```java
import org.web3j.crypto.Credentials;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Transfer;
import org.web3j.utils.Convert;

Credentials credentials = Credentials.create("YOUR_PRIVATE_KEY");
TransactionReceipt receipt = Transfer.sendFunds(
web3j, credentials, "0xRecipientAddress", BigDecimal.valueOf(0.01), Convert.Unit.ETHER
).send();
System.out.println("Transaction complete: " + receipt.getTransactionHash());
```

### Note on Codegen Module

Currently, the `codegen` module is not available in the Android version. It is under active development and testing, and will be released soon.
49 changes: 49 additions & 0 deletions docs/android/basic_usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## Basic Usage

### Initialize Web3j

To interact with an Ethereum node, initialize the `Web3j` instance:

```java
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;

Web3j web3j = Web3j.build(new HttpService("https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"));
System.out.println("Connected to Ethereum: " + web3j.web3ClientVersion().send().getWeb3ClientVersion());
```

### Fetch Account Balance

Retrieve the Ether balance of an account:

```java
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.utils.Convert;
import java.math.BigDecimal;

String address = "0xYourEthereumAddress";
EthGetBalance ethGetBalance = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).send();
BigDecimal balance = Convert.fromWei(ethGetBalance.getBalance().toString(), Convert.Unit.ETHER);
System.out.println("Balance: " + balance + " ETH");
```

### Send a Transaction

Send Ether to another account:

```java
import org.web3j.crypto.Credentials;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Transfer;
import org.web3j.utils.Convert;

Credentials credentials = Credentials.create("YOUR_PRIVATE_KEY");
TransactionReceipt receipt = Transfer.sendFunds(
web3j, credentials, "0xRecipientAddress", BigDecimal.valueOf(0.01), Convert.Unit.ETHER
).send();
System.out.println("Transaction complete: " + receipt.getTransactionHash());
```

### Note on Codegen Module

Currently, the `codegen` module is not available in the Android version. It is under active development and testing, and will be released soon.
21 changes: 21 additions & 0 deletions docs/android/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Web3j-Android Library

## Introduction

The **web3j-android** library provides an Android-compatible implementation of the **web3j** library, enabling developers to interact with the Ethereum blockchain directly from Android applications. It simplifies blockchain integration for mobile developers by offering a tailored solution that addresses Android-specific constraints and environments.

## Why a Separate Android Version?

Creating a dedicated version for Android was necessary to address compatibility issues between the original Java library and Android-specific environments. This ensures seamless performance and integration within mobile apps.

## Minimum Requirements

- **Java Version**: JDK Version 17 or higher
- **Android SDK Version**: 24 or higher
- **Dependency**: `org.web3j:web3j-android:4.6.0`

## Resources
- [web3j-android GitHub Repository](https://github.com/hyperledger-web3j/web3j/tree/web3j-android)
- [Sample App Repository](https://github.com/shashankiitbhu/web3japp)

For further assistance, refer to the [main web3j documentation](https://web3j.io/).
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Setting up Project for Android

In this article, we’ll walk through the steps to set up Web3j for Android development using the latest version.

> The latest version of Web3j Library (4.12.0) requires at least **JDK version 17**
> The latest version of Web3j Library for android (4.12.3-android) requires at least **JDK version 17**

## Step 1: Add Web3j Dependency

Expand All @@ -15,7 +15,7 @@ Add the following dependency to your pom.xml file:
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.12.0</version>
<version>4.12.3-android</version>
</dependency>
```

Expand All @@ -25,7 +25,7 @@ Add the Web3j dependency to your build.gradle.kts file :

```kotlin
dependencies {
implementation("org.web3j:core:4.12.0")
implementation("org.web3j:core:4.12.3-android")
}
```

Expand Down
103 changes: 103 additions & 0 deletions docs/android/testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Testing with Web3j-Android

## Overview

Testing your Ethereum-based Android application is an essential step in ensuring its stability, functionality, and blockchain interaction capabilities. This guide explains how to test your app effectively with **web3j-android**, focusing on local blockchain environments, tools, and best practices.

## Setting Up a Local Ethereum Node

A local Ethereum node allows you to simulate the blockchain environment for development and testing without interacting with the actual mainnet or testnets.

### Using Ganache

Ganache is a popular tool for setting up a local Ethereum blockchain. It provides:

- Instant mining for testing transactions
- Pre-funded test accounts
- A user-friendly interface for inspecting the blockchain state

### Steps to Set Up Ganache

1. **Download and Install Ganache**

- Visit [Truffle Suite](https://trufflesuite.com/ganache/) to download and install Ganache on your development machine.

2. **Run Ganache**

- Start Ganache and note the RPC URL (default: `http://127.0.0.1:7545`).
- Take note of the pre-funded accounts and their private keys for testing.

3. **Update Your App Configuration**
- Point your `web3j` instance to the local Ganache RPC URL:
```java
Web3j web3j = Web3j.build(new HttpService("http://10.0.2.2:7545"));
```
> **Note**: Use `10.0.2.2` instead of `127.0.0.1` when testing in an Android emulator.

## Writing Test Cases

### Testing Connectivity

Ensure your app can connect to the local blockchain:

```java
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;

public class TestConnection {
public static void main(String[] args) throws Exception {
Web3j web3j = Web3j.build(new HttpService("http://10.0.2.2:7545"));
String clientVersion = web3j.web3ClientVersion().send().getWeb3ClientVersion();
System.out.println("Connected to Ethereum client: " + clientVersion);
}
}
```

### Testing Account Balance

Verify the Ether balance of a test account:

```java
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.utils.Convert;
import java.math.BigDecimal;

public class TestBalance {
public static void main(String[] args) throws Exception {
Web3j web3j = Web3j.build(new HttpService("http://10.0.2.2:7545"));
String address = "0xYourTestAccountAddress";
EthGetBalance balance = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).send();
BigDecimal etherBalance = Convert.fromWei(balance.getBalance().toString(), Convert.Unit.ETHER);
System.out.println("Account Balance: " + etherBalance + " ETH");
}
}
```

### Testing Transactions

Simulate sending a transaction using a test account:

```java
import org.web3j.crypto.Credentials;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Transfer;
import org.web3j.utils.Convert;
import java.math.BigDecimal;

public class TestTransaction {
public static void main(String[] args) throws Exception {
Web3j web3j = Web3j.build(new HttpService("http://10.0.2.2:7545"));
Credentials credentials = Credentials.create("YourPrivateKey");
TransactionReceipt receipt = Transfer.sendFunds(
web3j, credentials, "0xRecipientAddress", BigDecimal.valueOf(0.01), Convert.Unit.ETHER
).send();
System.out.println("Transaction complete: " + receipt.getTransactionHash());
}
}
```

## Resources for Testing

- [Ganache Documentation](https://trufflesuite.com/docs/ganache/)
- [JUnit for Java Testing](https://junit.org/)
- [web3j-android GitHub Repository](https://github.com/hyperledger-web3j/web3j/tree/web3j-android)
6 changes: 5 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ nav:
- Quickstart: quickstart.md
- Getting Started:
- Run a Node locally: getting_started/run_node_locally.md
- Setting up project for Android: getting_started/setup_android.md
- Interacting with a Node: getting_started/interacting_with_node.md
- Deploy and Interact with Smart Contracts: getting_started/deploy_interact_smart_contracts.md
- Filters and Flowables: getting_started/pub_sub.md
Expand Down Expand Up @@ -78,6 +77,11 @@ nav:
- Links and Useful Resources: references/links_and_useful_resources.md
- Thanks and Credits: references/thanks_and_credits.md
- Javadoc: https://docs.web3j.io/latest/javadoc-api/
- Web3j Android:
- Introduction: android/introduction.md
- Installation: android/setup_android.md
- Basic Usage: android/basic_usage.md
- Testing: android/testing.md

# Configuration
theme:
Expand Down